Last active
October 25, 2022 17:09
-
-
Save kkumler/9752736cd6089cc56ba729e400521085 to your computer and use it in GitHub Desktop.
Get a set of parameters from Systems Manager Parameter Store and format them for use as environment variables
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 errexit | |
set -o nounset | |
set -o pipefail | |
# As is the classic warning, this is so far light on error handling. | |
# Get a set of parameters from Systems Manager Parameter Store and format them for use as environment variables | |
# Usage: ./get-parameters.sh [--export] prefix [prefix...] | |
# Options: | |
# --export Prefix each line with export command | |
function get_params_by_prefix { | |
# PARAMS needs to reflect tab-separated columns, which should be the case. Beware of that for test data explicitly added here | |
# Each item in the list should be of the format "/path/to/parameter value" | |
PARAMS=$(aws ssm get-parameters-by-path --recursive --query 'Parameters[*].[Name, Value]' --output text --with-decryption --path "$1") | |
for line in "${PARAMS[@]}"; do | |
while IFS=$'\t' read -r name value; do | |
short="${name##*/}" # Get just the last part of the name | |
short="${short//-/_}" # Convert dashes to underscores | |
echo "${EXPORT}${short^^}=${value}" # output, uppercase for the key name | |
done <<< "${line}" | |
done | |
} | |
EXPORT="" | |
if [[ $1 == "--export" ]]; then | |
EXPORT="export " | |
shift | |
fi | |
while [ $# -gt 0 ]; do | |
get_params_by_prefix "$1" | |
shift | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment