Skip to content

Instantly share code, notes, and snippets.

@mario21ic
Last active May 15, 2017 20:56
Show Gist options
  • Save mario21ic/73ac781a8b4ad653408a2369552aab7a to your computer and use it in GitHub Desktop.
Save mario21ic/73ac781a8b4ad653408a2369552aab7a to your computer and use it in GitHub Desktop.
Script to execute Terraform with environments and tfvars files
#!/bin/bash
# Usage: ./main.sh action [target] [params]
# ./main.sh plan
# ./main.sh plan -destroy
# ./main.sh plan vpc
# ./main.sh plan vpc -destroy
ACTION=$1
EXTRA=$*
TARGET=""
PARAMS=""
if [ ! -z $2 ]; then
if [[ ! "$2" =~ "-" ]]; then
TARGET="-target=module."$2
PARAMS=${EXTRA#"$ACTION $2"}
else
PARAMS=${EXTRA#"$ACTION"}
fi
fi
TERRAFORM_ENV=$(terraform env list|grep "\*"|awk '{print $2}')
ENV_FILE=$TERRAFORM_ENV".tfvars"
CMD="terraform $ACTION -var-file=$ENV_FILE $TARGET $PARAMS"
#echo "Action: "$ACTION
#echo "Env: "$TERRAFORM_ENV
#echo "tfvars: "$ENV_FILE
echo "Command: "$CMD
$CMD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment