How to Update Terraform to the Latest Version in Windows using PowerShell
I spent a large time of the day writing Terraform scripts, both at the office and at home.
Sometimes, I wrote code in one machine and continue in a different one, so, once in a while, I got this error when the version is different in both machines:
Error: Error locking state: Error acquiring the state lock: state snapshot was created by Terraform v0.12.21, which is newer than current v0.12.20; upgrade to Terraform v0.12.21 or greater to work with this state
So I decided to write a script in PowerShell to update Terraform to the latest version in Windows to simplify the process. This example is for the 64 bits version of Windows:
Let take a look at the code, line by line:
- In this line, the script downloads the page of Terraform releases.
$WebResponse = Invoke-WebRequest "https://releases.hashicorp.com/terraform"
2. Here, I describe the file to download. In this case, we are downloading the Windows 64-bits version. For the 32-bits, use “ _windows_386.zip” instead.
$tfFile = $WebResponse.Links[1].outerText + "_windows_amd64.zip"
3. We are defining the full URL of the file to download from the Terraform web site.