Skip to content

Instantly share code, notes, and snippets.

@niklasvincent
Last active October 3, 2018 12:43
Show Gist options
  • Save niklasvincent/1bcbbe22bda68ab5a738 to your computer and use it in GitHub Desktop.
Save niklasvincent/1bcbbe22bda68ab5a738 to your computer and use it in GitHub Desktop.
Validate CloudFormation during build
#!/bin/bash
# Add this script to your cloudformation directory (we have ours in the project root).
# Then set up a build step in Team City that executes the custom script:
#
# [[ -f ./cloudformation/validate-cloudformation.sh ]] && ./cloudformation/validate-cloudformation.sh
#
# That way branches without the script will still build.
#
# Make sure your CI user has the following policy attached to it for AWS:
# {
# "Version": "2012-10-17",
# "Statement": [
# {
# "Effect": "Allow",
# "Action": [
# "cloudformation:ValidateTemplate"
# ],
# "Resource": "*"
# }
# ]
#}
#
CLOUDFORMATION_DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
which aws 1>/dev/null 2>&1
if [[ $? -gt 0 ]]; then
echo "AWS CLI not installed. Cannot validate Cloudformation Templates."
exit 1
fi
if [[ -d "${CLOUDFORMATION_DIRECTORY}" ]]; then
for TEMPLATE in $(find ${CLOUDFORMATION_DIRECTORY} -iname "*.json"); do
echo -n "Validating CloudFormation template ${TEMPLATE}..."
aws cloudformation validate-template --template-body "file://${TEMPLATE}" 1>/dev/null
if [[ $? -gt 0 ]]; then
exit 1
fi
echo " OK"
done
fi
@jamespamplin
Copy link

Nice one @nlindblad!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment