Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

First steps in Terraform - Azure Setup

·465 words·3 mins· 100 views · 5 likes ·
Azure CLI Azure Cloud Shell Azure DevOps Azure service principal

Today I want to talk to you about terraform, this open-source tool that allows us to define, preview and deploy infrastructure on a multitude of cloud providers such as Azure, AWS, and google, using the HashiCorp Configuration Language (HCL). In this post, I want to show you how to install Terraform on your Windows system and how to configure it to allow resource manipulation in an Azure subscription.

Prerequisites>

Prerequisites #

  • This tutorial assumes that you already have a Microsoft Azure account configured.
Installing Terraform>

Installing Terraform #

To install Terraform, you should perform the following steps.

  1. Download the appropriate package for your operating system, check out this link.
  2. Unzip and copy the single executable file (terraform.exe) into a separate install directory (for example, inside c:\Terraform).
  3. Set the path environment variable with the path to the install directory.
    • Run the command sysdm.cpl and in the Advanced tab, click on Environment variables.
    • Scroll down in system variables until you find PATH and click edit.
    • Add the installation directory of Terraform (c:\Terraform).

4. Verify the installation, you can check the Terraform version by running the following command.

Terraform -version
Terraform -help

Terraform commands

Create an Azure service principal>

Create an Azure service principal #

To enable Terraform to provision resources into your Azure subscription, you should first create an Azure service principal (SP) in Azure Active Directory. This Azure SP grants your Terraform scripts to provision resources in your Azure subscription. Next, I will show you how to create an Azure SP using Azure CLI.

Azure CLI Workaround>

Azure CLI Workaround #

In this case, we will use Azure Cloud Shell, a browser-based shell built into Azure Portal. This allows us to use the Azure command-line tools (Azure CLI and Azure PowerShell) directly from a browser. If you want to know more about Azure Cloud Shell, check out this link To create the service principal, use the following command.

az ad sp create-for-rbac \
--name "SPForTerraformCLI" \
--role "Contributor" \
--scopes="/subscriptions/<subscription ID>"

Azure Terraform SP

Configure the terraform provider>

Configure the terraform provider #

Once the Azure SP has been created, you are ready to create your first terraform file. You must create the file “provider.tf” in your working directory, indicating the provider you will use and the authentication information.

provider "azurerm" {
    version = "~>1.32.0"
    subscription\_id = "" <----------"SubscriptionId"
    client\_id = "" <----------------"appId"
    client\_secret = "" <------------"password"
    tenant\_id = "" <----------------"tenant"
}

**Important: For security reasons, it is not recommended to put identifying information in code in a production environment. This information may be accessible to other people. For production environments, I will show you in future posts how to use environment variables and manage secrets.

Thanks for reading my post. I hope you find it useful.

If you want to know more about Terraform on Azure, check out this link.