Daniel's Tech Blog

Cloud Computing, Cloud Native & Kubernetes

Pin Terraform version in Azure Cloud Shell

When you cannot always work with the latest Terraform version for you Infrastructure as Code deployments and need to stick to a specific one like 0.12.10 for instance, then you should continue reading.

The Azure Cloud Shell is Microsoft’s CLI as a Service offering in Azure and provides you with a browser-based interface that is accessible from everywhere and comes with a rich tool set.

-> https://docs.microsoft.com/en-us/azure/cloud-shell/features#tools

The tools in Azure Cloud Shell get updated automatically on a regular basis, but how you can pin a specific Terraform version in the Cloud Shell itself?

> terraform version
Terraform v0.12.12
> which terraform

Before we continue, let us step back for a moment and elaborate on why we would like to do this.

Using Terraform in Azure Cloud Shell and on your workstation at the same time requires careful attention using the exact same version in conjunction with the Terraform state. Had you used Terraform 0.12.12 in Azure Cloud Shell for a deployment and want to rollout changes to the same deployment with Terraform 0.12.10 on your workstation, then this does not work. Terraform complains about that the state has been modified by a higher Terraform version. The only solution in this case is to update Terraform on your workstation to the same version as in Azure Cloud Shell.

If your team decided to stick on Terraform 0.12.10 for a while, you should pin the version in Azure Cloud Shell avoiding any conflicts in conjunction with the Terraform state.

The process is easy and requires only a few lines of code.

curl -O https://releases.hashicorp.com/terraform/0.12.10/terraform_0.12.10_linux_386.zip \
    && unzip terraform_0.12.10_linux_386.zip \
    && mkdir bin \
    && mv terraform bin/

After you downloaded the specific Terraform version. You create a folder named bin in your home directory and move the terraform executable into it. Afterwards restart your Cloud Shell session. The bin folder in your home directory gets added automatically to your PATH environment variable.

> terraform version
Terraform v0.12.10

Your version of Terraform is out of date! The latest version
is 0.12.12. You can update by downloading from www.terraform.io/downloads.html
> which terraform

The same process works for every executable you want to have in your Cloud Shell like kubectx, kubens and sonobuoy in my case.

> ls
kubectx  kubens  sonobuoy  terraform

WordPress Cookie Notice by Real Cookie Banner