Storing Multiple Credentials
-
Edit the AWS CLI Configuration File:
The configuration files are located in
~/.aws/credentials
for credentials and~/.aws/config
for configuration. You can edit these files directly or use theaws configure
command with the--profile
option.-
Credentials file (
~/.aws/credentials
):[default] aws_access_key_id = YOUR_DEFAULT_ACCESS_KEY_ID aws_secret_access_key = YOUR_DEFAULT_SECRET_ACCESS_KEY [profile1] aws_access_key_id = YOUR_PROFILE1_ACCESS_KEY_ID aws_secret_access_key = YOUR_PROFILE1_SECRET_ACCESS_KEY [profile2] aws_access_key_id = YOUR_PROFILE2_ACCESS_KEY_ID aws_secret_access_key = YOUR_PROFILE2_SECRET_ACCESS_KEY
-
Config file (
~/.aws/config
):[default] region = us-east-1 output = json [profile profile1] region = us-west-2 output = json [profile profile2] region = eu-west-1 output = json
-
-
Add Credentials Using AWS CLI:
You can also use the
aws configure
command with the--profile
option to add credentials.aws configure --profile profile1 aws configure --profile profile2
Follow the prompts to enter the
aws_access_key_id
,aws_secret_access_key
,region
, andoutput
.
Using Named Profiles
-
Specify a Profile for a Single Command:
You can specify a profile to use for a single AWS CLI command with the
--profile
option.aws s3 ls --profile profile1
-
Set a Profile as the Default for a Session:
You can set the
AWS_PROFILE
environment variable to specify which profile to use for the current session.-
Bash:
export AWS_PROFILE=profile1
-
Windows Command Prompt:
set AWS_PROFILE=profile1
-
PowerShell:
$env:AWS_PROFILE="profile1"
-
Listing All Profiles
To list all configured profiles, you can use the following command:
aws configure list-profiles
This will output a list of all profile names:
default
profile1
profile2
Example Workflow
-
Configure Profiles:
aws configure --profile profile1 aws configure --profile profile2
-
List Profiles:
aws configure list-profiles
-
Use a Specific Profile:
aws s3 ls --profile profile1
-
Set a Profile for a Session:
export AWS_PROFILE=profile1
Now, any subsequent AWS CLI commands will use
profile1
credentials until you change the profile or close the session.
By setting up multiple profiles, you can manage and switch between different AWS credentials easily on your local machine.