AWS multiple accounts/profiles

If you’re working with multiple AWS accounts or environments, using named profiles with the AWS CLI can be very handy. Named profiles are configurations of AWS access keys, secret keys, and default regions. Here’s how you can set up and use multiple profiles:

Setting Up Multiple Profiles

You can add profiles directly to your AWS configuration file (~/.aws/config) and your credentials file (~/.aws/credentials).

Here’s an example for ~/.aws/credentials:

[default]
aws_access_key_id = YOUR_DEFAULT_ACCESS_KEY
aws_secret_access_key = YOUR_DEFAULT_SECRET_KEY

[profileName1]
aws_access_key_id = YOUR_ACCESS_KEY_FOR_PROFILE1
aws_secret_access_key = YOUR_SECRET_KEY_FOR_PROFILE1

[profileName2]
aws_access_key_id = YOUR_ACCESS_KEY_FOR_PROFILE2
aws_secret_access_key = YOUR_SECRET_KEY_FOR_PROFILE2

And for ~/.aws/config:

[default]
region = us-west-1

[profile profileName1]
region = us-east-1

[profile profileName2]
region = eu-central-1

Alternatively, you can use the AWS CLI to configure these profiles:

aws configure --profile profileName1

This command will prompt you for the necessary details for profileName1.

Using a Specific Profile To use a specific profile with the AWS CLI, use the –profile flag:


aws s3 ls --profile profileName1