Skip to content

Instantly share code, notes, and snippets.

@kkumler
Last active October 25, 2022 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkumler/9752736cd6089cc56ba729e400521085 to your computer and use it in GitHub Desktop.
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
#!/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