Skip to content

Instantly share code, notes, and snippets.

@Luzifer
Last active August 21, 2021 18:13
Show Gist options
  • Save Luzifer/10382244 to your computer and use it in GitHub Desktop.
Save Luzifer/10382244 to your computer and use it in GitHub Desktop.
Fish-Shell: Switch AWS environments

Fish-Shell: Switch AWS environments

How to use

  1. Set up the folder ~/.awsenv/ with the folder structure shown below
  2. Create a credentials.txt for every environment folder (see an example below)
  3. Ensure no other user than yourself can view those files
  4. Create the function set_aws in ~/.config/fish/functions/set_aws.fish (see an example below)
  5. Now load any environment using set_aws <environment> like set_aws private

File structure

luzifer@knut-workstation01 ~> tree .awsenv/
.awsenv/
├── clear
│   └── credentials.txt
├── company
│   └── credentials.txt
├── private
│   └── credentials.txt
└── setup.fish

3 directories, 4 files
luzifer@knut-workstation01 ~>

Why Fish-Shell?

Just because I'm using fish as my daily shell and want to continue to use it. But I'm quite sure you don't need much time to re-create this for other shells like zsh or bash too.

AWSAccessKeyId=
AWSSecretKey=
AWSRegion=
function set_aws --description 'Set the AWS environment variables' --argument AWS_ENV
source $HOME/.awsenv/setup.fish
end
set -gx AWS_CREDENTIAL_FILE $HOME/.awsenv/$AWS_ENV/credentials.txt
# CFN CLI
set -gx EC2_REGION (grep '^AWSRegion=' $AWS_CREDENTIAL_FILE | cut -d= -f2)
set -gx AWS_ACCESS_KEY (grep '^AWSAccessKeyId=' $AWS_CREDENTIAL_FILE | cut -d= -f2)
set -gx AWS_SECRET_KEY (grep '^AWSSecretKey=' $AWS_CREDENTIAL_FILE | cut -d= -f2)
# AWS CLI
set -gx AWS_DEFAULT_REGION $EC2_REGION
set -gx AWS_ACCESS_KEY_ID $AWS_ACCESS_KEY
set -gx AWS_SECRET_ACCESS_KEY $AWS_SECRET_KEY
@joehillen
Copy link

joehillen commented Feb 8, 2018

I wrote a general purpose fish plugin for this a while back: https://github.com/joehillen/ev-fish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment