Skip to content

Instantly share code, notes, and snippets.

@jpancoast
Last active April 5, 2016 19:04
Show Gist options
  • Save jpancoast/fad1a40d5f7fcc4828100d2eb19cb41e to your computer and use it in GitHub Desktop.
Save jpancoast/fad1a40d5f7fcc4828100d2eb19cb41e to your computer and use it in GitHub Desktop.
Quick shell function to set AWS environment variables using ~/.aws/config

This works fine for me using zsh. I put it in a file called .functions that gets sourced when I login.

Assumptions:

  • you have a AWS credentials file that looks something like this:
[Credentials]
aws_access_key_id = <key_id>
aws_secret_access_key = <secret_key_id>
region = us-east-1

[profile testing-scratch]
aws_access_key_id = <key_id>
aws_secret_access_key = <secret_key_id>
region = us-west-2

[profile testing-dev]
aws_access_key_id = <key_id>
aws_secret_access_key = <secret_key_id>
region = us-west-2
  • aws CLI installed.

The function:

aws-env-vars () {
        unset AWS_ACCESS_KEY_ID
        unset AWS_SECRET_ACCESS_KEY
        unset AWS_DEFAULT_REGION
        unset EC2_REGION
        unset AWS_DEFAULT_PROFILE

        export AWS_ACCESS_KEY_ID=`aws configure get aws_access_key_id --profile $1`
        export AWS_SECRET_ACCESS_KEY=`aws configure get aws_secret_access_key --profile $1`
        export AWS_DEFAULT_REGION=`aws configure get region --profile $1`
        export EC2_REGION=`aws configure get region --profile $1`
        export AWS_DEFAULT_PROFILE=$1
}

To run:

aws-env-vars <profile-name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment