Dec 21, 2017

Use a Terraform wrapper script to easily manage Terraform installations

Terraform is a great tool for managing your cloud infrastructure resources using code. It supports Amazon Web Services, Google Cloud Platform, Microsoft Azure, and more. If you are looking to use Terraform for the first time, I recommend reading Introduction to Terraform.

This Terraform wrapper is a script that gets checked in with your Terraform project’s source code and automatically loads the appropriate version of Terraform for the particular project. It follows a pattern that I grew to appreciate as a Gradle user, the Gradle Wrapper (gradlew). For example, instead of running terraform init, you would run ./terraformw init.

As my clients are using many small Terraform projects, we found that we wanted a similar solution as gradlew that would provide the same benefits:

  • Always use the correct version of Terraform for the current project / version
  • Avoids the need for developers to install or update manually, and communication around those updates
  • Avoids the need to separately install or update Terraform on CI server
  • Easily manage multiple versions of Terraform

How to use

  1. Copy this script into the root of your Terraform project directory(ies) as a file named terraformw.
    #!/bin/bash
     
    TERRAFORM_VERSION="0.11.1"
     
    if [ -z ${TERRAFORM_BIN_PATH+x} ]; then
    	TERRAFORM_BIN_PATH="$HOME/.terraform";
    fi
     
    platform='unknown'
    unamestr=`uname`
    if [[ "$unamestr" == 'Linux' ]]; then
       platform='linux'
    elif [[ "$unamestr" == 'FreeBSD' ]]; then
       platform='freebsd'
    elif [[ "$unamestr" == 'Darwin' ]]; then
       platform='darwin'
    fi
     
    arch="unknown"
    unamestr=`uname -m`
    if [[ "$unamestr" == 'x86_64' ]]; then
       arch='amd64'
    elif [[ "$unamestr" == 'i686' ]]; then
       arch='386'
    fi
     
    TERRAFORM_URL="https://releases.hashicorp.com/terraform/$TERRAFORM_VERSION/terraform_$TERRAFORM_VERSION"_"$platform"_"$arch".zip
     
    TERRAFORM_PATH="$TERRAFORM_BIN_PATH/$TERRAFORM_VERSION"
    TERRAFORM_CMD="$TERRAFORM_PATH/terraform"
    if ! type "$TERRAFORM_CMD" > /dev/null 2>&1; then
    	echo "Downloading $TERRAFORM_URL"
    	mkdir -p "$TERRAFORM_PATH"
    	curl -s "$TERRAFORM_URL" -o $TERRAFORM_PATH.zip
    	cd $TERRAFORM_PATH && unzip $TERRAFORM_PATH.zip && cd -
    fi
     
    $TERRAFORM_CMD $@
     
    #
  2. Update the TERRAFORM_VERSION variable to match the version of Terraform you are using.
  3. Give the script executable permissions (chmod +x terraformw)
  4. Run it instead of the terraform executable: ./terraformw plan -out plan.out
  5. Commit the file to source control

There you have it! This is a pretty simple script but can save a lot of headaches when managing multiple projects or working with multiple DevOps engineers.

Final thoughts

Constantly-evolving tools are transforming the software development landscape, but there is little more frustrating than being unable to build or execute your code that was written just a few months ago due to a change in your tools or dependencies. A wrapper script like terraformw will help you keep your head screwed on straight.

Thanks to this gist by advincze which served as a starting point for this script.

About the Author

David Norton profile.

David Norton

Director, Platform Engineering

Passionate about continuous delivery, cloud-native architecture, DevOps, and test-driven development.

  • Experienced in cloud infrastructure technologies such as Terraform, Kubernetes, Docker, AWS, and GCP.
  • Background heavy in enterprise JVM technologies such as Groovy, Spring, Spock, Gradle, JPA, Jenkins.
  • Focus on platform transformation, continuous delivery, building agile teams and high-scale applications.
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
An Exploration in Rust: Musings From a Java/C++ Developer
Why Rust? It’s fast (runtime performance) It’s small (binary size) It’s safe (no memory leaks) It’s modern (build system, language features, etc) When Is It Worth It? Embedded systems (where it is implied that interpreted […]
Getting Started with CSS Container Queries
For as long as I’ve been working full-time on the front-end, I’ve heard about the promise of container queries and their potential to solve the majority of our responsive web design needs. And, for as […]
Simple improvements to making decisions in teams
Software development teams need to make a lot of decisions. Functional requirements, non-functional requirements, user experience, API contracts, tech stack, architecture, database schemas, cloud providers, deployment strategy, test strategy, security, and the list goes on. […]
JavaScript Bundle Optimization – Polyfills
If you are lucky enough to only support a small subset of browsers (for example, you are targeting a controlled set of users), feel free to move along. However, if your website is open to […]