Last Update: Nov 19, 2024 | Published: Jan 20, 2022
If you plan to manage and work with Azure along with Terraform, using the Azure provider is a must. The Terraform Azure Provider lets you interact with the many resources supported by Microsoft Azure.
In this ultimate guide, you’re going to learn, step-by-step, about everything you need to know about the Azure Provider and how to use this provider with Terraform to manage your Azure cloud infrastructure.
Let’s get started!
This tutorial comprises step-by-step instructions. If you’d like to follow along, be sure you have the following in place:
sudo curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
Terraform is a tool for building, versioning, and managing the infrastructure in an automated way. Terraform is an Infrastructure-as-Code (IaC) tool that simplifies your infrastructure management by using a simple syntax language called HashiCorp Configuration Language (HCL), which is easier than YAML or JSON formats.
Terraform is used with various cloud providers such as Amazon AWS, Oracle, Microsoft Azure, Google Cloud, and many more.
Terraform has dozens of benefits, and some of the key features are:
Terraform is an easy-to-use tool that generally requires three commands to run: terraform init, terraform plan, and terraform apply. You will learn them later in the guide.
Terraform depends on plugins to interact with cloud providers such as Azure, Google Cloud Platform (GCP), and Oracle. One of the most widely used providers is the Terraform Azure provider. The provider interacts with many resources supported by Azure, such as Azure SQL, Azure Data Factory, Azure Active Directory, and many more.
Terraform uses the Azure provider with proper credentials to authenticate and connect with Azure to manage or deploy/update dozens of Azure services.
Terraform supports multiple methods for authenticating to Azure, such as authenticating to Microsoft Azure using the Azure CLI and Managed Service Identity, etc. But this tutorial will authenticate using Azure CLI. So, let’s dive in.
The easiest way to test Terraform resource management is by authenticating with Azure CLI. If you want to try creating resources on your local machine or in a dev environment, consider using the Azure CLI as your authentication method. Let’s learn how to use Azure CLI and configure it for Terraform.
To install Azure CLI on an Ubuntu machine, click here
az login
az account list
az account set --subscription="SUBSCRIPTION_ID"
terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "=2.46.0" } } } # Configure the Microsoft Azure Provider (azurerm provider) in provider blocks. provider "azurerm" { features {} }
Callout: Find out more about authenticating with Terraform Azure cloud infrastructure.
Previously you learned how to declare the Terraform Azure Provider, which is excellent, but you will be required to manage resources in the Azure cloud.
In the Azure cloud, a resource can be a virtual machine (VM), storage accounts, web apps, databases, virtual networks, etc. Creating resources with Terraform templates is an easy process; let’s learn how to create an Azure resource from a Terraform template.
Terraform templates are configuration files that contain the code, or multiple configuration files, that you use to build or provision resources in the Azure cloud. There are mainly five configuration files that Terraform has: main.tf, vars.tf, provider.tf, output.tf and terraform.tfvars.
The code below will create a Linux virtual machine in Azure with standard_F2 size.
resource "azurerm_linux_virtual_machine" "petri_instance" { name = var.name resource_group_name = var.resource_group location = var.location size = "Standard_F2" } }
The vars.tf file below contains two string variables named location and name.
variable "location" { type = string}variable "name" {}
name = petri_instancelocation = West Europe
To deploy the Terraform files as shown above, below are the commands that you should run in the directory where these files reside:
If you are looking to manage several resources in one go, then you need to create an Azure resource group. Resource groups include all the resources you would like to operate as a group and that you think should be managed together. It becomes easier to deploy, manage, and delete the resources in a group with a resource group.
Now, let’s learn how to create an Azure resource group using the Terraform Azure provider.
mkdir ~/terraform-azure-democd ~/terraform-azure-demo cd ~/terraform-azure-demo
The code below contains the name of a new Azure resource group petri_rg, and its location in West Europe, which specifies where this resource group will be created. While this resource group will be created with the name petri_rg, the instance under this resource group will be tagged with the name petri_instance.
resource "azurerm_resource_group" "petri_rg" { name = petri_rg location = West Europe tags = { InstanceType = petri_instance } }
Now, use the terraform init command shown below to run Terraform.
terraform init
As you can see below, the Terraform has been successfully initialized.
terraform plan
Once the Terraform plan is executed, you will see something like ‘Plan: Number of resources to add, Number of resources to change, and Number of resources to destroy.’
As you can see below, the resources have been successfully deployed as the display shows ‘Apply complete, 1 resource has been added.’
Once the resource is deployed with Terraform, verify it by navigating Azure and checking Resource groups on the main dashboard. You can see below that the specified resource group has been created successfully.
In the previous section, you learned how to create an Azure resource using hardcoded values instead of dynamic variables, but if you need to reuse the variable file across different projects then consider defining them as variables in variable file referencing in the main file and retrieving the values from values file.
Let’s learn some of the built-in variables that are used in Terraform, as shown below.
There are multiple variable types available in Terraform, such as string, list, Boolean, etc. As you can see, the code snippet below contains a string variable.
variable "location" { type = string description = "Azure location where the resource will exist." default = "westus2" }
The label after the variable keyword is a variable that should contain the location, and it should be a unique value in the module. Further, the name is used to assign a value to the variable from outside and to reference the variable’s value from within the module.
Inside the variable block, you have the following arguments (optional) for variable declarations such as:
Up to now, whatever resource or resource group you provisioned in Azure had all the variables with specified values; that is, all the values are hardcoded. But if you need to create dynamic and more flexible Terraform configurations, consider adding variables in your configuration files.
Declaring the variables allows you to share modules across different Terraform configurations, making your module reusable. There are different types of variables used in Terraform, such as Boolean, list, string, maps, etc. Let’s see how different types of terraform variables are declared.
In the code below you will see different types of variables are declared with the following characteristics:
# Declaring the variable1 of type boolean variable "variable1" { type = bool default = false description = "boolean type variable" } # Declaring the variable2 of type map variable "variable2" { type = map default = { us-east-1 = "image-1" us-east-2 = "image2" } } # Declaring the variable3 of type list variable "variable3" { type = list(string) default = [] description = "list type variable" } # Declaring the variable4 of type list(object)) variable "variable5" { type = list(object({ instancetype = string minsize = number maxsize = number
With this ultimate guide, you now have the knowledge you need to work with the Azure Provider, from declaring to executing the Azure Provider within Terraform. You also learned how the Azure Provider allows you to securely declare credentials in many ways.
Now, which Azure service do you have in mind to manage with the Azure Provider and Terraform? If you’re working with AWS resources, you can also check out our separate guide for the Terraform Azure provider.