Skip to content

Instantly share code, notes, and snippets.

@epic9x
Created March 13, 2017 23:38
Show Gist options
  • Save epic9x/b14c6f07bb18292ce430c6a8b6e6e9e8 to your computer and use it in GitHub Desktop.
Save epic9x/b14c6f07bb18292ce430c6a8b6e6e9e8 to your computer and use it in GitHub Desktop.
#!/bin/bash
# WTG
# Simple script
#
# Todo:
# - add check for TF version
RUN_TIME=$(/bin/date "+%Y-%m-%d-%H:%M:%S")
export TF_LOG=TRACE
export TF_LOG_PATH=./tf_debug_logs/terraform_debug-${RUN_TIME}.log
if [ ! -e ./tf_debug_logs/ ]
then
echo "Creating tf_debug_logs directory to hold terraform debugging logs"
mkdir tf_debug_logs
fi
if [ ! -e /usr/local/bin/terraform ]
then
echo "Missing terraform binary: You should 'brew install terraform' to get it."
exit 1
fi
if [ -z "$AWS_PROFILE" ]; then
echo "Please set the AWS_PROFILE environment variable to the name of the profile configured via 'aws configure'"
exit 1
fi
AWS_CLI_BIN=$(which aws)
if [ -z $AWS_CLI_BIN ]
then
echo "Missing aws cli: You should 'brew install awscli' to get it."
exit 1
fi
if [ ! -e terraform.tfvars ];
then
echo "Missing a terraform.tfvars file: Edit and move terraform.tfvars.example"
exit 1
fi
if [ "$1" == "apply" ];
then
echo "#####################################################"
echo "# Beginning Terraform "
echo "# Do not hit control-c or attempt to abort the run or your account will be left in an unknown state"
echo "#####################################################"
echo "Updating terraform modules"
terraform get --update
terraform apply
elif [ "$1" == "destroy" ] ;
then
terraform destroy --force
elif [ "$1" == "plan" ] ;
then
terraform get --update
terraform plan
elif [ "$1" == "output" ] ;
then
terraform output |
awk -F= 'BEGIN {}
/{/ {LEADING_WHITESPACE=" "}
/}/ {LEADING_WHITESPACE="" }
(! /=/ || $2 == "{" ) { print }
(/=/ && $2 != "{" ) { gsub(/ */,"",$2);printf("%s%s= \"%s\"\n",LEADING_WHITESPACE,$1,$NF) }
END {}'
elif [ "$1" == "help" ]
then
echo "Executing as terraform $@"
scriptname=$(basename $0)
echo "Usage:"
echo "$scriptname plan # show the projected impact of your terraform apply"
echo "$scriptname apply # apply your terraform to realize or updating defined resources"
echo "$scriptname destroy # destroy your cluster - does /not/ ask for permission!"
echo "$scriptname output # show IP addresses and other outputs defined for your resources "
echo "$scriptname help # usage - you're looking at it."
echo "$scriptname 'x' # passes through any arguments to terraform"
else
echo "Executing terraform $@"
terraform $@
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment