Install Terraform on Windows with Azure PowerShell

Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.

This article describes how to get started with Terraform on Azure using PowerShell.

In this article, you learn how to:

  • Install the latest version of PowerShell
  • Install the new PowerShell Az Module
  • Install the Azure CLI
  • Install Terraform
  • Understand common Terraform and Azure authentication scenarios
  • Authenticate via a Microsoft account from Cloud Shell (using Bash or PowerShell)
  • Authenticate via a Microsoft account from Windows (using Bash or PowerShell)
  • Create a service principal using the Azure CLI
  • Create a service principal using Azure PowerShell
  • Specify service principal credentials in environment variables
  • Specify service principal credentials in a Terraform provider block

1. Configure your environment

  • Azure subscription: If you don't have an Azure subscription, create a free account before you begin.

2. Install Azure PowerShell

  1. The latest PowerShell module that allows interaction with Azure resources is called the Azure PowerShell Az module. When using the Azure PowerShell Az module, PowerShell 7 (or later) is the recommended version on all platforms. If you have PowerShell installed, you can verify the version by entering the following command at a PowerShell prompt.

    $PSVersionTable.PSVersion
    
  2. Install PowerShell. This demo was tested using PowerShell 7.2.1 (x64) on Windows 10.

3. Install the Azure CLI

For Terraform to authenticate to Azure, you need to install the Azure CLI. This demo was tested using Azure CLI version 2.32.0.

4. Install Terraform for Windows

  1. Download Terraform. This article was tested using Terraform version 1.1.4.

  2. From the download, extract the executable to a directory of your choosing (for example, c:\terraform).

  3. Update your system's global PATH environment variable to include the directory that contains the executable.

  4. Open a terminal window.

  5. Verify the global path configuration with the terraform command.

    terraform -version
    

Troubleshoot Terraform on Azure

Troubleshoot common problems when using Terraform on Azure

Next steps