#!/usr/bin/env bash # ENV Vars: # TERRAFORM_FILE - defaults to "infra.tf" # - specify state file to pass to terraform export TERRAFORM_FILE="${TERRAFORM_FILE:-infra.tf}" export TERRAFORM_ACTION="${TERRAFORM_ACTION:-apply}" run_terraform() { INOPTS=( "$@" ) if [ -d "${INOPTS[-1]}" ] then DIR="${INOPTS[-1]}" unset 'INOPTS[${#}-1]' else DIR="." fi # Plaintext vault decryption key, not checked into SCM if time terraform init -get=true -get-plugins=true -reconfigure -upgrade -verify-plugins=true "${INOPTS[@]}" "${DIR}"; then time terraform fmt -list=true -check -recursive "${DIR}" fi coderun=$? if [ $coderun ] then time terraform validate "${DIR}" coderun=$? if [ $coderun ] then if [ $TERRAFORM_ACTION == "destroy" ] then time terraform plan -${TERRAFORM_ACTION} -input=false "${INOPTS[@]}" "${DIR}" \ && time terraform ${TERRAFORM_ACTION} -auto-approve -input=false "${INOPTS[@]}" "${DIR}" else rm terraform.tfstate* time terraform plan -input=false "${INOPTS[@]}" "${DIR}" \ && time terraform ${TERRAFORM_ACTION} -auto-approve -input=false "${INOPTS[@]}" "${DIR}" fi return $? fi else return $codefmt fi } run_terraform "$@" retcode=$? exit $retcode