Change the default user on the fly on AWS CLI (Amazon Web Services)

On my AWS CLI, I have multiple named users, for different account accesses. As part of testing and clients' accesses, we naturally have multiple user keys entered.

To run a AWS CLI command, I understand that the syntax is

$ aws ec2 describe-instances --profile user2

The --profile option specifies which named user to use.

My question is: if I wanted to do a bunch of functions on a specific bucket that another user (let's say the name is brixwork, but it's the 3rd user in my config file) it's cumbersome to have to add --profile brixwork for each and every line. Is there a way to set the default user for a while without actually having to change the config file to set brixwork as the user?

1 answer

  • answered 2018-01-13 17:16 Rodrigo M

    Just set the AWS_PROFILE variable in your shell environment.

    In Windows:

    set AWS_PROFILE=brixwork

    All CLI commands will pick up that user profile without needing to specify it on the command line.

    I also use a number of different profiles. In interactive mode scripts, it is helpful to echo the current profile before running the entire command, as a way to make sure you do not run the script under the wrong profile:

    echo AWS_PROFILE=$AWS_PROFILE
    pause
    

    The script will confirm the profile it will reference, and I can hit Ctrl-C if I do not have the proper one configured.