Terraform is Infrastructure as Code (IaC) software tool developed by Hashicorp. It’s used to define and provision infrastructure with Hashicorp Configuration Language (HCL). Terraform can be used for deploying infrastructure in the below spaces:
– Azure Cloud
– AWS cloud
– Google Cloud
– Oracle cloud
– VMware vSphere
– Alibaba cloud
– Cisco devices (limited functionalities)
Why do we need Terraform?
Each cloud providers have their method/configuration language for deploying infrastructure, respectively. For Azure, we use JSON, AWS, and GCP as YAML.
Where terraform HCL is cross-platform used to deploy and manage infrastructure as code on any public cloud service. We just need to learn one code language (HCL), and it can be used for any public cloud.
Terraform provides Free and paid plans. The free plan offers the majority of its features as free.
The major difference between Terraform and Azure Resource Manager Templates:
Commands to deploy Terraform template:
To deploy the terraform file, follow the steps below with exact working directory folder:
· Terraform init — This command looks through all of the *.tf files in the current working directory. It automatically downloads any of the providers (Azure or AWS) required for them to provision infrastructure.
· Terraform plan — This command determines what actions are necessary to achieve the desired state specified in the configuration files. This is a dry run and shows which actions will be made.
· Terraform apply – auto-approve — This will create resources as per config file and create Tfstate file which created on the above command, with auto-approval and not prompt for interactive approval for deploy.
· Terraform destroy — This will destroy the configuration created based on the tfstate file.
Figure 1 – Terraform workflow
The things needed for it:
1. Visual Studio code software (Free edition)
2. Azure Subscription access. If not, you can create a free Azure account.
3. An account in Azure DevOps. If not, you can create a new account by log into https://visualstudio.microsoft.com/ and enable Azure DevOps service.
Deploy resources using Terraform in Azure DevOps
Follow the below steps with Azure DevOps and its pipelines.
network.tf
#############################################################################
# VARIABLES
#############################################################################
variable “resource_group_name” {
type = string
}
provider “azurerm” {
version = “=2.0.0”
features {}
}
resource “azurerm_resource_group” “rg” {
name = var.resource_group_name
location = “Australia East”
}
# Create a virtual network within the resource group
resource “azurerm_virtual_network” “terraform” {
name = “terraform-network”
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
address_space = [“10.10.0.0/24”]
}
resource “azurerm_subnet” “app-subnet” {
name = “appsubnet01”
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.terraform.name
address_prefix = “10.10.0.0/25”
}
resource_group_name = terraform-rg01
-var-file=”network.tfvars” -out vnettest.tfplan
I hope this blog provided the learning of how to use Terraform with Azure DevOps Pipelines.
Santhosh has over 15 years of experience in the IT organization. Working as a Cloud Infrastructure Architect and has a wide range of expertise in Microsoft technologies, with a specialization in public & private cloud services for enterprise customers. My varied background includes work in cloud computing, virtualization, storage, networks, automation and DevOps.