Last active
July 3, 2019 15:05
-
-
Save geowa4/dd4614c9037836f5fc506b3aaa0e71a8 to your computer and use it in GitHub Desktop.
Shell script to convert AWS Parameter Store to env vars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set \ | |
-o nounset \ | |
-o pipefail \ | |
-o errexit | |
checkdep() { | |
cmd="$1" | |
if ! which "$1" > /dev/null; | |
then | |
(>&2 echo "$cmd is not installed") | |
exit 127 | |
fi | |
} | |
checkdep aws | |
checkdep jq | |
PREFIX="${1:-/env}" | |
if [ -z "${AWS_DEFAULT_REGION+x}" ] && ! aws configure get region > /dev/null | |
then | |
checkdep curl | |
AWS_DEFAULT_REGION=$( | |
set \ | |
-o nounset \ | |
-o pipefail \ | |
-o errexit | |
curl --max-time 10 --silent 'http://169.254.169.254/latest/dynamic/instance-identity/document' | jq -r '.region' | |
) | |
if ! [ -z "$AWS_DEFAULT_REGION" ] | |
then | |
export AWS_DEFAULT_REGION | |
fi | |
fi | |
aws ssm describe-parameters | \ | |
jq -r ".Parameters[] | select(.Name | startswith(\"$PREFIX\")).Name" | \ | |
xargs -I {} aws ssm get-parameter --name {} --with-decryption | \ | |
jq -r "{Name: .Parameter.Name | sub(\"$PREFIX/\"; \"\"), Value: .Parameter.Value} | @text \"\(.Name)=\(.Value)\"" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment