Use multiple AWS accounts using AWS CLI

Using multiple AWS accounts from the command line is natively handled with profiles in the AWS CLI.

Using aws configure

Using the aws configure command, multiple profiles can be configured.

1
2
3
4
5
aws configure --profile account1
AWS Access Key ID [None]: ...
AWS Secret Access Key [None]: ...
Default region name [None]: ...
Default output format [None]: ...

Then, the --profile account1 option may be used with future commands.

1
aws s3 ls --profile account1

Or an environment variable may be set.

1
2
export AWS_PROFILE=account1
aws s3 ls # uses account1 credentials

Note: If the profile is named --profile default, it will represent the default profile when no --profile argument is provided.

Manually setting credentials

The ~/.aws/credentials and ~/.aws/config files can be modified directly.

  1. Add the credentials to the ~/.aws/credentials file
1
2
3
4
5
6
7
[default]
aws_access_key_id=accesskey
aws_secret_access_key=secretaccesskey

[account1]
aws_access_key_id=accesskey
aws_secret_access_key=secretaccesskey
  1. Add the profile to the ~/.aws/config file
1
2
3
4
5
6
7
[default]
region=us-east-1
output=json

[profile account1]
region=us-east-1
output=json
  1. Use the --profile argument or set the AWS_PROFILE environment variable.
1
aws s3 ls --profile account1

OR

1
2
export AWS_PROFILE=account1
aws s3 ls # uses account1 credentials