Skip to content

Instantly share code, notes, and snippets.

@busla
Created August 5, 2023 23:24
Show Gist options
  • Select an option

  • Save busla/b97c2dbc393a73c5a337e5b89785f6c1 to your computer and use it in GitHub Desktop.

Select an option

Save busla/b97c2dbc393a73c5a337e5b89785f6c1 to your computer and use it in GitHub Desktop.

Revisions

  1. busla created this gist Aug 5, 2023.
    50 changes: 50 additions & 0 deletions tf.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    #!/bin/bash

    set -eoux pipefail

    PROJECT_DIR="$(git rev-parse --show-toplevel)"
    TF_ROOT="${PROJECT_DIR}/terraform"

    function info() {
    local msg
    msg="${1}"
    echo "[INFO]: ${msg}"
    }

    function tf_action() {
    local action env
    action="${1}"
    # shellcheck disable=SC2124
    env="${@: -1}" # the last argument is env
    shift 1 # remove first argument (action)
    local extra_args=("${@:1:$#-1}") # get extra arguments, excluding the last one (env)

    # Build up the args array with the action, extra args, optional backend-config, and var-files
    local args=("${action}")
    if [ "${extra_args[@]}" ]; then
    args+=("${extra_args[@]}")
    fi
    if [ "${action}" == "init" ]; then
    args+=("-backend-config=${TF_ROOT}/backend/${env}.tfbackend")
    fi
    args+=(
    "-var-file=${TF_ROOT}/terraform.tfvars"
    "-var-file=${TF_ROOT}/vars/${env}.tfvars"
    )

    info "Running Terraform ${action} ..."
    terraform -chdir="${TF_ROOT}" "${args[@]}"

    if [ "${action}" == "init" ]; then
    info "Running Terraform validate ..."
    terraform validate
    fi
    }

    option=$1
    case $option in
    init|plan|apply|show) tf_action "${@}";;
    *)
    echo "Invalid option: -${option}."
    ;;
    esac