Skip to content

Instantly share code, notes, and snippets.

@zoltrain
Created August 12, 2016 15:38
Show Gist options
  • Save zoltrain/71995523678f2643e6f8882d188a4dd1 to your computer and use it in GitHub Desktop.
Save zoltrain/71995523678f2643e6f8882d188a4dd1 to your computer and use it in GitHub Desktop.
aws-env () {
if [[ $# -gt 0 ]]
then
local profile="$1"
shift
elif [[ $# -eq 0 && -f ".aws-profile" ]]
then
local profile="$(cat ".aws-profile")"
fi
if [[ -n "$profile" ]]
then
local profile_exists=$(cat ~/.aws/credentials | grep -c "\[$profile\]" || true)
if [[ -z "$profile_exists" || "$profile_exists" -eq "0" ]]
then
echo "profile $profile doens't exist"
return 1
fi
local access_id=$(aws configure get aws_access_key_id --profile $profile)
local secret_key=$(aws configure get aws_secret_access_key --profile $profile)
local region=$(aws configure get region --profile $profile)
if [[ -z "${region}" ]]
then
local region="eu-west-1"
fi
eval "export AWS_ACCESS_KEY_ID=\"$access_id\""
eval "export AWS_SECRET_ACCESS_KEY=\"$secret_key\""
eval "export AWS_DEFAULT_REGION=\"$region\""
echo "loaded aws environment $profile"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment