Using Terraform with Azure

Using Terraform with Azure  Ashish Pandey
Posted on Aug. 6, 2020, 11:19 a.m.

Terraform with Azure - Banner Image

 

What is Terraform?

Terraform is open-source software built by Hashicorp along with the community. The objective is to provide automation for any API-based tools. Since all the cloud service providers expose an API, So Terraform can be used to automate and manage hybrid-cloud and multi-cloud Infrastructure. Having said that Terraform can be used with more than 100 Infrastructure/API providers and works with most of the provisioning and configuration management tool. Hence Terraform is becoming de-facto tools for cloud automation and an integral part of DevOps and Automation landscape. It uses a DSL also known as Hashicorp Configuration Language (HCL), that can be seen as an improvement over JSON. HCL is compatible with JSON but also provides more flexibility such as commenting and custom handler functions. 

 

Terraform with Azure 

Azure provides its own automation mechanism in the form of Azure templates, but since Terraform is more interoperable and widely adopted, a lot of the enterprises are using Terraform in conjunction with Azure DevOps and Azure templates. There are a lot of modules already built for Azure that can be reused to minimize the effort to write terraform scripts. But before we get to Terraform Modules for Azure, let us see how to get started with Terraform for Azure cloud.

 

Getting started with Terraform on Azure Cloud

For Terraform, Azure has three providers (At the time of writing this blog). Azure Active Directory (Azuread), Azure Resource Manager (azurerm), and Azure Stack (azurestack). For Azure Active Directory is meant for provisioning AD hosted on Azure Cloud, Azure RM is to be used for Azure cloud and Azure Stack is to be used for Private or hybrid datacenter (using Azure Stack). In this example, we will go with Azure RM. Details of Azure RM can be found from the official documentation of Azure provided here: 

https://www.terraform.io/docs/providers/azurerm/index.html

There are multiple ways to authenticate and authorize Terraform to access Azure.  Terraform recommends using either a Service Principal or Managed Service Identity when running Terraform scripts as part of CI/CD pipelines - and authenticate using the Azure CLI when running Terraform locally. 

 

Prerequisite of using Terraform with Azure:

  1. You need to have an account on Azure. 
  2. If you don’t have one, you need to get a subscription for it.
  3. You then need to open the Azure portal.
  4. Have an Azure (az) CLI installed on your system
  5. Alternatively, open the cloud shell on the Azure Portal.

 

For Terraform authentication using Azure CLI:

Install az CLI, and use it to log in to the Azure CLI using the command below:

$ az login

Once logged in you can list down the available subscriptions associated with the account:

$ az account list

This shall provide a subscription Id, that needs to be set with az CLI. 

You can specify the Subscription to use via the following command:

$ az account set --subscription="SUBSCRIPTION_ID"

You can further specify subscription and tenant ID in the main.tf script, under provider block - 

provider "azurerm" {

  version = "=1.44.0"

  subscription_id = "00000000-0000-0000-0000-000000000000"

  tenant_id       = "11111111-1111-1111-1111-111111111111"

}

Initialize Terraform by running the below-mentioned command in the same directory 

$Terraform Init

Now you can write the rest of the script and run below-commands to execute the Terraform script

$ Terraform Plan

$ Terraform Apply

 

Benefits of using Terraform with Azure

Using Terraform with Azure enables us to define, provision, and configure Azure resources in a repeatable and predictable manner. So essentially Azure with Terraform provides the following benefits

  1. It lessens the chances for any human error while managing and deploying the infrastructure that was made. 
  2. It will help you deploy a single template multiple times which will let you create identical development, test, and production environments. 
  3. It will also help optimize the infrastructure cost, as you can build and destroy infrastructure quickly whenever you need them.
  4. It helps write the automation script integrating multiple other cloud and service providers.
  5. Supports multiple configuration management tools (aka provisioners) that helps create a CI/CD pipeline and achieve holistic automation.

Along with the above-mentioned benefits of Terraform we need to understand that in this dynamic technological landscape, the infrastructure changes very quickly. Terraform CLI allows the user to have a review and compare the infrastructure changes that were made before applying the script. Being Idempotent Terraform scripts can be applied multiple times without the danger of duplicate provisioning of infrastructure.  Commands such as Terraform Plan, Terraform show, Terraform Import helps understand the actual state of the infrastructure and the changes that will occur after running the script. The changes that will not be required shall also be notified to you as early as possible. 

 

While you are here, do check out zekeLabs training in Terraform and in Microsoft Azure


Ashish Graduated from MNNIT Allahabad in the Computer Science stream. With ~10 years of Corporate experience in companies like Aricent in the telecom domain & Guavus in the big data domain, Ashish brings a lot of insight into Tech Industry Space. Here goes his expertise: Cloud Technologies - AWS, Azure, Docker, Kubernetes, Terraform, DevOps Tools - Chef, Puppet, Ansible, Jenkins, Programming - Python, C, Java. He worked with Nokia Siemens Network regarding Infrastructure Management. Apart from Tech. industry, he is passionate about the social sector.




Keywords : terraform azure Technology


Recommended Reading


Using Terraform with AWS

Terraform is an open-source software made by Hashicorp Inc and the open-source community. It is an Infrastructure-provisioning tool that uses a high-level language. The language which it uses is known as Hashicorp Configuration Language (HCL). Terraform ca...


How to build a Multi-Cloud and Hybrid-Cloud Infrastructure using Terraform?

In the era of cloud-wars, the CIOs often have a hard time adopting a single cloud. Putting all their infrastructure into one cloud is a risky proposition. So the best approach is to use the multi-cloud or hybrid cloud strategy. Having multiple cloud provide...


How to do Cloud Automation using Terraform?

Cloud Automation is coming together of Cloud Computing with Infrastructure Automation. As cloud adoption is accelerated in the industry, the industry needs to automate the management of cloud infrastructure and cloud services. All of the public cloud servic...


Know more about Terraform

Terraform is a tool made by Hashicorp. It is also used as a tool for cloud-automation. It is an open-source software to implement “Infrastructure as Code (IaC)”. The language used to write the terraform script is known as Hashicorp Configuration Language (H...


How to install Kubernetes Clusters Using Terraform?

Kubernetes is a container orchestration platform that can be used to deploy and manage a containerized applications. Generally, Microservices-based applications are first converted into Docker (or other container runtimes) images and then these microservice...