Skip to content

Instantly share code, notes, and snippets.

@mccutchen
Last active May 4, 2020 18:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mccutchen/d7902e2c8cd414cdd2b0 to your computer and use it in GitHub Desktop.
Save mccutchen/d7902e2c8cd414cdd2b0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# When eval'd, the output from script will export the AWS_ACCESS_KEY_ID and
# AWS_SECRET_ACCESS_KEY credentials from the a specific profile in
# ~/.aws/credentials.
#
# Usage:
#
# export-aws-credentials [PROFILE]
#
# If PROFILE is not given, the "default" profile will be exported. The
# output of this script is intended to be fed into `eval`.
#
# Examples:
#
# eval $(export-aws-credentials)
# eval $(export-aws-credentials other-profile)
[[ -z "$1" ]] && profile="default" || profile="$1"
credentials=$(grep -A 2 "$profile" ~/.aws/credentials | grep -v "$profile")
if [[ "$credentials" == "" ]]; then
echo "Error: profile '$profile' not found in ~/.aws/credentials" >&2
exit 1
fi
echo "Exporting profile: $profile" >&2
for key in AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY; do
val=$(echo "$credentials" | grep -i $key | tr -d '[:space:]' | awk -F '=' '{print $2}')
if [[ "$val" == "" ]]; then
echo " Error: missing $key" >&2
exit 1
fi
echo "export $key='$val'"
done
@kaustubh-padegaonkar-sp

if you add a tr -d '[:space]' after the grep in :30, that would be also helpful since its the case sometimes that creds file has a space before and after =.

@mccutchen
Copy link
Author

@kaustubh-padegaonkar-sp good call, updated!

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