You can configure multiple profiles in the AWS CLI configuration file to use multiple AWS accounts from the command line. Here is a short example for you –
Open a command line and execute aws configure command with –profile parameter for your first AWS account. Provide your first AWS account IAM user’s access key and secret key when prompted.
1 2 3 4 5 6 7 |
## configure aws cli for 1st account aws configure --profile account1_user #AWS Access Key ID [None]: #AWS Secret Access Key [None]: #Default region name [None]: #Default output format [None]: |
Perform the same operation for your second AWS account using a different profile name.
1 2 3 4 5 6 7 |
## configure aws cli for 2nd account aws configure --profile account2_user #AWS Access Key ID [None]: #AWS Secret Access Key [None]: #Default region name [None]: #Default output format [None]: |
Once done, your AWS config and the credential file should content below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## config file contents | cat .aws/config ## [profile account1_user] ## region = ## ## [profile account2_user] ## region = ## credential file contents | cat .aws/credential ## [account1_user] ## aws_access_key_id = ## aws_secret_access_key = ## ## [account2_user] ## aws_access_key_id = ## aws_secret_access_key = |
Now test your credentials for both accounts to see if they are working
1 2 3 |
## Check if aws cli properly configured aws sts get-caller-identity --profile account1_user aws sts get-caller-identity --profile account2_user |
This should return your account ID and username for each AWS account. If you haven’t install AWS CLI yet, follow the below link to install AWS CLI first.