Skip to content

Instantly share code, notes, and snippets.

@Pierozi
Created January 15, 2018 12:36
Show Gist options
  • Select an option

  • Save Pierozi/901b7019cb0e2eda16acb81346a46a15 to your computer and use it in GitHub Desktop.

Select an option

Save Pierozi/901b7019cb0e2eda16acb81346a46a15 to your computer and use it in GitHub Desktop.
AWS Cloudformation check and upload to S3
#!/bin/bash -
#title :cloudformation
#description :handle cloudformation command
#author :© 2016 Continuous SA - Tomasina Pierre
#date :20170202
#version :0.1
#usage :bash cloudformation [OPTIONS] action
#notes :Requirement : aws-cli
#==============================================================================
GREEN="\\033[1;32m"
NORMAL="\\033[0;39m"
RED="\\033[1;31m"
PINK="\\033[1;35m"
BLUE="\\033[1;34m"
WHITE="\\033[0;02m"
YELLOW="\\033[1;33m"
CYAN="\\033[1;36m"
# function for help output
usage() {
echo -e "DESCRIPTION:\n\t $(basename "$0") - manage cloudformation stack $NORMAL"
echo -e "USAGE:\n\t [OPTIONS] ACTION\n
\033[1mOPTIONS\033[0m
-h\t Displays this message.
-d\t directory must be deployed
-p\t AWS Profile (Optional)
-b\t bucket where upload files
\033[1mACTIONS\033[0m
check\t Run the validate-template command on directory selected
upload\t Upload to S3 all stacks template
"
}
#Function for error handling
bail ()
{
echo -e "${RED}Error code $1${NORMAL}"
exit 1
}
directory="./"
######
## Options
######
while getopts "hd:p:b:" opt; do
case $opt in
h) usage && exit 0 ;;
d) directory="$OPTARG" ;;
p) profile="$OPTARG" ;;
b) bucket="$OPTARG" ;;
\?) echo -e "${RED}Invalid option: -$OPTARG${NORMAL}" >&2 ; bail 1 ;;
:) echo "Option -$OPTARG requires an argument." >&2 ; bail 2
esac
done
shift $((OPTIND-1))
if [ ! -d "$directory" ]
then
echo -e "${RED}directory option are not valid directory${NORMAL}"
bail 1
fi
######
## Actions
######
if [ "" == "$1" ]
then
usage && exit 1
fi
if [ "check" == "$1" ]
then
for file in `find "$directory" -type f -name '*.template' -o -name '*.yml' -o -name '*.json'`
do
if [ -z "$profile" ]
then
aws cloudformation validate-template --template-body file://$file
else
aws --profile $profile cloudformation validate-template --template-body file://$file
fi
if [ ! $? -eq 0 ]
then
echo -e "${RED}Validate template fail for $file"
exit 255
fi
done
fi
if [ "upload" == "$1" ]
then
if [ -z "$bucket" ]
then
echo -e "${RED}the bucket parameter is required for deploy the stack on S3${NORMAL}"
bail 1
fi
cd $directory
for file in `find . -type f -name '*.template' -o -name '*.yml' -o -name '*.json' -o -name '*.sh' -o -name '*.rules'`
do
file=${file:2}
if [ -z "$profile" ]
then
aws s3 cp $file s3://$bucket/$file
else
aws --profile $profile s3 cp $file s3://$bucket/$file
fi
# aws --profile $profile cloudformation update-stack \
# --stack-name XXXX \
# --capabilities CAPABILITY_IAM \
# --use-previous-template \
# --parameters \
# ParameterKey=EnvType,ParameterValue=staging \
# ParameterKey=KeyName,ParameterValue=myKeyPair
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment