Created
January 11, 2018 06:22
-
-
Save luckylittle/27b9b36ea7f5c362a925d8f7b649659d to your computer and use it in GitHub Desktop.
AWS CLI CloudFormation inject AMI ID to the Parameters file
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
# 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