Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple AWS accounts from the command line?

I've got two different apps that I am hosting (well the second one is about to go up) on Amazon EC2.

How can I work with both accounts at the command line (Mac OS X) but keep the EC2 keys & certificates separate? Do I need to change my environment variables before each ec2-* command?

Would using an alias and having it to the setting of the environment in-line work? Something like:

alias ec2-describe-instances1 = export EC2_PRIVATE_KEY=/path; ec2-describe-instances
like image 879
Matt Culbreth Avatar asked Feb 27 '09 02:02

Matt Culbreth


People also ask

How can you access multiple accounts by 1 CLI?

You have to generate an Access Key Id and Secret Access Key for both of your accounts. You can do that by clicking on your user in the IAM console. In the Security credentials tab click on Create access key and save both files.

How do I switch between multiple users in AWS command line?

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.

Which AWS service allows you to control multiple AWS services from the command line?

The AWS Command Line Interface (AWS CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.


8 Answers

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.

Examples:

$ aws configure --profile account1
$ aws configure --profile account2

You can then switch between the accounts by passing the profile on the command.

$ aws dynamodb list-tables --profile account1
$ aws s3 ls --profile account2

Note:

If you name the profile to be default it will become default profile i.e. when no --profile param in the command.


More on default profile

If you spend more time using account1, you can make it the default by setting the AWS_DEFAULT_PROFILE environment variable. When the default environment variable is set, you do not need to specify the profile on each command.

Linux, OS X Example:

$ export AWS_DEFAULT_PROFILE=account1
$ aws dynamodb list-tables

Windows Example:

$ set AWS_DEFAULT_PROFILE=account1
$ aws s3 ls
like image 70
iBrianCox Avatar answered Oct 01 '22 23:10

iBrianCox


How to set "manually" multiple AWS accounts ?

1) Get access - key

AWS Console > Identity and Access Management (IAM) > Your Security Credentials > Access Keys

2) Set access - file and content

~/.aws/credentials

[default]
aws_access_key_id={{aws_access_key_id}}
aws_secret_access_key={{aws_secret_access_key}}

[{{profile_name}}]
aws_access_key_id={{aws_access_key_id}}
aws_secret_access_key={{aws_secret_access_key}}

3) Set profile - file and content

~/.aws/config

[default]
region={{region}}
output={{output:"json||text"}}

[profile {{profile_name}}]
region={{region}}
output={{output:"json||text"}}

4) Run - file with params

Install command-line app - and use AWS Command Line it, for example for product AWS EC2

aws ec2 describe-instances -- default

aws ec2 describe-instances --profile {{profile_name}} -- [{{profile_name}}]


Ref

  • https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html
like image 43
BGBRUNO Avatar answered Sep 30 '22 23:09

BGBRUNO


You should be able to use the following command-options in lieu of the EC2_PRIVATE_KEY (and even EC2_CERT) environment variables:

  • -K <private key>
  • -C <certificate>

You can put these inside aliases, e.g.

alias ec2-describe-instances1 ec2-describe-instances -K /path/to/key.pem
like image 29
vladr Avatar answered Oct 02 '22 23:10

vladr


IMHO, the easiest way is to edit .aws/credentials and .aws/config files manually.

It's easy and it works for Linux, Mac and Windows. Just read this for more detail (1 minute read).

.aws/credentials file:

[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

.aws/config file:

[default]
region=us-west-2
output=json

[profile user1]    <-- 'profile' in front of 'profile_name' (not for default)!!
region=us-east-1
output=text
like image 29
oz19 Avatar answered Oct 03 '22 23:10

oz19


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

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

% export AWS_PROFILE=user1

do what you like:

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

you can also do it command by command if you would rather by including --profile user1 with each command:

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

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

like image 41
ox. Avatar answered Oct 04 '22 23:10

ox.


The new aws tools now support multiple profiles.

If you configure access with the tools, it automatically creates a default in ~/.aws/config.

You can then add additional profiles - more details at:

http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-multiple-profiles

like image 28
chris Avatar answered Sep 30 '22 23:09

chris


I created a simple tool, aaws, to switch between AWS accounts.

It works by setting the AWS_DEFAULT_PROFILE in your shell. Just make sure you have some entries in your ~/.aws/credentials file and it will easily switch between multiple accounts.

/tmp
$ aws s3 ls
Unable to locate credentials. You can configure credentials by running "aws configure".
/tmp
$ aaws luk3

[luk3] 🔐 /tmp
$ aws s3 ls
2013-11-05 21:40:04 luk3thomas.com
like image 42
luk3thomas Avatar answered Sep 30 '22 23:09

luk3thomas


I wrote a toolkit to switch default AWS profile.

The mechanism is physically moving the profile key to the default section in config and credentials files.

The better solution today should be one of the following ways:

  • Use aws command option --profile.
  • Use environment variable AWS_PROFILE.

I don't remember why I didn't use the solution of --profile, maybe I was not realized its existence.

However the toolkit can still be useful by doing other things. I'll add a soft switch flag by using the way of AWS_PROFILE in the future.

$ xsh list aws/cfg
[functions] aws/cfg/move
[functions] aws/cfg/set
[functions] aws/cfg/activate
[functions] aws/cfg/get
[functions] aws/cfg/delete
[functions] aws/cfg/list
[functions] aws/cfg/copy

Repo: https://github.com/xsh-lib/aws

Install:

curl -s https://raw.githubusercontent.com/alexzhangs/xsh/master/boot | bash && . ~/.xshrc
xsh load xsh-lib/aws

Usage:

xsh aws/cfg/list
xsh aws/cfg/activate <profilename>
like image 1
alex Avatar answered Oct 02 '22 23:10

alex