DEV Community

hmintoh
hmintoh

Posted on

How to: Use multiple AWS accounts with the AWS-CLI

If you are working with multiple AWS accounts, you can easily switch between your accounts by creating multiple profiles on the CLI.

  1. Get list of AWS profiles aws configure list-profiles. By default, there will be one default profile

  2. To add a new profile, you will first need to generate an access_key_id and a secret_access_key.

  3. Profiles are stored under the config and credentials files. You can configure additional profiles by adding entries to the files. In this case, we will add a new profile user1:

$ vim ~/.aws/config

[default]
region=us-west-2
output=json

[profile user1] // Include the prefix "profile" only when 
configuring a named profile in the config file
region=us-east-1
output=json
Enter fullscreen mode Exit fullscreen mode
$ vim ~/.aws/credentials

[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

[user1] // Do not use the prefix "profile" 
aws_access_key_id=YOUR_ACCESS_KEY
aws_secret_access_key=YOUR_SECRET_ACCESS_KEY
Enter fullscreen mode Exit fullscreen mode
  1. Verify that the profile is successfully added by aws configure list-profiles. You should now see 2 profiles:
 default
 user1
Enter fullscreen mode Exit fullscreen mode
  1. To switch between different AWS accounts, set the AWS_profile environment variable at the command line via export AWS_PROFILE=profile_name. Setting the env variable changes the default profile until the end of your shell session or until you set the variable to a different value.

  2. Use aws configure list to list your current profile.

For documentation to named profiles, see guide here.

Top comments (5)

Collapse
 
mckenzm profile image
Matthew

Beware of this, one day you will have many accounts and need to multi-task. Specify the profile explicitly.

Collapse
 
jannekevanhulten profile image
Janneke van Hulten

For windows users, use:
set AWS_PROFILE=profile_name
instead

Collapse
 
layor2257 profile image
Glory Isaiah

how can i make user 1 the default user

Collapse
 
rephidimc profile image
Victor Adeleke Afolayan

Life saver.
Short, direct and powerful article

Collapse
 
mfadhili profile image
mfadhili

thanks a million