How to temporarily switch profiles for AWS CLI?

James Shapiro picture James Shapiro · Apr 8, 2018 · Viewed 22k times · Source

Edit: Here's the solution that worked for me:

export AWS_DEFAULT_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.

Answer

James Shapiro picture James Shapiro · Apr 10, 2018

The cleanest solution is:

export AWS_DEFAULT_PROFILE=user2

Afterward, commands like:

aws s3 ls

... are handled from the appropriate account.