Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Created January 11, 2018 06:22
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 luckylittle/27b9b36ea7f5c362a925d8f7b649659d to your computer and use it in GitHub Desktop.
Save luckylittle/27b9b36ea7f5c362a925d8f7b649659d to your computer and use it in GitHub Desktop.
AWS CLI CloudFormation inject AMI ID to the Parameters file
# 1. Find the latest ID of the AMI:
NEW_AMI_ID=$(aws ec2 describe-images \
--filters Name=name,Values=*WHATEVER AMI NAME YOU ARE LOOKING FOR* \
--query 'Images[*].[ImageId,CreationDate]' \
--output text | sort -k2 -r | head -n1 | awk {'print $1'})
# 2. Modify a PARAMETERS.vars.json that is being used by AWS CLI in step 3.:
tmp=$(mktemp)
jq -r '[ .[] | select(.ParameterKey=="AmiId").ParameterValue |= '\"$NEW_AMI_ID\"' ]' \
PARAMETERS.vars.json > "$tmp" && mv "$tmp" PARAMETERS.vars.json
# 3. Create a stack using the above file with the latest AMI ID:
AWS_PROFILE=test aws cloudformation create-stack \
--stack-name WHATEVER \
--template-body file://WHATEVER.template.yaml \
--tags Key=BLA,Value=BLA \
--parameters file://PARAMETERS.vars.json \
--capabilities CAPABILITY_NAMED_IAM \
--profile ${AWS_PROFILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment