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.
1aws configure --profile account1
2AWS Access Key ID [None]: ...
3AWS Secret Access Key [None]: ...
4Default region name [None]: ...
5Default output format [None]: ...Then, the --profile account1 option may be used with future commands.
1aws s3 ls --profile account1Or an environment variable may be set.
1export AWS_PROFILE=account1
2aws s3 ls # uses account1 credentialsNote: 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.
- Add the credentials to the
~/.aws/credentialsfile
1[default]
2aws_access_key_id=accesskey
3aws_secret_access_key=secretaccesskey
4
5[account1]
6aws_access_key_id=accesskey
7aws_secret_access_key=secretaccesskey- Add the profile to the
~/.aws/configfile
1[default]
2region=us-east-1
3output=json
4
5[profile account1]
6region=us-east-1
7output=json- Use the
--profileargument or set theAWS_PROFILEenvironment variable.
1aws s3 ls --profile account1OR
1export AWS_PROFILE=account1
2aws s3 ls # uses account1 credentials