Skip to content

Instantly share code, notes, and snippets.

@nathanielks
Last active June 3, 2023 17:24
Show Gist options
  • Select an option

  • Save nathanielks/5bd4de708e831bbc170f to your computer and use it in GitHub Desktop.

Select an option

Save nathanielks/5bd4de708e831bbc170f to your computer and use it in GitHub Desktop.

Revisions

  1. nathanielks revised this gist Dec 3, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -27,4 +27,6 @@ Caveat: this won't work if you've written a plan out to a file and then try to a
    ./terraform-env.sh apply plan
    ```

    The above will not work. It's recommend you just run `terraform apply plan` as the plan file will contain all the necessary bits to run the apply.
    The above will not work. It's recommend you just run `terraform apply plan` as the plan file will contain all the necessary bits to run the apply.

    This script also allows you to use environment-specific config files. For example, you could have specific dns settings for production in `env/production/dns-extra.tf` and the script will copy the file into the directory you're calling the script from, run terraform, then delete the copied file.
  2. nathanielks revised this gist Dec 3, 2015. 3 changed files with 54 additions and 24 deletions.
    7 changes: 4 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,10 @@ This script will pull down an S3 remote configuration before running any terrafo
    ```
    main.tf
    terraform.cfg
    vars/dev
    vars/staging
    vars/production
    env/dev/vars
    env/staging/vars
    env/whatever/vars
    env/whatever/somefile.tf
    ```

    `terraform.cfg` is required for the remote configuration.
    65 changes: 45 additions & 20 deletions terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    #!/bin/bash
    #!/usr/bin/env bash

    set -o pipefail
    set -u

    function help {
    echo "usage: ${0} <environment> <action> [<args>]"
    @@ -13,39 +16,42 @@ function contains_element () {
    return 1
    }

    function files_exist(){
    ls ${1} 1> /dev/null 2>&1
    }

    #All of the args are mandatory.
    if [ $# -lt 1 ]; then
    help
    fi


    # Let's set up our environment
    export ENVIRONMENT=$1
    export ACTION=$2
    ADDTL_PARAMS=${*:3}

    CONFIG_FILE=./terraform.cfg

    # Let's check the existence of the config file
    if [ ! -f $CFG_FILE ]; then
    echo "Error: $CFG_FILE does not exist. You'll need to create a config file so we know where to set up the remote config."
    if [ ! -f $CONFIG_FILE ]; then
    echo "Error: $CONFIG_FILE does not exist. You'll need to create a config file so we know where to set up the remote config."
    exit 1
    fi

    source ./terraform.cfg

    # Let's set up our environment
    if [[ $default_environment && ${default_environment-x} ]]
    then
    ENVIRONMENT=$default_environment
    ACTION=$1
    ADDTL_PARAMS=${*:2}
    else
    ENVIRONMENT=$1
    ACTION=$2
    ADDTL_PARAMS=${*:3}
    fi
    source ${CONFIG_FILE}

    # Let's set up our variables
    ALLOWS_VARFILE=(apply plan push refresh destroy)
    ENV_FILE=.terraform/environment
    VARS_FILE=vars/$ENVIRONMENT
    ENV_DIR=env/$ENVIRONMENT
    VARS_FILE=${ENV_DIR}/vars
    VARS_FILE_FLAG=
    BUCKET_KEY=$bucket_prefix/state/$ENVIRONMENT
    PREVIOUS_ENVIRONMENT=$([ -f $ENV_FILE ] && echo "$(<$ENV_FILE)" || echo "previous")

    EXTRA_ARGS=${extra_args:-''}
    PRE_CMD=${pre_command:-''}
    POST_CMD=${post_command:-''}

    # Let's check to see if a vars file exists for the requested environment before proceeding
    if [ ! -f $VARS_FILE ]; then
    @@ -60,7 +66,8 @@ if [ $? -eq 0 ]; then
    fi

    # Let's check if the requested environment is different from the previous environment
    if [ $PREVIOUS_ENVIRONMENT != $ENVIRONMENT ]; then
    if [ $PREVIOUS_ENVIRONMENT != $ENVIRONMENT ] || [ ! -f '.terraform/terraform.tfstate' ]
    then

    # Move current state out of the way to make room for the new state
    mv -f .terraform/terraform.tfstate .terraform/terraform.tfstate.$PREVIOUS_ENVIRONMENT > /dev/null 2>&1
    @@ -74,5 +81,23 @@ if [ $PREVIOUS_ENVIRONMENT != $ENVIRONMENT ]; then

    fi

    # Let's run the PRE_CMD hook if it's defined
    eval ${PRE_CMD}

    # let's copy environment specific configuration to the root of the directory
    if files_exist ${ENV_DIR}/*.tf; then
    cd env/${ENVIRONMENT}
    pax -wrs'/\.tf$/\.env\.tf/' *.tf ../../
    cd ../../
    fi

    # Let's do work!
    terraform $ACTION $VARS_FILE_FLAG $ADDTL_PARAMS
    terraform $ACTION $VARS_FILE_FLAG $ADDTL_PARAMS ${EXTRA_ARGS}

    # Let's remove those environment-specific configuration files we copied earlier
    if files_exist *.env.tf; then
    rm *.env.tf
    fi

    # Let's run the POST_CMD hook if it's defined
    eval ${POST_CMD}
    6 changes: 5 additions & 1 deletion terraform.cfg
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    bucket=some_secret_bucket
    bucket_prefix=example
    region=us-east-1
    default_environment=dev # This is optional. if set, specifying the environment before running commands isn't necessary

    # Both are optional. This allows you to run scripts before and after terraform executes
    # (perhaps to decrypt/encrypt files, move files, modify templates, etc)
    pre_command='bin/pre.sh'
    post_command='bin/post.sh'
  3. nathanielks revised this gist Jul 28, 2015. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -17,4 +17,13 @@ You can use it like so:
    ./terraform-env.sh production apply -var="some_var=some_value"
    ```

    You can use the script just like you'd use `terraform`, passing whatever arguments you would before. It will only pull the latest environment if the current environment is different from the one you're requesting.
    You can use the script just like you'd use `terraform`, passing whatever arguments you would before. It will only pull the latest environment if the current environment is different from the one you're requesting.

    Caveat: this won't work if you've written a plan out to a file and then try to apply that plan as `terraform apply` doesn't accept the `-var-file` flag when reading from a plan. For reference:

    ```
    ./terraform-env.sh plan -out plan
    ./terraform-env.sh apply plan
    ```

    The above will not work. It's recommend you just run `terraform apply plan` as the plan file will contain all the necessary bits to run the apply.
  4. nathanielks revised this gist Jul 28, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -63,8 +63,8 @@ fi
    if [ $PREVIOUS_ENVIRONMENT != $ENVIRONMENT ]; then

    # Move current state out of the way to make room for the new state
    mv -f .terraform/terraform.tfstate .terraform/terraform.tfstate.$PREVIOUS_ENVIRONMENT &>2
    mv -f .terraform/terraform.tfstate.backup .terraform/terraform.tfstate.backup.$PREVIOUS_ENVIRONMENT &>2
    mv -f .terraform/terraform.tfstate .terraform/terraform.tfstate.$PREVIOUS_ENVIRONMENT > /dev/null 2>&1
    mv -f .terraform/terraform.tfstate.backup .terraform/terraform.tfstate.backup.$PREVIOUS_ENVIRONMENT > /dev/null 2>&1

    # Let's log the new environment for later
    echo $ENVIRONMENT > $ENV_FILE
  5. nathanielks revised this gist Jul 28, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ vars/staging
    vars/production
    ```

    `ansible.cfg` is required for the remote configuration.
    `terraform.cfg` is required for the remote configuration.

    You can use it like so:
    ```
  6. nathanielks revised this gist Jul 28, 2015. 2 changed files with 25 additions and 15 deletions.
    38 changes: 24 additions & 14 deletions terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -14,26 +14,37 @@ function contains_element () {
    }

    #All of the args are mandatory.
    if [ $# -lt 2 ]; then
    if [ $# -lt 1 ]; then
    help
    fi

    # Let's set up our variables
    ENVIRONMENT=$1
    ACTION=$2
    ALLOWS_VARFILE=(apply plan push refresh destroy)
    ENV_FILE=.terraform/environment
    VARS_FILE=vars/$ENVIRONMENT
    CFG_FILE=./terraform.cfg
    VARS_FILE_FLAG=

    # Let's check the existence of the config file
    if [ ! -f $CFG_FILE ]; then
    echo "Error: $CFG_FILE does not exist. You'll need to create a config file so we know where to set up the remote config."
    exit 1
    fi

    source $CFG_FILE
    source ./terraform.cfg

    # Let's set up our environment
    if [[ $default_environment && ${default_environment-x} ]]
    then
    ENVIRONMENT=$default_environment
    ACTION=$1
    ADDTL_PARAMS=${*:2}
    else
    ENVIRONMENT=$1
    ACTION=$2
    ADDTL_PARAMS=${*:3}
    fi

    # Let's set up our variables
    ALLOWS_VARFILE=(apply plan push refresh destroy)
    ENV_FILE=.terraform/environment
    VARS_FILE=vars/$ENVIRONMENT
    VARS_FILE_FLAG=
    BUCKET_KEY=$bucket_prefix/state/$ENVIRONMENT
    PREVIOUS_ENVIRONMENT=$([ -f $ENV_FILE ] && echo "$(<$ENV_FILE)" || echo "previous")


    # Let's check to see if a vars file exists for the requested environment before proceeding
    @@ -49,7 +60,6 @@ if [ $? -eq 0 ]; then
    fi

    # Let's check if the requested environment is different from the previous environment
    PREVIOUS_ENVIRONMENT=$([ -f $ENV_FILE ] && echo "$(<$ENV_FILE)" || echo "previous")
    if [ $PREVIOUS_ENVIRONMENT != $ENVIRONMENT ]; then

    # Move current state out of the way to make room for the new state
    @@ -60,9 +70,9 @@ if [ $PREVIOUS_ENVIRONMENT != $ENVIRONMENT ]; then
    echo $ENVIRONMENT > $ENV_FILE

    # Set up remote configuration and pull latest version
    terraform remote config -backend S3 -backend-config="bucket=$bucket" -backend-config="key=$bucket_key" -backend-config="region=$region"
    terraform remote config -backend S3 -backend-config="bucket=$bucket" -backend-config="key=$BUCKET_KEY" -backend-config="region=$region"

    fi

    # Let's do work!
    terraform $ACTION $VARS_FILE_FLAG ${*:3}
    terraform $ACTION $VARS_FILE_FLAG $ADDTL_PARAMS
    2 changes: 1 addition & 1 deletion terraform.cfg
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    bucket=some_secret_bucket
    bucket_prefix=example
    region=us-east-1
    bucket_key=$bucket_prefix/state/$ENVIRONMENT
    default_environment=dev # This is optional. if set, specifying the environment before running commands isn't necessary
  7. nathanielks revised this gist Jul 27, 2015. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -24,9 +24,16 @@ ACTION=$2
    ALLOWS_VARFILE=(apply plan push refresh destroy)
    ENV_FILE=.terraform/environment
    VARS_FILE=vars/$ENVIRONMENT
    CFG_FILE=./terraform.cfg
    VARS_FILE_FLAG=

    source ./terraform.cfg
    # Let's check the existence of the config file
    if [ ! -f $CFG_FILE ]; then
    echo "Error: $CFG_FILE does not exist. You'll need to create a config file so we know where to set up the remote config."
    exit 1
    fi

    source $CFG_FILE


    # Let's check to see if a vars file exists for the requested environment before proceeding
  8. nathanielks revised this gist Jul 27, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ fi
    # Let's set up our variables
    ENVIRONMENT=$1
    ACTION=$2
    ALLOWS_VARFILE=(apply plan push refresh)
    ALLOWS_VARFILE=(apply plan push refresh destroy)
    ENV_FILE=.terraform/environment
    VARS_FILE=vars/$ENVIRONMENT
    VARS_FILE_FLAG=
  9. nathanielks revised this gist Jul 27, 2015. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -43,8 +43,7 @@ fi

    # Let's check if the requested environment is different from the previous environment
    PREVIOUS_ENVIRONMENT=$([ -f $ENV_FILE ] && echo "$(<$ENV_FILE)" || echo "previous")
    if [ $PREVIOUS_ENVIRONMENT != $ENVIRONMENT ]
    then
    if [ $PREVIOUS_ENVIRONMENT != $ENVIRONMENT ]; then

    # Move current state out of the way to make room for the new state
    mv -f .terraform/terraform.tfstate .terraform/terraform.tfstate.$PREVIOUS_ENVIRONMENT &>2
  10. nathanielks revised this gist Jul 27, 2015. 1 changed file with 12 additions and 4 deletions.
    16 changes: 12 additions & 4 deletions terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -18,19 +18,27 @@ if [ $# -lt 2 ]; then
    help
    fi


    # Let's set up our variables
    ENVIRONMENT=$1
    ACTION=$2
    VARFILE=
    ALLOWS_VARFILE=(apply plan push refresh)
    ENV_FILE=.terraform/environment
    VARS_FILE=vars/$ENVIRONMENT
    VARS_FILE_FLAG=

    source ./terraform.cfg


    # Let's check to see if a vars file exists for the requested environment before proceeding
    if [ ! -f $VARS_FILE ]; then
    echo "Error: $VARS_FILE does not exist. You'll need to create a vars file for the requested environment before continuing."
    exit 1
    fi

    # Checks if current action allows a varfile to be passed
    contains_element "$ACTION" "${ALLOWS_VARFILE[@]}"
    if [ $? -eq 0 ]; then
    VARFILE="-var-file=vars/$ENVIRONMENT"
    VARS_FILE_FLAG="-var-file=$VARS_FILE"
    fi

    # Let's check if the requested environment is different from the previous environment
    @@ -51,4 +59,4 @@ then
    fi

    # Let's do work!
    terraform $ACTION $VARFILE ${*:3}
    terraform $ACTION $VARS_FILE_FLAG ${*:3}
  11. nathanielks revised this gist Jul 27, 2015. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -33,14 +33,14 @@ if [ $? -eq 0 ]; then
    VARFILE="-var-file=vars/$ENVIRONMENT"
    fi

    # Let's check if the requested environment is different from our current environment
    CURRENT_ENVIRONMENT=$(<$ENV_FILE)
    if [ $CURRENT_ENVIRONMENT != $ENVIRONMENT ]
    # Let's check if the requested environment is different from the previous environment
    PREVIOUS_ENVIRONMENT=$([ -f $ENV_FILE ] && echo "$(<$ENV_FILE)" || echo "previous")
    if [ $PREVIOUS_ENVIRONMENT != $ENVIRONMENT ]
    then

    # Move current state out of the way to make room for the new state
    mv -f .terraform/terraform.tfstate .terraform/terraform.tfstate.$ENVIRONMENT &>2
    mv -f .terraform/terraform.tfstate.backup .terraform/terraform.tfstate.backup.$ENVIRONMENT &>2
    mv -f .terraform/terraform.tfstate .terraform/terraform.tfstate.$PREVIOUS_ENVIRONMENT &>2
    mv -f .terraform/terraform.tfstate.backup .terraform/terraform.tfstate.backup.$PREVIOUS_ENVIRONMENT &>2

    # Let's log the new environment for later
    echo $ENVIRONMENT > $ENV_FILE
    @@ -51,4 +51,4 @@ then
    fi

    # Let's do work!
    terraform $ACTION $VARFILE ${*:3}
    terraform $ACTION $VARFILE ${*:3}
  12. nathanielks revised this gist Jul 27, 2015. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,7 @@ ENVIRONMENT=$1
    ACTION=$2
    VARFILE=
    ALLOWS_VARFILE=(apply plan push refresh)
    ENV_FILE=.terraform/environment

    source ./terraform.cfg

    @@ -33,7 +34,7 @@ if [ $? -eq 0 ]; then
    fi

    # Let's check if the requested environment is different from our current environment
    CURRENT_ENVIRONMENT=$(<.terraform/environment)
    CURRENT_ENVIRONMENT=$(<$ENV_FILE)
    if [ $CURRENT_ENVIRONMENT != $ENVIRONMENT ]
    then

    @@ -42,12 +43,12 @@ then
    mv -f .terraform/terraform.tfstate.backup .terraform/terraform.tfstate.backup.$ENVIRONMENT &>2

    # Let's log the new environment for later
    echo $ENVIRONMENT > .terraform/environment
    echo $ENVIRONMENT > $ENV_FILE

    # Set up remote configuration and pull latest version
    terraform remote config -backend S3 -backend-config="bucket=$bucket" -backend-config="key=$bucket_key" -backend-config="region=$region"

    fi

    # Let's do work!
    terraform $ACTION $VARFILE ${*:3}
    terraform $ACTION $VARFILE ${*:3}
  13. nathanielks revised this gist Jul 27, 2015. 2 changed files with 15 additions and 8 deletions.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -17,4 +17,4 @@ You can use it like so:
    ./terraform-env.sh production apply -var="some_var=some_value"
    ```

    You can use the script just like you'd use `terraform`, passing whatever arguments you would before.
    You can use the script just like you'd use `terraform`, passing whatever arguments you would before. It will only pull the latest environment if the current environment is different from the one you're requesting.
    21 changes: 14 additions & 7 deletions terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -32,15 +32,22 @@ if [ $? -eq 0 ]; then
    VARFILE="-var-file=vars/$ENVIRONMENT"
    fi

    # Move current state out of the way to make room for the new state
    mv -f .terraform/terraform.tfstate .terraform/terraform.tfstate.$ENVIRONMENT &>2
    mv -f .terraform/terraform.tfstate.backup .terraform/terraform.tfstate.backup.$ENVIRONMENT &>2
    # Let's check if the requested environment is different from our current environment
    CURRENT_ENVIRONMENT=$(<.terraform/environment)
    if [ $CURRENT_ENVIRONMENT != $ENVIRONMENT ]
    then

    # Let's log the current environment, may use it later
    echo $ENVIRONMENT > .terraform/environment
    # Move current state out of the way to make room for the new state
    mv -f .terraform/terraform.tfstate .terraform/terraform.tfstate.$ENVIRONMENT &>2
    mv -f .terraform/terraform.tfstate.backup .terraform/terraform.tfstate.backup.$ENVIRONMENT &>2

    # Set up remote configuration and pull latest version
    terraform remote config -backend S3 -backend-config="bucket=$bucket" -backend-config="key=$bucket_key" -backend-config="region=$region"
    # Let's log the new environment for later
    echo $ENVIRONMENT > .terraform/environment

    # Set up remote configuration and pull latest version
    terraform remote config -backend S3 -backend-config="bucket=$bucket" -backend-config="key=$bucket_key" -backend-config="region=$region"

    fi

    # Let's do work!
    terraform $ACTION $VARFILE ${*:3}
  14. nathanielks revised this gist Jul 27, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -15,4 +15,6 @@ You can use it like so:
    ./terraform-env.sh dev show
    ./terraform-env.sh staging plan -out plan
    ./terraform-env.sh production apply -var="some_var=some_value"
    ```
    ```

    You can use the script just like you'd use `terraform`, passing whatever arguments you would before.
  15. nathanielks revised this gist Jul 27, 2015. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,7 @@ if [ $# -lt 2 ]; then
    help
    fi


    ENVIRONMENT=$1
    ACTION=$2
    VARFILE=
    @@ -31,5 +32,15 @@ if [ $? -eq 0 ]; then
    VARFILE="-var-file=vars/$ENVIRONMENT"
    fi

    # Move current state out of the way to make room for the new state
    mv -f .terraform/terraform.tfstate .terraform/terraform.tfstate.$ENVIRONMENT &>2
    mv -f .terraform/terraform.tfstate.backup .terraform/terraform.tfstate.backup.$ENVIRONMENT &>2

    # Let's log the current environment, may use it later
    echo $ENVIRONMENT > .terraform/environment

    # Set up remote configuration and pull latest version
    terraform remote config -backend S3 -backend-config="bucket=$bucket" -backend-config="key=$bucket_key" -backend-config="region=$region"

    # Let's do work!
    terraform $ACTION $VARFILE ${*:3}
  16. nathanielks revised this gist Jul 23, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -18,18 +18,18 @@ if [ $# -lt 2 ]; then
    help
    fi

    source ./terraform.cfg

    ENVIRONMENT=$1
    ACTION=$2
    VARFILE=
    ALLOWS_VARFILE=(apply plan push refresh)

    source ./terraform.cfg

    # Checks if current action allows a varfile to be passed
    contains_element "$ACTION" "${ALLOWS_VARFILE[@]}"
    if [ $? -eq 0 ]; then
    VARFILE="-var-file=vars/$ENVIRONMENT"
    fi

    terraform remote config -backend S3 -backend-config="bucket=$bucket" -backend-config="key=$bucket_key" -backend-config="region=$region"
    terraform $ACTION $VARFILE ${*:3}
    terraform $ACTION $VARFILE ${*:3}
  17. nathanielks revised this gist Jul 22, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ ACTION=$2
    VARFILE=
    ALLOWS_VARFILE=(apply plan push refresh)

    # Validate the desired role.
    # Checks if current action allows a varfile to be passed
    contains_element "$ACTION" "${ALLOWS_VARFILE[@]}"
    if [ $? -eq 0 ]; then
    VARFILE="-var-file=vars/$ENVIRONMENT"
  18. nathanielks revised this gist Jul 22, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    Assumes the following structure:
    This script will pull down an S3 remote configuration before running any terraform actions. Assumes the following structure:

    ```
    main.tf
  19. nathanielks revised this gist Jul 22, 2015. 2 changed files with 3 additions and 3 deletions.
    3 changes: 1 addition & 2 deletions terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,5 @@ if [ $? -eq 0 ]; then
    VARFILE="-var-file=vars/$ENVIRONMENT"
    fi


    terraform remote config -backend S3 -backend-config="bucket=$bucket" -backend-config="key=$bucket_prefix/state/$ENVIRONMENT" -backend-config="region=$region"
    terraform remote config -backend S3 -backend-config="bucket=$bucket" -backend-config="key=$bucket_key" -backend-config="region=$region"
    terraform $ACTION $VARFILE ${*:3}
    3 changes: 2 additions & 1 deletion terraform.cfg
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    bucket=some_secret_bucket
    bucket_prefix=example
    region=us-east-1
    region=us-east-1
    bucket_key=$bucket_prefix/state/$ENVIRONMENT
  20. nathanielks revised this gist Jul 22, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@ vars/staging
    vars/production
    ```

    `ansible.cfg` is required for the remote configuration.

    You can use it like so:
    ```
    ./terraform-env.sh dev show
  21. nathanielks created this gist Jul 22, 2015.
    16 changes: 16 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    Assumes the following structure:

    ```
    main.tf
    terraform.cfg
    vars/dev
    vars/staging
    vars/production
    ```

    You can use it like so:
    ```
    ./terraform-env.sh dev show
    ./terraform-env.sh staging plan -out plan
    ./terraform-env.sh production apply -var="some_var=some_value"
    ```
    36 changes: 36 additions & 0 deletions terraform-env.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/bin/bash

    function help {
    echo "usage: ${0} <environment> <action> [<args>]"
    exit 1
    }

    function contains_element () {
    local i
    for i in "${@:2}"; do
    [[ "$i" == "$1" ]] && return 0
    done
    return 1
    }

    #All of the args are mandatory.
    if [ $# -lt 2 ]; then
    help
    fi

    source ./terraform.cfg

    ENVIRONMENT=$1
    ACTION=$2
    VARFILE=
    ALLOWS_VARFILE=(apply plan push refresh)

    # Validate the desired role.
    contains_element "$ACTION" "${ALLOWS_VARFILE[@]}"
    if [ $? -eq 0 ]; then
    VARFILE="-var-file=vars/$ENVIRONMENT"
    fi


    terraform remote config -backend S3 -backend-config="bucket=$bucket" -backend-config="key=$bucket_prefix/state/$ENVIRONMENT" -backend-config="region=$region"
    terraform $ACTION $VARFILE ${*:3}
    3 changes: 3 additions & 0 deletions terraform.cfg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    bucket=some_secret_bucket
    bucket_prefix=example
    region=us-east-1