Skip to content

Instantly share code, notes, and snippets.

@ewdurbin
Last active January 17, 2018 21:00
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ewdurbin/5412ba1a2c76308f12a0 to your computer and use it in GitHub Desktop.
Save ewdurbin/5412ba1a2c76308f12a0 to your computer and use it in GitHub Desktop.

aws-cli bash helper functions

The official AWS command line tools, have support for configuration profiles. See Configuring the AWS Command Line Interface - Named Profiles.

Managing multiple profiles with the AWS CLI itself is relatively straight forward, switching between them with --profile flag on the command line or the AWS_PROFILE environment variable.

These helpers extend that functionality for convenience with other tools in the ecosystem.

aws-profile

Configure the current shell with AWS configuration for a given aws-cli profile.

aws-profile <profile_name>

Sets the following Environment Variables

  • AWS_PROFILE=<profile_name> used by aws-cli
  • AWS_DEFAULT_REGION=<profiles_configured_region>
  • AWS_ACCESS_KEY_ID=<profiles_access_key_id>
  • AWS_SECRET_ACCESS_KEY=<profiles_secret_access_key>

with-aws-profile

Run a single command in a subshell configured for a specific aws-cli profile.

with-aws-profile <profile_name> my --arbitrary command

Any command line tool which can understand the environment varibles set by aws-profile should just work.

aws-profile () {
export AWS_PROFILE=${1}
export AWS_DEFAULT_REGION=$(aws configure get region)
export AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id)
export AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key)
}
with-aws-profile () {
(
aws-profile ${1}
shift
${@}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment