Skip to content.

Terraform init-validate-plan-apply Bash function

Tired of typing Terraform init, validate, apply? I’ve written this bash function to speed this up and implement some good practices.

Features #

  • Directory argument is passed to -chdir= before running Terraform
    • Example: tf-iva my-terraform/v1
  • All invocations generate a Terraform plan which is saved to $TMPDIR/terraform/<UUID>.tfplan
    • Set TF_PLANS to save your plans to a specific directory
    • UUIDs are randomly generated using /proc/sys/kernel/random/uuid, if your platform does not provide this feature then you will need to subsitute it
  • User is prompted whether to apply the plan, plans are saved prior to this prompt
  • -h/--help arguments prints usage information
# tf-iva - init, validate, plan, apply
function tf-iva {
    if [[ "${1}" =~ ^(-h|--help)$ ]]; then
		cat <<-EOT
			tf-iva - Terraform init, validate, plan, apply by Joe Heaton.dev

			Usage: [TF_PLANS=DIR] tf-iva [chdir]

		Default destiantion for tfplans is \$TMPDIR/terraform
		EOT
        return
    fi

    # Use argument as dir
    [ "${1}" ] && DIR="${1}" || DIR=""

    # Set tfplan destination
    if [ "${TF_PLANS}" ]; then
        # Override DEST if $TF_PLANS set
        DEST="${TF_PLANS}"
    else
        # Set TMPDIR if missing
        DEST="${TMPDIR:-/tmp}/terraform"
    fi

    # Make temp directory for terraform
    [ ! -d "${DEST}" ] && mkdir ${DEST} >/dev/null

    # Get random UUID for our plan name
    UUID="$(< /proc/sys/kernel/random/uuid )"
    
    echo
    echo "> terraform ${DIR:+-chdir=${DIR}} init"
    terraform ${DIR:+-chdir=${DIR}} init
    [ ! "$?" = 0 ] && { echo "Error: Terraform init failed."; return 1; }

    echo
    echo "> terraform ${DIR:+-chdir=${DIR}} validate"
    terraform ${DIR:+-chdir=${DIR}} validate
    [ ! "$?" = 0 ] && { echo "Error: Terraform validate failed."; return 1; }

    echo
    echo "> terraform ${DIR:+-chdir=${DIR}} plan -out ${DEST}/${UUID}.tfplan"
    terraform ${DIR:+-chdir=${DIR}} plan -out ${DEST}/${UUID}.tfplan
    [ ! "$?" = 0 ] && { echo "Error: Terraform plan failed."; return 1; }

    echo
    read -n1 -p "Apply this plan? [Y/n]: " yn
    echo
    case $yn in
        [nN]*) echo "Aborting.."; return;;
        [yY]*)
            echo "> terraform ${DIR:+-chdir=${DIR}} apply ${DEST}/${UUID}.tfplan"
            terraform ${DIR:+-chdir=${DIR}} apply ${DEST}/${UUID}.tfplan
            ;;
        "")
            echo "> terraform ${DIR:+-chdir=${DIR}} apply ${DEST}/${UUID}.tfplan"
            terraform ${DIR:+-chdir=${DIR}} apply ${DEST}/${UUID}.tfplan
            ;;
        *) echo "Aborting.."; return;;
    esac 
}

Download tf-iva.sh

avatar
Joe Heaton
Cloud HPC Engineer

Joe Heaton’s technology-focused murmurings.
comments powered by Disqus

Settings

Reveal the cookie consent prompt

Use Dyslexia-friendly font OpenDyslexia

Toggle advertisements