Thursday, November 21, 2024

PowerShell: Automate Terraform Install On Windows

Share


While HashiCorp Terraform is a single executable, it’s really useful to install it on the local machine so the terraform.exe executable can be run from anywhere. To do this, Terraform needs to be downloaded, placed somewhere on the local machine, and then that path must be added to the system PATH environment variable.

I’ve done this before, but haven’t shared the script out, so I though I would do so. The following PowerShell script will download the latest stable version of Terraform, and essentially install it on the local Windows machine:

# ############################################################
# Set the URL to check the latest Terraform version
# ############################################################
$latestReleaseUrl = "

# ############################################################
# Define the installation directory
# ############################################################
$installDir = "C:\Terraform"

# ############################################################
# Create the installation directory if it does not exist
# ############################################################
if (-Not (Test-Path -Path $installDir)) {
    New-Item -ItemType Directory -Path $installDir
}

# ############################################################
# Check for the latest version
# ############################################################
Write-Output "Checking for the latest Terraform version..."
$latestVersionResponse = Invoke-RestMethod -Uri $latestReleaseUrl
$latestVersion = $latestVersionResponse.tag_name.TrimStart("v")

Write-Output "Latest Terraform version is $latestVersion"

# ############################################################
# Construct the download URL for the Windows 64-bit binary
# ############################################################
$downloadUrl = "

# ############################################################
# Define the paths for the downloaded zip file and the executable
# ############################################################
$zipPath = "$installDir\terraform.zip"
$exePath = "$installDir\terraform.exe"

# ############################################################
# Download the latest Terraform zip file
# ############################################################
Write-Output "Downloading Terraform $latestVersion..."
$client = New-Object System.Net.WebClient
$client.DownloadFile($downloadUrl, $zipPath)

# ############################################################
# Extract the Terraform executable
# ############################################################
Write-Output "Extracting Terraform executable..."
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $installDir)

# ############################################################
# Cleanup the zip file
# ############################################################
Remove-Item -Path $zipPath

# ############################################################
# Add Terraform to the system PATH environment variable
# ############################################################
$currentPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
# Add the new path to the current PATH
$newPath = "$currentPath;$installDir"
# Set the system PATH environment variable
[System.Environment]::SetEnvironmentVariable("Path", $newPath, [System.EnvironmentVariableTarget]::Machine)

# ############################################################
Write-Output "Terraform $latestVersion installation completed."
# Write-Output "You may need to restart your terminal or system for the changes to take effect."

As you can see the PowerShell script has comments, so you should be able to tell all the steps and what they are doing. I hope you find this script as useful as I have in any efforts where you need to automate the complete installation of Terraform on a Windows or Windows Server machine.

Happy Terraforming!

About the Author:

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.

Microsoft MVP
HashiCorp Ambassador

Categories:
HashiCorp Terraform DevOps PowerShell



Source link

Read more

Local News