Skip to content

Instantly share code, notes, and snippets.

@diogoko
Last active October 24, 2023 20:21
Show Gist options
  • Save diogoko/c2157763be994f2a13cfb6aeaec956d2 to your computer and use it in GitHub Desktop.
Save diogoko/c2157763be994f2a13cfb6aeaec956d2 to your computer and use it in GitHub Desktop.
Create a local version of a CloudFormation template.yaml with updated SSM Parameter timestamps
#!/bin/bash -eu
PROFILE="your-aws-cli-profile"
STACK_NAME="the-cloduformation-stack-name"
PARAMETER_NAME="the-parameter-name"
aws ssm get-parameter --profile "$PROFILE" --name "$PARAMETER_NAME" > current-parameter.json
CURRENT_TIMESTAMP=$(cat current-parameter.json | python3 -c 'import sys,json,datetime; x = json.load(sys.stdin)["Parameter"]["LastModifiedDate"]; print(int(datetime.datetime.fromisoformat(x).timestamp() * 1000))')
CURRENT_VERSION=$(cat current-parameter.json | python3 -c 'import sys,json; x = json.load(sys.stdin)["Parameter"]["Version"]; print(x)')
aws cloudformation get-template --profile "$PROFILE" --stack-name "$STACK_NAME" --output json --template-stage Processed | python3 -c 'import sys,json; x = json.load(sys.stdin)["TemplateBody"]; json.dump(x, sys.stdout)' > original-template.json
sed -E -e "s#\{\{resolve:ssm:$PARAMETER_NAME:[0-9]+:[0-9]+\}\}#{{resolve:ssm:$PARAMETER_NAME:$CURRENT_VERSION:$CURRENT_TIMESTAMP}}#g" original-template.json > new-template.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment