Skip to content

Instantly share code, notes, and snippets.

@mario21ic
Last active January 16, 2018 22:33
Show Gist options
  • Save mario21ic/be97d051ee93cb9937f5f05aa958e1fc to your computer and use it in GitHub Desktop.
Save mario21ic/be97d051ee93cb9937f5f05aa958e1fc to your computer and use it in GitHub Desktop.
Script to work with terraform and workspaces inside of a docker container
#!/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=$*
if [ $ACTION == "plan" ] || [ $ACTION == "apply" ] || [ $ACTION == "destroy" ]; then
TERRAFORM_ENV=$(terraform workspace list|grep "\*"|awk '{print $2}')
ENV_FILE=$TERRAFORM_ENV".tfvars"
if [ ! -f $ENV_FILE ]; then
echo "File $ENV_FILE not found!" >&2
exit
fi
TARGET=""
PARAMS=""
if [ ! -z $2 ]; then
if [[ ! "$2" =~ "-" ]]; then
TARGET="-target=module."$2
PARAMS=${EXTRA#"$ACTION $2"}
else
PARAMS=${EXTRA#"$ACTION"}
fi
fi
CMD="docker run --rm -i \
-v $PWD/:/app \
-v $HOME/.aws/:/root/.aws/ \
code.yarkan.com/terraform:latest \
$ACTION -var-file=$ENV_FILE $TARGET $PARAMS"
#echo "Action: "$ACTION
#echo "Env: "$TERRAFORM_ENV
#echo "tfvars: "$ENV_FILE
else
CMD="docker run --rm -i \
-v $PWD/:/app \
-v $HOME/.aws/:/root/.aws/ \
code.yarkan.com/terraform:latest \
$EXTRA"
fi
echo "Command: "$CMD
$CMD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment