Skip to content

Instantly share code, notes, and snippets.

@Loupax
Last active August 21, 2020 15:32
Show Gist options
  • Save Loupax/f3cda37fcdbbc8eda1425af42bc019d2 to your computer and use it in GitHub Desktop.
Save Loupax/f3cda37fcdbbc8eda1425af42bc019d2 to your computer and use it in GitHub Desktop.
A script that locates all the aws parameters from a template and generates a file with the actual values from aws
#!/bin/bash
set -e
if [ $# -ne 2 ] ; then
echo "You need destination pass exactly two arguments destination this command. Source and Destination"
echo "For example ./populate_values.hs values.tmpl.yaml values.yaml"
fi
src=$1
destination=$2
params=$(grep -oP "ap\(/.+?\)" "$src" | cut -d ')' -f 1 | cut -c 4-)
invalid=$(aws ssm get-parameters --name $params --query "InvalidParameters[*]" --output text)
if [ ! -z "$invalid" ]; then
echo "Invalid parameters found inside $src: ${invalid[*]}"
exit 1
fi
cp "$src" "$destination" &&
while read -r parameter; do
key=$(echo "$parameter" | awk '{print "ap("$1")"}' )
value=$(echo "$parameter" | awk '{print $2}')
sed -i "s~$key~$value~g" "$destination"
done < <(aws ssm get-parameters --name $params --query "Parameters[*].{Name:Name,Value:Value}" --output text )
config:
appEnvVars:
# The script will recognise this as a parameter key being surrounded by ap(...),
# fetch it's actual value and replace it in the new file
REG_API_US_DB_HOST: "ap(/terraform-output/rds_endpoint_rw)"
# It also works for multiple keys in the same file
REG_API_US_REDIS_ADDRESS: "ap(/terraform-output/redis_endpoint):6379"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment