Terraform - Download & Configure on Windows


About Terraform

Terraform is an Infrastructure as Code (IaC) tool from HashiCorp, and it is an open-source, cross-platform and multi-cloud infrastructure deployment tool. It uses HashiCorp Configuration Language (HCL) and supports all popular cloud service providers.

Terraform Installation

Terraform comes as a single binary in a zip archive, you need to download it from the terraform official download page  , extract the archive and you can use it without having it installed and you can also use chocolate  package provider to download the terraform. To use the binary globally you need to set the binary location to the PATH.

Alternatively, the PowerShell script below does all these steps for you and make it ready to run your terraform deployments…

Function Install-Terraform
{
    # Ensure to run the function with administrator privilege 
    if (-not (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
    { Write-Host -ForegroundColor Red -Object "!!! Please run as Administrator !!!"; return }
    
    # Terrafrom download Url
    $Url = 'https://www.terraform.io/downloads.html'

    # Local path to download the terraform zip file
    $DownloadPath = 'C:\Terraform\'

    # Reg Key to set the persistent PATH 
    $RegPathKey = 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment'

    # Create the local folder if it doesn't exist
    if ((Test-Path -Path $DownloadPath) -eq $false) { $null = New-Item -Path $DownloadPath -ItemType Directory -Force }

    # Download the Terraform exe in zip format
    $Web = Invoke-WebRequest -Uri $Url
    $FileInfo = $Web.Links | Where-Object href -match windows_amd64
    $DownloadLink = $FileInfo.href
    $FileName = Split-Path -Path $DownloadLink -Leaf
    $DownloadFile = [string]::Concat( $DownloadPath, $FileName )
    Invoke-RestMethod -Method Get -Uri $DownloadLink -OutFile $DownloadFile

    # Extract & delete the zip file
    Expand-Archive -Path $DownloadFile -DestinationPath $DownloadPath -Force
    Remove-Item -Path $DownloadFile -Force

    # Setting the persistent path in the registry if it is not set already
    if ($DownloadPath -notin $($ENV:Path -split ';'))
    {
        $PathString = (Get-ItemProperty -Path $RegPathKey -Name PATH).Path
        $PathString += ";$DownloadPath"
        Set-ItemProperty -Path $RegPathKey -Name PATH -Value $PathString

        # Setting the path for the current session
        $ENV:Path += ";$DownloadPath"
    }

    # Verify the download
    Invoke-Expression -Command "terraform version"
}

You can run this function any number of times, if terraform is not configured it will download and configure the path, if it is already there it will replace/upgrade with the latest version.

You can visit terrafrom docs  to start with terraform.

To start with Azure deployments using terraform you can visit Microsoft Docs. 


Share it on     |   |   |   | 
  Prev:  

Variable Squeezing in PowerShell

  :Next  

String Operations

comments powered by Disqus