Created
May 20, 2022 17:44
-
-
Save haversnail/d446b0e7deddda3485d3f90cdc333361 to your computer and use it in GitHub Desktop.
A bash script for renaming AWS SSM Parameter Store parameters.
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
#!/bin/bash | |
OLD="$1"; shift | |
NEW="$1"; shift | |
RESPONSE=$(eval "aws ssm describe-parameters") | |
# Get the parameter descriptions (since they aren't included in `get-parameters-by-path`): | |
PARAMS=$(echo $RESPONSE | jq -c --arg path $OLD '.Parameters | map({Name, Description, Type}) | .[] | select(.Name | startswith($path))') | |
# Replace the separator used by the `for` loop with only the newline character | |
# to prevent spaces in the param description strings from breaking the loop: | |
_IFS=$IFS | |
IFS=$'\n' | |
for PARAM in $PARAMS; do | |
NAME=$(echo "$PARAM" | jq -r '.Name') | |
VALUE=$(eval "aws ssm get-parameter --name=$NAME --with-decryption" | jq -r '.Parameter | .Value') | |
PAYLOAD=$(echo "$PARAM" | jq -c --arg value $VALUE '. + {Value: $value}') | |
aws ssm put-parameter --cli-input-json ${PAYLOAD//$OLD/$NEW} | |
# Delete the old parameter: | |
aws ssm delete-parameter --name=$NAME | |
done | |
# Restore the original value: | |
IFS=$_IFS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment