Install Terraform On Windows And Authenticate Terraform To Azure

Overview

Using tools for infrastructure as code (IaC), you can control infrastructure without a graphical user interface by using configuration files. IaC enables you to define resource configurations that you can version, reuse, and share, allowing you to develop, modify, and manage your infrastructure in a secure, dependable, and repeatable manner.

Install Terraform for Windows

Download Terraform. Select Windows Tab and Download AMD 64.

Install Terraform on Windows and Authenticate Terraform to Azure

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

Install Terraform on Windows and Authenticate Terraform to Azure

Update your system's global path to the executable.

The path is in the registry but usually you edit through this interface:

  • Go to Control Panel -> System -> System settings -> Environment Variables.
  • Scroll down in system variables until you find PATH.
  • Click edit and change accordingly.

Install Terraform on Windows and Authenticate Terraform to Azure

Open power Shell Run as Admin and Verify the global path configuration with the ‘terraform’ command.

terraform -version

Install the Azure CLI tool

You will use the Azure CLI tool to authenticate with Azure.

Install the Azure CLI tool - Open Power shell as Run as Admin and run this,

Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi

Authenticate using the Azure CLI

Terraform must authenticate to Azure to create infrastructure.

Open PowerShell as run as admin and Run az login without any parameters and follow the instructions to sign in to Azure.

az login

Upon successful sign-in, az login displays a list of the Azure subscriptions associated with the logged-in Microsoft account, including the default subscription.

Install Terraform on Windows and Authenticate Terraform to Azure

To confirm the current Azure subscription, run-

az account show

To use a specific Azure subscription, run az account set.

az account set --subscription "<subscription_id_or_subscription_name>"

Create a Service Principal

Make a Service Principal next. A service principal is an Azure Active Directory application that has the authentication tokens required by Terraform to act on your behalf. The subscription ID you gave in the preceding step should be added to the SUBSCRIPTION ID>.

az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/<SUBSCRIPTION_ID>"

Install Terraform on Windows and Authenticate Terraform to Azure

Navigate to Azure portal - Subscription - IAM - Role Assignments, Then you can see the App - Contributor

Install Terraform on Windows and Authenticate Terraform to Azure

Set your environment variables

Instead of storing these values in your Terraform setup, HashiCorp advises defining them as environment variables.

In your Powershell terminal, set the following environment variables. Be sure to update the variable values with the values Azure returned in the previous command.

$Env:ARM_CLIENT_ID = "<APPID_VALUE>"
$Env:ARM_CLIENT_SECRET = "<PASSWORD_VALUE>"
$Env:ARM_SUBSCRIPTION_ID = "<SUBSCRIPTION_ID>"
$Env:ARM_TENANT_ID = "<TENANT_VALUE>"

Install Terraform on Windows and Authenticate Terraform to Azure

That’s all. I will meet with my next blog, write configurations, and build infrastructure—Terraform Azure Example (Resource Group, Virtual Network, and Virtual Machine)

Summary

We learned how to Install Terraform on Windows and Authenticate Terraform to Azure. Please leave a comment in the comment box if you have any questions.


IFS R&D International (Pvt) Ltd
IFS develops and delivers enterprise software for customers around the world