Installing Terraform & Packer, The Scripted Way

Here at Peloton Tech.IO we’ve installed Terraform and Packer a few gazillion times. In order to simplify the way we go about installing Terraform and Packer we’ve put together a script to help get the job done.

Terraform Terraforming!

Terraform Terraforming!

There are two prerequisites to using this install script. The first is to install the jq CLI. To install jq on the Mac, just use Brew and issue the brew install jq command. On Debian, Elementary, Ubuntu, or related apt-get systems, issue the sudo apt-get install jq command. On Windows you can use Chocolately NuGet to install jqwith chocolately install jq. If you’re not using one of these operating systems you can likely still find your binary to install on the download page.

The other installation needed is for the unzip command. This can also be installed via brew install unzip on MacOS, on Debian, Ubuntu, or related apt-get system run sudo apt-get install unzip. For Windows simply ensure that the zip/unzip functionality is enabled in bash for Windows.

Speaking of Windows, to use this script you would need bash for Windows or some type fo subsystem that would enable execution of bash scripts. Sorry, no powershell, but we’d be happy to make a mention if anybody has put together a powershell script, so just let us know via @PelotonTechIO and we’d be happy to add it here!

When downloading Terraform and Packer there’s a page for that, but using jq (hat tip Ryan MacLean for the idea and script snippet) we can add it to the script and no longer need to navigate over and manually download either.

For the MacOS binaries, using “darwin” for the search string works as shown.

terraform_url=$(curl https://releases.hashicorp.com/index.json | jq '{terraform}' | egrep "darwin.*64" | sort -r | head -1 | awk -F[\"] '{print $4}')
packer_url=$(curl https://releases.hashicorp.com/index.json | jq '{packer}' | egrep "darwin.*64" | sort -r | head -1 | awk -F[\"] '{print $4}')

But for Linux the string needs swapped out to “linux”, specifically the 64-bit version, as shown.

terraform_url=$(curl https://releases.hashicorp.com/index.json | jq '{terraform}' | egrep "linux.*64" | sort -r | head -1 | awk -F[\"] '{print $4}')
packer_url=$(curl https://releases.hashicorp.com/index.json | jq '{packer}' | egrep "linux.*64" | sort -r | head -1 | awk -F[\"] '{print $4}')

Finally to have the script select which download variables and commands to execute, an if combined with its respective else if can be used.

if [ "$(uname)" == "Darwin" ]; then
terraform_url=$(curl https://releases.hashicorp.com/index.json | jq '{terraform}' | egrep "darwin.*64" | sort -r | head -1 | awk -F[\"] '{print $4}')
packer_url=$(curl https://releases.hashicorp.com/index.json | jq '{packer}' | egrep "darwin.*64" | sort -r | head -1 | awk -F[\"] '{print $4}')
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
terraform_url=$(curl https://releases.hashicorp.com/index.json | jq '{terraform}' | egrep "linux.*64" | sort -r | head -1 | awk -F[\"] '{print $4}')
packer_url=$(curl https://releases.hashicorp.com/index.json | jq '{packer}' | egrep "linux.*64" | sort -r | head -1 | awk -F[\"] '{print $4}')
fi

With that done, the terraform_url and packer_url variables can now simply be passed to curl to download the Terraform and Packer binaries.

curl -o terraform.zip $terraform_url
curl -o packer.zip $packer_url

With the binaries downloaded they simply need unzipped and placed into a directory, then referenced via the ~/.bashrc, ~/.bash_profile, or whatever file is used by the system to set the path to executable binaries.

On MacOS this is a simple echo to file, and then source command against the ~/.bash_profile.

echo '
# Terraform & Packer Paths.
export PATH=~/terraform/:~/packer/:$PATH
' >>~/.bash_profile

source ~/.bash_profile

All that put together combines to form the following complete installation script.

Once the script is executed, both terraform and packer are available. To verify we usually either type just terraform and packer or to verify and check the version we’d execute terraform version and packer version.

$ terraform version
Terraform v0.9.4
$ packer version
Packer v1.0.0

With that, we’re all set to terraform some cloudy planetary ecosystems!

If you have any suggestions, improvements, or other helper scripts you’d like to see us blog about, just let us know via @PeloTechnology. Happy terraforming!