Skip to content

Instantly share code, notes, and snippets.

@mjul
Created July 8, 2016 13:32
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mjul/f93ee7d144c5090e6e3c463f5f312587 to your computer and use it in GitHub Desktop.
Save mjul/f93ee7d144c5090e6e3c463f5f312587 to your computer and use it in GitHub Desktop.
Get environment variables from AWS profile (for use with docker-machine)
#!/bin/sh
# Set the AWS environment variables for an AWS profile
# Useful for docker-machine
#
# Example:
#
# aws_env profile-for-testing
#
# Further information:
# See the AWS CLI `aws configure`
#
echo "# Environment for AWS profile '$1'"
echo export AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id --profile $1)
echo export AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key --profile $1)
echo export AWS_DEFAULT_REGION=$(aws configure get region --profile $1)
echo "# Run this command to configure your shell:"
echo "# eval \$($0 ${@})"
@bencooling
Copy link

Thanks for your script. I adapted to a ~/.zshrc function so it doesn't require a standalone file.

aws_env() {
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);
echo "$1 environment variables exported";
}

@dfevre
Copy link

dfevre commented Feb 7, 2017

When doing an sts.assumerole you get a third key called AWS_SESSION_TOKEN. Some tools refer to this as AWS_SECURITY_TOKEN.

aws_env() {
  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_SESSION_TOKEN=$(aws configure get aws_session_token --profile "$1");
  export AWS_SECURITY_TOKEN=$(aws configure get aws_security_token --profile "$1");
  export AWS_DEFAULT_REGION=$(aws configure get region --profile "$1");
  echo "$1 environment variables exported";
}

@g3kr
Copy link

g3kr commented Mar 27, 2018

@mjul I understand this script runs inside docker container. Where does this pick up the profile from?

@Crusader4Christ
Copy link

Crusader4Christ commented Feb 9, 2022

Enhance a little: "default" profile by default will be used

aws_env() {
  PROFILE="${1:-default}" 
  echo "setup AWS $PROFILE"
  export AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id --profile "$PROFILE");
  export AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key --profile "$PROFILE");
  export AWS_SESSION_TOKEN=$(aws configure get aws_session_token --profile "$PROFILE");
  export AWS_SECURITY_TOKEN=$(aws configure get aws_security_token --profile "$PROFILE");
  export AWS_DEFAULT_REGION=$(aws configure get region --profile "$PROFILE");
  echo "$PROFILE environment variables exported";
}

aws_env === aws_env default

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