DEV Community

Cover image for How to work with multiple AWS CLI accounts
Santiago Orlando
Santiago Orlando

Posted on

How to work with multiple AWS CLI accounts

Sometimes it happens that one needs to work with multiple AWS accounts in the same computer.

If that’s your case, the way to do that is the following:

First if you don’t have AWS Cli V2 installed, you should do the following steps, as explained on the AWS documentation.

Once you have aws cli v2 installed in your computer, now you need to set up your AWS profile, as explained in this link.

There are two ways to do that.

If you don’t specify anything, you just write the following on your terminal, and then follow the steps as always:

$ aws configure
Enter fullscreen mode Exit fullscreen mode

It will start a command prompt asking you to complete the details as following:

AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json
Enter fullscreen mode Exit fullscreen mode

Then if you write the following on your terminal:

$ aws configure list-profiles
Enter fullscreen mode Exit fullscreen mode

It will list your profiles, and that profile will be named as “default”

Also you can write this command, and it will show you the profile that you have currently set as “default”:

$ aws configure list
Enter fullscreen mode Exit fullscreen mode

The other way to work with that is naming your profiles when you first configure it, in this way:

$ aws configure — profile account1
Enter fullscreen mode Exit fullscreen mode

Then if you want to switch between one of this profiles, you do it by switching them as your default profile, doing the following:

$ export AWS_DEFAULT_PROFILE=account1
Enter fullscreen mode Exit fullscreen mode

Note: It’s important to write it just as appears above, in capital letters and without spaces, otherwise it won’t work.

Other way to work with the profiles are specifying with which profile you want to run certain command, like this:

$ aws dynamodb list-tables — profile account1
$ aws s3 ls — profile account2
Enter fullscreen mode Exit fullscreen mode

Also another way to work with different AWS profiles, is to specify that on the .ENV variables of your project, like this:

AWS_ACCESS_KEY_ID=”your_key_id”
AWS_SECRET_ACCESS_KEY=”your_secret_key”
Enter fullscreen mode Exit fullscreen mode

If for any reason you need to edit or view where all this magic is stored on your computer, this is the path where you can find the aws cli config and credentials:

~/.aws/credentials

~/.aws/config
Enter fullscreen mode Exit fullscreen mode

Note:If you can’t see it, you need to allow seeing hidden files.

For more info related with that, you can read this document.

Hope it was clarifying and helped you.
If you have any comments or anything to add, please don’t hesitate to do it on the comment section!

Have a great day :)

Top comments (1)

Collapse
 
mmuller88 profile image
Martin Muller 🇩🇪🇧🇷🇵🇹

Nice article but now the days I prefer AWS SSO where it gives you temporarly CLI credentials. zero setup necessary.