Setting Multiple Profile for AWS CLI

AWS CLI

The AWS Command Line Interface (AWS CLI) is an open source tool that enables you to interact with AWS services using commands in your command-line shell.

AWS CLI versions

The AWS CLI is available in two versions and information in this guide applies to both versions unless stated otherwise.

Version 2.x – The current, generally available release of the AWS CLI that is intended for use in production environments. This version does include some “breaking” changes from version 1 that might require you to change your scripts so that they continue to operate as you expect.

Version 1.x – The previous version of the AWS CLI that is available for backwards compatiblity.

Named profiles
A named profile is a collection of settings and credentials that you can apply to a AWS CLI command. When you specify a profile to run a command, the settings and credentials are used to run that command.

The AWS CLI supports using any of multiple named profiles that are stored in the config and credentials files. You can configure additional profiles by using aws configure with the –profile option, or by adding entries to the config and credentials files.

Execute the below command, it will ask access key and secret key for the ‘user1’

$ aws configure --profile user1
$ ~/.aws/credentials

[default] 
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY 
[user1] 
aws_access_key_id=AKIAI44QH8DHBEXAMPLE
aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY

Each profile can specify different credentials—perhaps from different IAM users—and can also specify different AWS Regions and output formats.

$ cat ~/.aws/config

[default] 
region=us-west-2 
output=json 

[profile user1] 
region=us-east-1 
output=text

Using profiles with the AWS CLI

To use a named profile, add the –profile profile-name option to your command. The following example lists all of your Amazon EC2 instances using the credentials and settings defined in the user1 profile from the previous example files.

$ aws ec2 describe-instances --profile user1

To use a named profile for multiple commands, you can avoid specifying the profile in every command by setting the AWS_PROFILE environment variable at the command line.

$ export AWS_PROFILE=user1 

Now you can use AWS CLI without specifying the –profile in the following command. 

Leave a Reply