Skip to content

Instantly share code, notes, and snippets.

@antklim
Last active August 29, 2022 20:43
Show Gist options
  • Save antklim/3fc9908ddbd0d1bc8b058148a6c7b2fd to your computer and use it in GitHub Desktop.
Save antklim/3fc9908ddbd0d1bc8b058148a6c7b2fd to your computer and use it in GitHub Desktop.
#!/bin/bash
################################################################################
##
## The following creates AWS resources for the project
##
################################################################################
. .env # populate parameters from .env file
REGION=${REGION:-"ap-southeast-2"}
DISTRIBUTION_ENABLED=${DISTRIBUTION_ENABLED:-"false"}
if [ -z $PROJECT ] ; then
echo "error: PROJECT required"
exit 1
fi
# ... the other params verification
stack_name=$PROJECT
stack_output_file=$stack_name-output.txt
stacks=$(aws cloudformation describe-stacks \
--query "Stacks[?StackName=='$stack_name']" \
--region $REGION \
--output json | jq '. | length')
if [ $stacks -eq 0 ] ; then
echo "Creating resources for $PROJECT project ..."
aws cloudformation create-stack --stack-name $stack_name \
--template-body file://main.yml \
--parameters ParameterKey=ProjectName,ParameterValue=$PROJECT \
ParameterKey=TableName,ParameterValue=$TABLE \
# ... the other params
--tags Key=project,Value=$PROJECT \
--region $REGION \
--capabilities CAPABILITY_NAMED_IAM \
--output text > $stack_output_file
status=$?
if [ $status -ne 0 ] ; then
echo "error: failed to initiate stack $stack_name"
exit 1
fi
echo "Waiting for resources creation completion ..."
aws cloudformation wait stack-create-complete --stack-name $stack_name \
--region $REGION
status=$?
if [ $status -ne 0 ] ; then
echo "error: failed to create stack $stack_name"
exit 1
fi
echo "$PROJECT resources successfully created."
else
echo "Updating resources for $PROJECT project ..."
aws cloudformation update-stack --stack-name $stack_name \
--template-body file://main.yml \
--parameters ParameterKey=ProjectName,UsePreviousValue=true \
ParameterKey=TableName,UsePreviousValue=true \
# ... the other params
--region $REGION \
--capabilities CAPABILITY_NAMED_IAM \
--output text > $stack_output_file
status=$?
if [ $status -ne 0 ] ; then
echo "error: failed to initiate stack update $stack_name"
exit 1
fi
echo "Waiting for resources update completion ..."
aws cloudformation wait stack-update-complete --stack-name $stack_name \
--region $REGION
status=$?
if [ $status -ne 0 ] ; then
echo "error: failed to update stack $stack_name"
exit 1
fi
echo "$PROJECT resources successfully updated."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment