jdkealy
jdkealy

Reputation: 4907

How can i switch AWS credentials easily in the terminal?

I have a few different projects I work on. I use AWS and I use Kubernetes. I have a number of AWS credentials stored in my ~/.aws/credentials each with a label like

[account-1]
aws_access_key = x
aws_secret_access_key = y

[account-2]
aws_access_key = x
aws_secret_access_key = y

How can i toggle between them and easily set my config? Currently I type aws configure in the terminal and manually paste the key/secret/regionn every time i want to switch between them.

Upvotes: 1

Views: 3544

Answers (3)

andrew lorien
andrew lorien

Reputation: 2688

If you are using zsh and oh-my-zsh with the aws plugin, you have the asp command.

asp account-1

and if your theme is set up nicely, your commandline prompt will tell you what account you're in.

Upvotes: 1

Amjad Hussain Syed
Amjad Hussain Syed

Reputation: 1040

In addition to @Gunjan answer you can also pass the profile name like this

$ aws ecr get-login-password  --region us-east-1 --profile account-1

If you want to connect to multiple eks clusters

$ aws eks --region us-east-1  update-kubeconfig --name account-1-eks --region eu-west-1 --profile account-1

You need to have proper IAM permissions to run this command

This command will generate a kube config file in ~/.kube move that file to some another location and add alias in your bash_profile or .zshrc like this line

account-1-eks='export KUBECONFIG=:/path/to/the/account-1-eks.config

Now reload your shell and you can switch using the alias like account-1-eks

You can repeat the steps for multiple accounts

Upvotes: 0

Gunjan
Gunjan

Reputation: 1244

When you hit the aws configure command, every time a new profile will be created in the ~/.aws/credentials. You can generate all the required profiles single time and then set environment variable based on the project you're working.

For example, while working on project 1, set the environment variable

export AWS_PROFILE=account-1

and while working on project 2, set the environment variable

export AWS_PROFILE=account-2

Upvotes: 5

Related Questions