Skip to content

Instantly share code, notes, and snippets.

@PeteGoo
Last active February 13, 2023 10:51
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save PeteGoo/ba6374f355327e7f52ddaa500c8e5a31 to your computer and use it in GitHub Desktop.
Save PeteGoo/ba6374f355327e7f52ddaa500c8e5a31 to your computer and use it in GitHub Desktop.
Helpful SSM Parameter Store scripts
#!/bin/bash
# To use just set a variable with SSM_<target_env_var>=<ssm_parameter_store_path>
# e.g. SSM_database_password=prod/myservice/database-password
function get_parameter {
SSM_ENV_VAR_NAME=$1
ENV_VAR_NAME=`echo "$SSM_ENV_VAR_NAME" | cut -c5-`
SSM_PARAM_NAME="${!SSM_ENV_VAR_NAME}"
echo "Getting parameter $SSM_PARAM_NAME from SSM parameter store if it exists and setting into the variable $ENV_VAR_NAME"
SSM_VALUE=`aws ssm get-parameters --with-decryption --names "${SSM_PARAM_NAME}" --query 'Parameters[*].Value' --output text`
#echo "SSM_VALUE = $SSM_VALUE"
COMMAND="export $ENV_VAR_NAME=$SSM_VALUE"
#echo $COMMAND
eval ${COMMAND}
#echo "$ENV_VAR_NAME = ${!ENV_VAR_NAME}"
}
while read name ; do
get_parameter $name
done <<EOT
$(printenv | grep -o '^SSM_[^=]*')
EOT
exec "$@"
@jedis00
Copy link

jedis00 commented Jan 29, 2020

Do you have a variation where it takes a large array of param names and can query 10 at a time (API limit)?

@PeteGoo
Copy link
Author

PeteGoo commented Jan 29, 2020

We switched most of this to get-parameters-by-path [docs] to avoid these kinds of problems.

@imanebosch
Copy link

Not getting this to work. Can you provide an example of usage?

@PeteGoo
Copy link
Author

PeteGoo commented Nov 11, 2021

TBH it's been a while since we used this. I would recommend checking out Chamber, we tend to use it mostly these days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment