Tag Archives: stackedit

Fish function to switch aws profiles


tags: [fish, aws, stackedit]

Note: I’m sure there are better ways of doing this!

I wanted a convenient way to switch aws command line environment variables based on my desired profile

Below are the environment variables a wanted to swap based on configured profiles

Name Description
AWS_ACCESS_KEY_ID AWS access key.
AWS_SECRET_ACCESS_KEY AWS secret key. Access and secret key variables override credentials stored in credential and config files.
AWS_DEFAULT_REGION AWS region. This variable overrides the default region of the in-use profile, if set.

Creating a profile

You can configure your credentials profiles in ~/.aws/credentials.

[default]
aws_access_key_id=AKIAIOSFODNN7123456890
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCY123456890

[ed]
aws_access_key_id=AKIAI44QH8DHB123456890
aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvb123456890

And your other settings in ~/.aws/config

[default]
output = json
region = us-east-1

[profile ed]
output = json
region = eu-west-1

> Note you can create these by hand or use `aws config –profile’ which is easier

Creating a fish function

The function below is really simple and uses aws config get to set the appropriate environment variable for the profile you have selected

 function aws-profile -- 'Switch aws profile'
     set -gx AWS_ACCESS_KEY_ID (aws configure get --profile $argv aws_access_key_id)
      set -gx AWS_SECRET_ACCESS_KEY (aws configure get --profile $argv aws_secret_access_key)
      set -gx AWS_DEFAULT_REGION (aws configure get --profile $argv region)
      echo Profile switched to $argv
      echo  AWS_ACCESS_KEY_ID $AWS_ACCESS_KEY_ID
      echo  AWS_SECRET_ACCESS_KEY $AWS_SECRET_ACCESS_KEY
      echo  AWS_DEFAULT_REGION $AWS_DEFAULT_REGION
end

Example

$ aws-profile ed

Profile switched to ed
AWS_ACCESS_KEY_ID AKIAI44QH8DHB123456890
AWS_SECRET_ACCESS_KEY je7MtGbClwBF/2Zp9Utk/h3yCo8nvb123456890
AWS_DEFAULT_REGION eu-west-1
Tagged , ,