Skip to content

Instantly share code, notes, and snippets.

@b3cft
Created September 15, 2017 15:58
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 b3cft/d7cb6676b03f772c8f34798b600b2c28 to your computer and use it in GitHub Desktop.
Save b3cft/d7cb6676b03f772c8f34798b600b2c28 to your computer and use it in GitHub Desktop.
AWS CloudFormation Makefile
.PHONY: help sync update-stack update-all create-stack
.DEFAULT: help
SHELL=bash
ifndef VERBOSE
.SILENT:
endif
DONE = echo ✓ $@ done
ALL = uat1 uat2 uat3 uat4 uat5
AMI=`aws --region=eu-west-1 --profile=mgmt --output=text ec2 describe-images \
--filter "Name=tag:Name,Values=AMI_TAG_OF_CHOICE" --owners YOUR_AWS_ACCOUNT_ID_HERE \
--query 'Images[*].[Name,ImageId]'|sort -n|tail -n 1|awk '{print $$6}'`
help::
echo -e ""
grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[33m%-20s\033[0m %s\n", $$1, $$2}'
echo -e "\nAdditional parameters passed to make"
echo -e "\033[33mENV=<env>\033[0m update a specific environment e.g uat1,uat2"
sync: ## Upload cloudformation templates to S3 ready for updating a stack
aws s3 sync --region eu-west-1 --output text --profile mgmt ./ s3://YOUR_S3_BUCKET_LOCATION_HERE/ --exclude=*~ --exclude=*.swp --exclude=.* --exclude=Makefile
$(DONE)
update-stack: ## Update a specific stack, needs ENV=<env> specifying
if [[ -z "$(ENV)" ]]; then echo "ENV must be specified"; exit 1; fi
aws cloudformation update-stack \
--profile test \
--region eu-west-1 \
--output text \
--stack-name euwest1-$(ENV)-fullstack \
--template-url https://YOUR_S3_BUCKET_LOCATION/fullstack.yaml \
--parameters \
ParameterKey=Environment,UsePreviousValue=true \
ParameterKey=ImageId,ParameterValue=$(AMI) \
--capabilities CAPABILITY_IAM
$(DONE)
update-all: sync ## Update all available environments with latest stack
for env in $(ALL); do $(MAKE) update-stack ENV=$$env; done
$(DONE)
create-stack: sync ## Create a new stack, needs NEWENV=<env> specifiying
if [[ -z "$(NEWENV)" ]]; then echo "NEWENV must be specified"; exit 1; fi
aws cloudformation create-stack \
--profile test \
--region eu-west-1 \
--output text \
--stack-name euwest1-$(NEWENV)-fullstack \
--template-url https://YOUR_S3_BUCKET_LOCATION_HERE/fullstack.yaml \
--parameters \
ParameterKey=Environment,ParameterValue=$(NEWENV) \
ParameterKey=ImageId,ParameterValue=$(AMI) \
--capabilities CAPABILITY_IAM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment