DEV Community

Cover image for Automate Terraform CLI

Posted on • Updated on

Automate Terraform CLI

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 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)

# Update Terraform version bash script automation
version=$1 #version# 0.2.28
tfver=$2 #
tfsha=$3 # terraform_0.2.28_SHA256SUMS
tfshasig=$4 #terraform_0.2.28_SHA256SUMS.sig
cd $HOME/Path/to/TerraformCLI
gpg  --import hashicorp.asc
curl -Os$version/$tfsha
curl -Os$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, then execute the script with this command
./ 0.12.28 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

Top comments (0)