Skip to content

Instantly share code, notes, and snippets.

@qoomon
Last active August 13, 2020 08:03
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 qoomon/7e6f751415389a8bd67f05dd0b984d06 to your computer and use it in GitHub Desktop.
Save qoomon/7e6f751415389a8bd67f05dd0b984d06 to your computer and use it in GitHub Desktop.
Deploy CloudFormation Templates including Packaging and Deploy Event Watching
#!/usr/bin/env bash
set -o errexit # exit when a command line fails
set -o pipefail # pipes exit code will be the last non-zero exit code of all pipe commands
set -o nounset # exit on read a undeclared variable
#set -o xtrace # enable debug logging
cd "$(dirname "${BASH_SOURCE[0]}")"
### SOURCE: https://gist.github.com/qoomon/7e6f751415389a8bd67f05dd0b984d06
### REQUIREMENTS #######################################################################################################
#
# Installed AWS CLI
#
# ensure cloudformation-deploy-watcher.sh in same directory as this script
# install from https://gist.github.com/qoomon/d6633abe35eea297f475260478f86c8c
#
### INPUT PARAMETERS ###################################################################################################
SERVICE_NAME="${SERVICE_NAME:-aws-configuration-monitor}"
SERVICE_ENVIRONMENT="${SERVICE_ENVIRONMENT:-develop}"
STACK_TEMPLATE_FILE="${1:-"template.yaml"}"
STACK_TEMPLATE_FILE_PACKAGED="$(dirname "$STACK_TEMPLATE_FILE")/.aws-cloudformation/build/$(basename "$STACK_TEMPLATE_FILE")"
STACK_NAME="${STACK_NAME:-${SERVICE_NAME}}"
read -r -a STACK_PARAMETERS <<<"$(echo "${STACK_PARAMETERS:-}" | tr '\n' ' ')"
STACK_PARAMETERS+=(
"Environment=${SERVICE_ENVIRONMENT}"
)
read -r -a STACK_DEPLOY_CAPABILITIES <<<"$(echo "${STACK_DEPLOY_CAPABILITIES:-}" | tr '\n' ' ')"
read -r -a STACK_TAGS <<<"$(echo "${STACK_TAGS:-}" | tr '\n' ' ')"
STACK_TAGS=(
"Service=${SERVICE_NAME}"
"Environment=${SERVICE_ENVIRONMENT}"
)
export PAGER=cat # disable aws cli pager
########################################################################################################################
echo "--- Caller Identity ---"
echo
aws sts get-caller-identity --query "Arn" --output text
### CLOUDFORMATION PACKAGE #############################################################################################
AWS_ACCOUNT_ID="$(aws sts get-caller-identity --query Account --output text)"
AWS_REGION="${AWS_DEFAULT_REGION:-$(aws configure get region)}"
AWS_CLOUDFORMATION_BUCKET="aws-cloudformation-${AWS_ACCOUNT_ID}-${AWS_REGION}"
#-- ensure cloudformation bucket existence -----------------------------------------------------------------------------
aws s3api head-bucket --bucket "${AWS_CLOUDFORMATION_BUCKET}" 2>/dev/null || (
echo "Create Cloudformation Bucket - ${AWS_CLOUDFORMATION_BUCKET}"
aws s3api create-bucket --bucket "${AWS_CLOUDFORMATION_BUCKET}" \
--create-bucket-configuration "LocationConstraint=${AWS_REGION}"
aws s3api put-bucket-encryption --bucket "${AWS_CLOUDFORMATION_BUCKET}" \
--server-side-encryption-configuration '{"Rules": [{"ApplyServerSideEncryptionByDefault": {"SSEAlgorithm": "AES256"}}]}'
)
#-----------------------------------------------------------------------------------------------------------------------
echo
echo "--- Cloudformation Package ---"
mkdir -p "$(dirname "${STACK_TEMPLATE_FILE_PACKAGED}")"
aws cloudformation package \
--template-file "${STACK_TEMPLATE_FILE}" \
--s3-bucket "${AWS_CLOUDFORMATION_BUCKET}" \
--s3-prefix "${STACK_NAME}" \
--output-template-file "${STACK_TEMPLATE_FILE_PACKAGED}" \
| grep -v -e "Execute the following command to deploy the packaged template" -e "<YOUR STACK NAME>"
####################### #################################################################################################
### CLOUDFORMATION DEPLOY ##############################################################################################
echo
echo "--- Cloudformation Deploy ---"
aws cloudformation deploy \
--stack-name "${STACK_NAME}" \
--template-file "${STACK_TEMPLATE_FILE_PACKAGED}" \
${STACK_PARAMETERS+--parameter-overrides "${STACK_PARAMETERS[@]}"} \
${STACK_TAGS+--tags "${STACK_TAGS[@]}"} \
${STACK_DEPLOY_CAPABILITIES+--capabilities "${STACK_DEPLOY_CAPABILITIES[@]}"} \
--s3-bucket "${AWS_CLOUDFORMATION_BUCKET}" \
--s3-prefix "${STACK_NAME}" \
--no-fail-on-empty-changeset \
| ./cloudformation-deploy-watcher.sh --stack-name "${STACK_NAME}" || (exit "${PIPESTATUS[0]}")
echo
echo "--- Stack Outputs ---"
echo
aws cloudformation describe-stacks \
--stack-name "${STACK_NAME}" \
--query 'Stacks[0].Outputs[*]' \
--output json
########################################################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment