Skip to content

Instantly share code, notes, and snippets.

@posilva
Last active September 27, 2018 13:18
Show Gist options
  • Save posilva/dea882b38259e594592061f4b95e5788 to your computer and use it in GitHub Desktop.
Save posilva/dea882b38259e594592061f4b95e5788 to your computer and use it in GitHub Desktop.
Helper script to test cloudformation scripts
#!/usr/bin/env bash
# Helper commands for clouformation deploy in the EC2 instances
# curl http://169.254.169.254/latest/user-data
# cat /var/log/cloud-init-output.log
# sudo yum install htop tmux tcpdump telnet -y
# describe stack
# this parameters are not passed by command line parameters
# to ensure that the developer does not hit a different STACK NAME or template by mistake
# using the shell history
# This way we can add this build script to the project add the cf template/name/region
# and run inside the current directory
### MODIFIABLE PARAMETERS ###
ENVIRONMENT=dev
STACK="my-stack-template"
TEMPLATE_FILE=cloudformation.yaml
REGION="eu-central-1"
PROFILE="default"
### STATIC PARAMETERS ####
STACK_NAME="${ENVIRONMENT}-${STACK}"
STACK_PARAMS_FILE=stack-${REGION}.params
STACK_TAGS_FILE=stack.tags
INFO="StackName: '${STACK_NAME}', AWS Profile: '${PROFILE}', AWS Region: '${REGION}'"
LONG_INFO="${INFO}, Template file: '${TEMPLATE_FILE}'"
CF_AWS_CMD="aws --profile ${PROFILE} --region ${REGION} cloudformation"
function create_stack(){
${CF_AWS_CMD} create-stack \
--stack-name ${STACK_NAME} \
--parameters file://./${STACK_PARAMS_FILE} \
--tags file://./${STACK_TAGS_FILE} \
--capabilities CAPABILITY_NAMED_IAM \
--template-body file://${TEMPLATE_FILE}
}
function delete_stack(){
echo "Deleting stack ${INFO}..."
${CF_AWS_CMD} delete-stack \
--stack-name ${STACK_NAME}
}
function describe_stack(){
echo "Describe stack ${LONG_INFO}"
${CF_AWS_CMD} describe-stacks \
--stack-name ${STACK_NAME} | jq '.Stacks[0].Tags' > ${STACK_TAGS_FILE}
${CF_AWS_CMD} describe-stacks \
--stack-name ${STACK_NAME} | jq '.Stacks[0].Parameters' > ${STACK_PARAMS_FILE}
}
function await_delete(){
echo "Waiting for stack ${INFO} deletion ..."
${CF_AWS_CMD} wait stack-delete-complete \
--stack-name ${STACK_NAME} \
--no-paginate
}
function await_create(){
echo "Waiting for stack ${INFO} creation..."
${CF_AWS_CMD} wait stack-create-complete \
--stack-name ${STACK_NAME} \
--no-paginate
}
while getopts cdsr option
do
case "${option}"
in
c) create_stack && await_create ;;
d) delete_stack && await_delete ;;
s) describe_stack ;;
r) delete_stack && await_delete && create_stack && await_create ;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment