Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to temporarily switch profiles for AWS CLI?

Updated answer (7/10/2021): For AWS CLI v1, do this:

export AWS_DEFAULT_PROFILE=user2

For AWS CLI v2, the following will work:

export AWS_PROFILE=user2

The full question is below for context:


(1.) After successfully configuring a second profile for the AWS CLI, I unsuccessfully tried to set the profile to user2 in my bash session with the following command:

export AWS_PROFILE=user2

... per the advice here: https://docs.aws.amazon.com/cli/latest/userguide/cli-multiple-profiles.html

(2.) The following command works:

aws s3 ls --profile user2

So I know that the AWS CLI and the user2 profile are both working on my computer.

(3.) However, when I subsequently (that is, after entering "export AWS_PROFILE=user2") try something like:

aws s3 ls

... AWS's response assumes that I want to query it as the default user (NOT user2)

(4.) So the only way I can use the user2 profile from the command line is by continuing to append "--profile user2" to every single command, which is tedious.

(5.)

echo $AWS_PROFILE

yields:

>> user2

, as expected.

Any idea what's going on here? I'm sure I'm making some dumb mistake somewhere.

like image 615
James Shapiro Avatar asked Sep 26 '22 11:09

James Shapiro


People also ask

How do I switch between Users in AWS CLI?

To switch between different AWS accounts, set the AWS_profile environment variable at the command line via export AWS_PROFILE=profile_name . 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.

How do I manage multiple AWS CLI accounts?

You can work with two accounts by creating two profiles on the aws command line. It will prompt you for your AWS Access Key ID, AWS Secret Access Key and desired region, so have them ready. You can then switch between the accounts by passing the profile on the command.

How do I change the default profile in AWS CLI?

You just switch to the profile you want (for ex: in Linux use: export AWS_DEFAULT_PROFILE=MyProfile ) and then switch back to the default profile using export AWS_DEFAULT_PROFILE=default .

How do I use a named profile in 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.


Video Answer


2 Answers

For AWS CLI v1, the cleanest solution is:

export AWS_DEFAULT_PROFILE=user2

Afterward, commands like:

aws s3 ls

... are handled from the appropriate account.

For AWS CLI v2, the following will work:

export AWS_PROFILE=user2
like image 79
James Shapiro Avatar answered Oct 19 '22 02:10

James Shapiro


The accepted answer assumes you are using a Linux or Mac terminal. I added commands for Windows, Linux and Mac OS.

Windows

CMD

set AWS_PROFILE=profile_name

Powershell

$env:AWS_PROFILE = 'profile_name'

Linux or Mac

export AWS_PROFILE=profile_name

These will set your aws profile that you will use every time you execute an aws command. But if you just want to switch profile temporarily for one aws command.

aws [command] [sub-command] --profile [profile-name]

like image 11
Mac Ignacio Avatar answered Oct 19 '22 03:10

Mac Ignacio