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.
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.
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.
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
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
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.
Keywords : terraform azure Technology
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...
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...
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...
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...
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...