Use Multiple AWS Accounts with AWS CLI

Use Multiple AWS Accounts with AWS CLI

AWS CLI

AWS CLI is Command Line Utility tool for Amazon Web Services which makes it easy for user to control multiple AWS services from command line. AWS CLI is just one another tool other than AWS Management Console provided by AWS to view , monitor , control AWS services.

Multiple Account Usage

With the help of AWS CLI you can set up multiple named profiles which are collection of settings and credentials relating to that particular AWS Account. Multiple named profiles are stored in the config and credentials files.

Get Your Default AWS Profile

Run aws configure list in command line to fetch your default profile and current configuration values. Output will look something like below.

list — AWS CLI 1.27.1 Command Reference - Google Chrome 04-11-2022 19_13_35.png It provides you with profile name ,access key , secret access key , region , location of where each of these variable is set. Here the name of default profile is not set , region is us-west-2 which is stored as an environment variable.

Using the following command you can also fetch the account id for your default / current AWS Account in use

aws sts get-caller-identity --query "Account" --output text

Set up Multiple AWS Accounts/Profiles for AWS CLI

You can configure additional profiles/accounts by using aws configure with the --profile option, or by manually adding entries to the config and credentials files.

Create Named Profile using Command line

Type aws configure --profile demoUser

This is will create another profile named demoUser. Proceed to add access key , secret access key , region and output for this account.

After you have finished the set up , you can checkout your default and other profile in config and credential files

# on Linux and macOS 
~/.aws/credentials
~/.aws/config

# on Windows
C:\Users\USERNAME\.aws\credentials
C:\Users\USERNAME\.aws\config

List All Profiles using Command Line

To list all your profile names, use the aws configure list-profiles command.

Using named profiles / Switching Account

To use a named profile, add the --profile profile-name option to your command.

Examples -

aws sts get-caller-identity --query "Account" --output text  --profile demoUser  # list Account ID for demoUser

aws ec2 describe-instances --profile demoUser   # List down all EC2 Instances for demoUser

aws s3 ls --profile demoUser   # List down all buckets for demoUser

The default Profile is used when you run a AWS CLI command with no --profile option / parameter .

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.

Set Environment Variable on Windows

# on Windows with CMD.exe
setx AWS_PROFILE demoUser

Verify Environment Variable is set on your Machine

# on Windows with CMD.exe
echo %AWS_PROFILE%

Conclusion

Via the AWS CLI , we can switch between multiple accounts to use aws services

Feel free to add suggestion / review. Thank you for reading and/or following along with the Blog.

Happy Learning!!!!