Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Get an IAM user using an Amazon SDK
The following code examples show how to get an IAM user.
To avoid security risks, don't use IAM users for authentication when developing purpose-built software
or working with real data. Instead, use federation with an identity provider such as
Amazon IAM Identity Center.
- .NET
-
- Amazon SDK for .NET
-
/// <summary>
/// Get information about an IAM user.
/// </summary>
/// <param name="userName">The username of the user.</param>
/// <returns>An IAM user object.</returns>
public async Task<User> GetUserAsync(string userName)
{
var response = await _IAMService.GetUserAsync(new GetUserRequest { UserName = userName });
return response.User;
}
- Bash
-
- Amazon CLI with Bash script
-
###############################################################################
# function errecho
#
# This function outputs everything sent to it to STDERR (standard error output).
###############################################################################
function errecho() {
printf "%s\n" "$*" 1>&2
}
###############################################################################
# function iam_user_exists
#
# This function checks to see if the specified AWS Identity and Access Management (IAM) user already exists.
#
# Parameters:
# $1 - The name of the IAM user to check.
#
# Returns:
# 0 - If the user already exists.
# 1 - If the user doesn't exist.
###############################################################################
function iam_user_exists() {
local user_name
user_name=$1
# Check whether the IAM user already exists.
# We suppress all output - we're interested only in the return code.
local errors
errors=$(aws iam get-user \
--user-name "$user_name" 2>&1 >/dev/null)
local error_code=${?}
if [[ $error_code -eq 0 ]]; then
return 0 # 0 in Bash script means true.
else
if [[ $errors != *"error"*"(NoSuchEntity)"* ]]; then
aws_cli_error_log $error_code
errecho "Error calling iam get-user $errors"
fi
return 1 # 1 in Bash script means false.
fi
}
- Go
-
- SDK for Go V2
-
// UserWrapper encapsulates user actions used in the examples.
// It contains an IAM service client that is used to perform user actions.
type UserWrapper struct {
IamClient *iam.Client
}
// GetUser gets data about a user.
func (wrapper UserWrapper) GetUser(userName string) (*types.User, error) {
var user *types.User
result, err := wrapper.IamClient.GetUser(context.TODO(), &iam.GetUserInput{
UserName: aws.String(userName),
})
if err != nil {
var apiError smithy.APIError
if errors.As(err, &apiError) {
switch apiError.(type) {
case *types.NoSuchEntityException:
log.Printf("User %v does not exist.\n", userName)
err = nil
default:
log.Printf("Couldn't get user %v. Here's why: %v\n", userName, err)
}
}
} else {
user = result.User
}
return user, err
}
For a complete list of Amazon SDK developer guides and code examples, see
Using IAM with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.