简体   繁体   中英

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 Kube.netes. 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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM