95

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.

James Shapiro
  • 4,805
  • 3
  • 31
  • 46

9 Answers9

111

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
James Shapiro
  • 4,805
  • 3
  • 31
  • 46
  • 1
    Per Diego's comment below, this variable name has been renamed to `AWS_PROFILE`. [More here](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html#envvars-list). – emilebaizel Dec 21 '20 at 21:38
  • 1
    @emilebaizel actually "export AWS_PROFILE=[profile_name]" has never worked for me or for many others, hence the popularity of this question. – James Shapiro Dec 21 '20 at 22:25
  • does this still fail for you with the latest AWS CLI? – emilebaizel Dec 23 '20 at 17:18
  • 1
    @emilebaizel "export AWS_PROFILE=user2" does work in AWS CLI v2 – James Shapiro Jul 10 '21 at 16:49
  • 1
    For anyone interested here is a [link](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html#:~:text=output%3Dtext-,Using%20named%20profiles,-To%20use%20a) to official AWS documentation on this. – rsmets Sep 15 '22 at 22:59
24

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]

Mac Ignacio
  • 655
  • 7
  • 5
  • 1
    The windows answer here is for cmd. For powershell it is `$env:AWS_PROFILE = 'profile_name'` – DollarAkshay Dec 31 '21 at 14:52
  • This doesn't work. If I run aws configure list, I get access_key ****************BMSB shared-credentials-file secret_key ****************JStS shared-credentials-file then if I run set AWS_PROFILE=scoop_production and re-run aws configure list I get the same details, which aren't the correct details for scoop_production. – Nick Wright Jul 21 '23 at 13:08
9

You can see how it works doing this

$ export AWS_PROFILE=myprofile
$ aws s3 ls --debug 2>&1 | grep profile
2018-04-08 19:19:17,990 - MainThread - botocore.session - DEBUG - Loading variable profile from environment with value 'myprofile'.

I doubt this works differently for you.

You can also verify that

$ AWS_PROFILE=myprofile aws s3 ls --debug 2>&1 | grep profile

and

$ aws s3 ls --profile myprofile --debug 2>&1 | grep profile

all give the same result.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • These commands do NOT yield the same result for me. The first two do not produce any output. The final one produces the following output: "2018-04-08 23:11:33,683 - MainThread - awscli.clidriver - DEBUG - Arguments entered to CLI: ['s3', 'ls', '--profile', 'myprofile', '--debug'] 2018-04-08 23:11:33,683 - MainThread - botocore.credentials - DEBUG - Skipping environment variable credential check because profile name was explicitly set. – James Shapiro Apr 09 '18 at 06:12
  • @JamesShapiro remove `| grep profile` from the first commands and see what's there – Diego Torres Milano Apr 09 '18 at 15:26
6

create or edit this file:

% vim ~/.aws/credentials

list as many key pairs as you like:

[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

include --profile user1 to select a profile & do what you like:

aws s3api list-buckets --profile user1
# any aws cli command now using user1 pair of keys

.... OR ....

set a local variable to select the pair of keys you want to use:

% export AWS_PROFILE=user1

then do what you like:

aws s3api list-buckets  # any aws cli command now using user1 pair of keys

more details: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html

ox.
  • 3,579
  • 1
  • 21
  • 21
  • That option is listed as suboptimal in the original problem description. It's not a good one, because it involves appending "--profile [user]" to every single CLI command that you run. – James Shapiro Dec 12 '21 at 00:53
  • I would suggest deleting, there is nothing in your answer that is not explicitly stated in the answers above. – James Shapiro Dec 12 '21 at 03:36
2
user@machine:~/.aws$ aws --version
aws-cli/2.1.2 Python/3.7.3 Linux/5.4.0-53-generic exe/x86_64.linuxmint.20

I add aliases to my .bashrc if I have a lot of named profiles.

for example:

alias harry-tuttle='export AWS_PROFILE=harry-tuttle'

Then switching profiles becomes one command with less typing.

To see all your profiles:

aws configure list-profiles`
agentsmith
  • 1,226
  • 1
  • 14
  • 27
2

You could add the profile flag

aws s3 ls --profile marketingadmin
hestellezg
  • 3,309
  • 3
  • 33
  • 37
1

AWS cli has 3 level of ways it will read variables

  • environment variables of key_id / key_secret
  • profile via cred/config (normally in ~/.aws/cre...)
  • manual value provided inline

see: https://docs.aws.amazon.com/cli/latest/topic/config-vars.html#credentials

one way will be overwritten by another. based on OP, it might be that although DEFAULT_PROFILE is set as userX, the AWS_ACCESS_KEY_ID and/or AWS_SECRET_ACCESS_KEY environment variables is set to something else.

You can do an alias to a shell function that load credentials to the current environment thru the use of

"export AWS_ACCESS_KEY_ID=XXXXXXX;"... and more

or to be safer load via a secrets manager

"export AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id --profile XXXX)"... and more

Export all access key/secrets etc and then check that the right credentials are loaded in memory thru

aws configure list

finally.. do a reset of the the variable to "default" .. as a good habit to ensure you do what you need as the AWS role; especially when using multiple profiles. hope this helps.

mirageglobe
  • 2,446
  • 2
  • 24
  • 30
1

For windows use

set AWS_DEFAULT_PROFILE=user2
turbopasi
  • 3,327
  • 2
  • 16
  • 43
  • Hi and welcome to SO ! Please read [How to write a good answer](https://stackoverflow.com/help/how-to-answer). Try to include what your answer does better and/or differently compared to the already accepted one! – turbopasi Jan 26 '21 at 13:11
1

I want to specify what's the case for windows users -

  1. Use set for setting environment variable for the current terminal session.

    set AWS_PROFILE=some_profile_name
    
  2. Use setx for setting environment variable that stays even if the terminal is closed.

    setx AWS_PROFILE=some_profile_name
    

One thing to keep in mind is that while using setx, the environment variable gets set in the new terminal session. The existing session will not yield the set environment variable value.

Fedor
  • 17,146
  • 13
  • 40
  • 131
Prerak Jain
  • 13
  • 1
  • 5