TecBrix

Terraform for Azure DevOps Implementation

terraform for azure devops

Infrastructure-as-a-code, collaboration, and automation are the three main components that shape the entire framework of DevOps culture. DevOps professionals achieve their primary goal of streamlining some mundane and repetitive tasks or workflows with great ease by deploying infrastructure on a cloud platform as a code. The remarkable utility of IaaC frameworks like Terraform and its cross-over with Azure gives rise to one of the most discussed topics in the tech world, “Terraform Azure DevOps.”

But what makes Terraform Azure DevOps one of the most preferable combos for DevOps professionals? In this article, we will discuss how Terraform integrates with Azure to give digital birth to DevOps. Let’s discuss how Terraform Azure DevOps framework is set up and aligned to enable development and operations teams to work in close collaboration with maximum efficiency.

Terraform for Azure DevOps

Hashicorp’s Terraform is an IaaC resource used to develop, deploy, and manage infrastructure using HCL (Hashicorp Configuration Language). It can be used to deploy infrastructure on cloud networks, including Microsoft Azure Cloud, Google Cloud, Amazon Cloud (AWS), Oracle Cloud, etc, with full-fledge functionalities and support.

Also Read: What is the Use of Terraform in DevOps

All these cloud providers offer their own set of protocols and configuration languages to allow infrastructure deployment. In Terraform Azure DevOps, our focused framework is Microsoft Azure. In Azure, we can use JSON, AWS, & GCP as human-readable YAML (Yet Another Markup Language).

With Terraform, we are restricted to using HCL instead of Azure Resource Manager’s (ARM), which uses JSON language. HCL supports cross-platform infrastructure deployments and management (as code) on different cloud services.

Terraform Services

The primary differentiating factors between Terraform & ARM are:

  • Terraform is based on HCL, and ARM uses JSON for infrastructure deployment
  • Terraform offers a built-in and automated dependency. Whereas ARM is based on explicit dependencies and needs to be called separately.
  • Terraform use of variables in the ARM template, it’s been referred to as parameters. The values from the user for deploying the template and defined as a separate file.
  • Terraform offers different local variables. Whereas ARM templates itself used as variables with pre-defined values.
  • Terraform offers different modules. The ARM template serves as the Nested template (templates within the templates).

Important Commands/Tasks to Implement Terraform template

The following hierarchy of steps is intended to deploy the terraform template:

  • Terraform init – The init command deals with all .tf file templates in the existing directory. It streamlines the calling and downloading of Azure cloud resources mandatory for infrastructure provisioning.
  • 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 – This step creates all the required resources that align with the file configuration and defines Tfstate. The nature of approval is not restricted to manual deployment approval. Hence it’s called Terraform apply auto-approval.
  • Terraform destroy – This command is used to destroy or vanish the configuration created for the tfstate template.

Resource Required for Complete Terraform Azure Deployment

  1. Microsoft Visual Studio
  2. Microsoft Azure’s Account Subscription
  3. Azure DevOps functionalities turned to ENABLED

Deploying Terraform for Azure DevOps – Step-by-Step Guide

The steps used for Azure DevOps deployment and pipelines development are as under:

  1. Sign in to Microsoft Azure DevOps > Go to the existing project or create a new project.
  2. Select “Repos” > Click on the “Create a folder” option > Upload terraform file for the deployment of necessary resources.
  3. In this article, we are going to use Azure Virtual network and deploy resources via a file named BrandName.tf.

BrandName.tf

#############################################################################

#DEFINING 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 = "Country Name"

}

#To create a Azure Virtual network for defined resources

resource "azurerm_virtual_network" "terraform" {

name = "terraform-network"

resource_group_name = azurerm_resource_group.rg.name

location = azurerm_resource_group.rg.location

address_space = ["Your private IP address space with public & private (RFC 1918) addresses"]

}

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 = "Your subnet address"

}

  1. As we declare the resource group as a variable, it is mandatory to create a variable file with the title “network.tfvars” (network; resource group, tf; file extension, vars; for variable). The specification of the file is “resource_group_name = terraform-rg01”
  2. The files we created above must be stored in the same folder for the same file path.
  3. Go to the “Pipelines” section > click “Releases” > Hit the “New” button > Choose newly released pipeline > Hit the “empty job” button.
  4. Give the title for stage “prod” > Hit the “close” button.
  5. Opt for the “Add an artifact” option > Select Azure Repos.
  6. Select a project you created in Azure DevOps in the beginning> Choose the repository having Brandname.tf
  7. Choose the master branch > Select “latest” from the default branch > Hit the “ADD” button.
  8. Choose the “prod” stage > Hit “View Stage Tasks” to access the dashboard of tasks in the pipeline.
  9. Click the “+” button under the “Agent job” section > Find terraform > Click the “Add” button under “terraform tool installer” > Click the “Add” button under terraform three-times to add three tasks
  10. Opt for the Terraform tool installer task and replace its version with 0.12.10
  11. Go for the first Terraform task > Replace its display title to “Terraform init”. Must verify that the resource provider is Azurerm with its template set to “init”> Choose the config directory where Brandname.tf is located.
  12. Select your Azure subscription and allow permission to access it > To store th tf files, select your
    1. Resource group
    2. Blob container
    3. Storage account
  13. Choose the second Terraform task > Replace its display title with “Terraform plan” > Again verify that resource provider is set to Azurerm > For Terraform Plan, declare command as “Plan” > Select the config directory where network.tf is located. Select the Azure subscription model you are using to deploy infrastructure.
  14. Write -var-file=”network.tfvars” -out vnettest.tfplan as additional commands
  15. Now, choose your last Terraform task > Set its display title to “Terraform applies”. Again verify that the resource provider is set to Azurerm > verify command is set to “validate and apply” for Terraform applies > Choose the config directory where Brandname.tf is located.
  16. For additional commands, write vnettest.tfplan. The terraform state file is used as a reference to execution, defined while setting up the second task. Again, select the Azure subscription model you are using for infrastructure deployment.
  17. After choosing and defining all three tasks, Click the “Save” button > Release/launch your Virtual Network.
  18. After the successful launch, visit your Microsoft Azure account. Search with the title of the resource group we defined as a variable file > You will see your virtual network being deployed.

End Words

Terraform Azure DevOps is fueling the development and operation workflows with utmost efficiency and effectiveness. With rich support for a range of cloud services, including Microsoft Azure, Terraform has experienced notable prominence as the IaaC framework. Feel free to contact our Cloud and DevOps expert for further consultation on Azure and Terraform implications for your cloud framework.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>