DEV Community

loading...
Cover image for Automate Terraform CLI

Automate Terraform CLI

pauld profile image Paul Updated on ・2 min read

TL:DR
If you want to know more about the mechanisms at hand, head DOWNLOAD > TerraformCLI and CHECKSUM > Terraformchksm
First, create the PGP key and name it hashicorp.asc and save it in the same directory as the Terraform CLI will be installed at. (Click CHECKSUM https://www.hashicorp.com/security to copy the PGP key)
Second, I stored the Terraform CLI in the dir that I'm working in, save it at /usr/bin if you're using macOS or Linux. 
To start the script, I have created three arguments that will simplify the version and the file version that Hashicorp keeps updating. (for good reasons, I assume the developers want more features in Terraform I'm all for that)

#!/bin/bash
# Update Terraform version bash script automation
#Arguments
version=$1 #version# 0.2.28
tfver=$2 # terraform_0_2_28_darwin_amd64.zip
tfsha=$3 # terraform_0.2.28_SHA256SUMS
tfshasig=$4 #terraform_0.2.28_SHA256SUMS.sig
cd $HOME/Path/to/TerraformCLI
wget https://releases.hashicorp.com/terraform/$version/$tfver
gpg  --import hashicorp.asc
curl -Os https://releases.hashicorp.com/terraform/$version/$tfsha
curl -Os https://releases.hashicorp.com/terraform/$version/$tfshasig
gpg  --verify $tfshasig $tfsha
shasum -a 256 -c $tfsha
unzip $tfver
rm $tfver
Enter fullscreen mode Exit fullscreen mode

The above image is showing four variables version number, file with version, SHA256SUMS with version, and SHA256SUMS.sig with version. 
Copy and paste the bash script and chmod +x automateTFCLI.sh, then execute the script with this command
./automateTFCLI.sh 0.12.28 terraform_0.12.28_darwin_amd64.zip terraform_0.12.28_SHA256SUMS terraform_0.12.28_SHA256SUMS.sig
The script takes care of downloading, checking the integrity of the file and then, unzipping and deleting. 
Leaving the Terraform CLI inflation (meaning in use)
Next run Terraform - version to output version and confirm you have the working Terraform. 
(side note, I was tired of typing long cli commands so I put an alias in bash_profile to shorten it i.e alias tf="terraform" and also 
tfplan="terraform init && terraform plan")

Hashicorp Terraform has made this much simpler by adding Terraform CLI from the repo using APT. SetupTFCLIAPT

If you found this article to be useful please consider buying me a cup of coffee

Discussion (0)

pic
Editor guide