Last active
May 15, 2017 20:56
-
-
Save mario21ic/73ac781a8b4ad653408a2369552aab7a to your computer and use it in GitHub Desktop.
Script to execute Terraform with environments and tfvars files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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