As a AWS user, you likely have multiple accounts for different projects or teams. However, managing multiple AWS accounts can be a difficult task, especially if you don’t want to run your tests using the web interface provided by Amazon. Fortunately, the AWS CLI offers a simple solution to this problem: Profiles.
What is AWS CLI?
The AWS Command Line Interface is an open source tool from Amazon Web Services (AWS). You can use it to interact with AWS services using commands in your command-line shell.
What are AWS CLI profiles?
AWS CLI profiles allow you to interact with different AWS accounts. Each profile has its own settings, including login credentials, region, and other settings.
Requirements
To follow this guide you need these requirements:
- AWS account with access credentials (access key ID and access secret key).
- AWS CLI installed on your computer. You can download and install here AWS CLI
- Basic knowledge of the command line.
1. Create multiple AWS CLI profiles using the command lines
To create a new profile, follow these steps:
- Verify that you have AWS-CLI installed using the following command in your terminal:
aws --version
# sample output: aws-cli/2.15.16
- Open the command prompt on your computer and type the following command to create a new profile:
aws configure --profile <nombre del perfil>
- For example, if you want to create a profile named user1, you can run the following command:
aws configure --profile user1
- Follow the next steps to provide access credentials, region, and other values for the new profile. Make sure the values you provide are correct. The information required is the following:
- AWS Access Key ID
- AWS Secret Access Key
- Default region name (optional)
- Default output format (optional) - Repeat the steps for each additional profile you want to create.
- You will see that if you enter the path ~/.aws you will find two files: “config” and “credentials” and when you print both files you will find the user1 profile that you just configured in the previous steps.
- To print the “config” file run this command in your terminal
cd ~/.aws && cat config
- To print “credentials” run this command in your terminal
cd ~/.aws && cat credentials
2. Create multiple AWS CLI profiles using configuration files
To configure a new profile using the configuration files, follow these steps:
- Verify that you have AWS-CLI installed using the following command in your terminal:
aws --version
# sample output: aws-cli/2.15.16
- Open the command prompt on your computer and type the following command to list all configured profiles:
aws configure list-profiles
# output: default
Profiles are stored in the setting files in the `~/.aws` path. You can setup additional profiles by adding entries to the files. In this case, we will add a new profile `user2`:
$ nano ~/.aws/config
[default]
region=us-west-2
output=json
# Include the prefix "profile" only when configuring a named profile in the config file
[profile user2]
region=us-east-1
output=json
$ nano ~/.aws/credentials
[default]
aws_access_key_id=DANY08141327EXAMPLE
aws_secret_access_key=dAnYBayMAxEÑXpkJsy/KPxRfiCYEXAMPLElKEY
# Don´t use the prefix "profile"
[user2]
aws_access_key_id=YOUR_ACCESS_KEY
aws_secret_access_key=YOUR_SECRET_ACCESS_KEY
- Verify that the `user2` profile has been added correctly by running the following in your terminal, you should be able to see your new profile in addition to the default one:
aws configure list-profiles
# output:
# default
# user2
3. How to use a specific profile using the flag — profile
- To use a specific profile to run some AWS CLI command, you can place the _ — profile_ flag at the end of your command.
aws s3 ls --profile <nombre del perfil>
- For example, if you want to list the S3 buckets for the account that you configured with the user1 profile, you can run the following command:
aws s3 ls --profile user1
4. How to use a specific profile using the environment values
- To use a specific profile without constantly adding the profile tag, you can set a profile as the default with the `AWS_PROFILE=profile_name` environment variable by running the following command in your terminal.
export AWS_PROFILE=<profile_name>
Note: Setting the env variable changes the default profile until the end of your shell session or until you set the variable to a different value
- For example, if you want to set the profile `user2` as your environment variable, you can run the following command:
export AWS_PROFILE=user2
- To verify your current configured profile, run the following command:
aws configure list
#output: user2
Conclusión
When you finish this guide, you will be able to create and manage multiple profiles on your computer and interact with different accounts easily. It is also possible to change the path of the configuration files, which by default is `~/.aws`. An example to change the default path of the configuration files will remain pending. Author: dany0814