---
title: "Gripping Terraform in Azure DevOps to build Infrastructure"
description: "Learn to use Terraform in Azure DevOps to provision infrastructure as code, maintain state, and enhance your environment!"
canonical: "https://adamtheautomator.com/terraform-azure-devops/"
---

# Gripping Terraform in Azure DevOps to build Infrastructure

> Learn to use Terraform in Azure DevOps to provision infrastructure as code, maintain state, and enhance your environment!

Source: https://adamtheautomator.com/terraform-azure-devops/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Gripping Terraform in Azure DevOps to build Infrastructure](https://adamtheautomator.com/wp-content/uploads/2021/11/How-to-Build-Infrastructure-with-Terraform-in-Azure-DevOps.jpg)

# Gripping Terraform in Azure DevOps to build Infrastructure

[![](https://secure.gravatar.com/avatar/30af83d8e9a0cf63ad11cbd348c7fdbcb01d92ac46d70d18c96585b7304f3ced?s=192&d=mm&r=g)Amanda Punch](https://adamtheautomator.com/author/amanda-punch/)24 November 20218 min. read

Categories: [DevOps](/category/devops/)

Tags:[AzureDevops](/tag/azuredevops/)[Terraform](/tag/terraform/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Creating a Terraform in Azure Devops Configuration](#creating-a-terraform-in-azure-devops-configuration)
*   [Publishing the Terraform Configuration via an Azure CI Pipeline](#publishing-the-terraform-configuration-via-an-azure-ci-pipeline)
*   [Building Infrastructure with Terraform via an Azure CD Pipeline](#building-infrastructure-with-terraform-via-an-azure-cd-pipeline)
*   [Conclusion](#conclusion)

If you are a DevOps engineer, stop wasting time manually managing your infrastructure. Instead, let Terraform in [Azure Devops](https://adamtheautomator.com/powershell-pipeline/) manage it for you!

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

[Terraform](https://www.terraform.io/intro/index.html) is an [infrastructure as code (IaC)](https://en.wikipedia.org/wiki/Infrastructure_as_code) tool that allows you to build, change, and version infrastructure safely and efficiently. Keep reading, and you will learn how to integrate Terraform Azure DevOps project.

## Prerequisites

To follow along with this tutorial, here is what you will need:

*   A [Microsoft Azure Account](https://azure.microsoft.com/en-us/free/?v=b&adobe_mc_sdid=SDID%3D02CBDF5A4A132E98-0DCE2E16C7C2B2D9%7CMCORGID%3DEA76ADE95776D2EC7F000101%40AdobeOrg%7CTS%3D1634096769&adobe_mc_ref=https%3A%2F%2Fazuredevopslabs.com%2F): the free trial is sufficient.
*   An [Azure DevOps Account](https://azure.microsoft.com/en-us/services/devops/) with access to hosted parallelism. To gain free access to hosted parallelism, submit an [Azure DevOps Parallelism Request](https://forms.office.com/pages/responsepage.aspx?id=v4j5cvGGr0GRqy180BHbR63mUWPlq7NEsFZhkyH8jChUMlM3QzdDMFZOMkVBWU5BWFM3SDI2QlRBSC4u).

> _The granting of Azure DevOps Parallelism Request can take up to 2-3 business days._

## Creating a Terraform in Azure Devops Configuration

Before you can start building infrastructure with Terraform, you need to create a Terraform configuration.

A Terraform configuration is a document that tells Terraform how to manage your desired infrastructure and consists of one or more configuration files. The configuration languages for Terraform are [JSON](https://www.terraform.io/docs/language/syntax/json.html) and the [Hashicorp Control Language (HCL)](https://www.terraform.io/docs/language/syntax/configuration.html), a declarative language.

> _Declarative languages allow you to describe your intended infrastructure and not the steps to build that infrastructure. You declare the intended infrastructure with HCL, and Terraform takes care of the rest!_

Time to create your Terraform configuration! The examples here will use a sample Terraform Azure DevOps project, which contains a web application called **PartsUnlimited**. The sample project will include the source code, the unit tests, and a starting Terraform configuration folder for the sample web application.

1\. Navigate to the [Azure DevOps Demo Generator](https://azuredevopsdemogenerator.azurewebsites.net/?TemplateId=77382&Name=Terraform) and sign in with your Azure DevOps account. Add an [Azure organization](https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/create-organization?view=azure-devops#create-an-organization) to your Azure DevOps account if you do not already have one.

Enter the project name, select your Azure organization, and choose the **Terraform** template. Ensure the extensions [Replace Tokens](https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens) and [Terraform](https://marketplace.visualstudio.com/items?itemName=ms-devlabs.custom-terraform-tasks) are installed. Finally, click on **Create Project**.

![Creating the Azure DevOps sample project](https://adamtheautomator.com/wp-content/uploads/2021/11/image-340.png)

Creating the Azure DevOps sample project

2\. After creating the project, click on **Navigate to project**.

![Navigating to the Azure DevOps sample project](https://adamtheautomator.com/wp-content/uploads/2021/11/image-341.png)

Navigating to the Azure DevOps sample project

3\. Navigate to **Repos → Branches** and select the **terraform** branch.

![Navigating to the 'terraform' branch](https://adamtheautomator.com/wp-content/uploads/2021/11/image-342.png)

Navigating to the ‘terraform’ branch

4\. Click on the **Terraform** folder and open the file _webapp.tf,_ the Terraform configuration file written in HCL.

![Opening the Terraform configuration file](https://adamtheautomator.com/wp-content/uploads/2021/11/image-343.png)

Opening the Terraform configuration file

5\. Replace the sample _webapp.tf_ file with the following code. You must add the [Features blocks](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs#features) to the sample configuration file to configure the Microsoft Azure Provider. In this example, the [Terraform state file](https://www.terraform.io/docs/language/state/index.html) will be stored [remotely](https://www.terraform.io/docs/language/state/remote.html) in your Azure storage account. The two resources the configuration holds are an [app service plan](https://docs.microsoft.com/en-us/azure/app-service/overview-hosting-plans) and the **PartsUnlimited** web application. The app service plan defines the compute resources for the web application to run on.

```json
terraform {
  required_version = ">= 0.11"
  
  backend "azurerm" {
    storage_account_name = "__terraformstorageaccount__"
    container_name       = "terraform"
    key                  = "terraform.tfstate"
    access_key           = "__storagekey__"
    features{
    }
  }
}

provider "azurerm" {
  features {
  }
}

resource "azurerm_resource_group" "dev" {
  name     = "PULTerraform"
  location = "West Europe"
}

resource "azurerm_app_service_plan" "dev" {
  name                = "__appserviceplan__"
  location            = "${azurerm_resource_group.dev.location}"
  resource_group_name = "${azurerm_resource_group.dev.name}"

  sku {
    tier = "Free"
    size = "F1"
  }
}

resource "azurerm_app_service" "dev" {
  name                = "__appservicename__"
  location            = "${azurerm_resource_group.dev.location}"
  resource_group_name = "${azurerm_resource_group.dev.name}"
  app_service_plan_id = "${azurerm_app_service_plan.dev.id}"
}
```

> _Note that some of the values in this configuration file have the prefix and suffix ‘\_\_’. These are placeholder values._

## Publishing the Terraform Configuration via an Azure CI Pipeline

Now it is time to set up the [continuous integration](https://www.atlassian.com/continuous-delivery/continuous-integration) (CI) pipeline! A continuous integration pipeline automates code integration by running automated tests whenever one pushes code to the repository. You will publish the Terraform configuration you created in the previous section to a build artifact in your CI pipeline.

The build artifact is a compressed copy of your Terraform configuration. The pipeline will store the exact version of the configuration that you specify by publishing it to a build artifact. The alternative, pulling the configuration directly from your repository, is not ideal as other users can change the configuration.

1\. In your Azure DevOps project, navigate to **Pipelines → Pipelines**.

![Navigating to the Azure DevOps Pipelines](https://adamtheautomator.com/wp-content/uploads/2021/11/image-344.png)

Navigating to the Azure DevOps Pipelines

2\. The sample project has its own CI pipeline. To access this pipeline, click **Pipelines** and then **Terraform-CI**.

![Opening the Terraform continuous integration pipeline](https://adamtheautomator.com/wp-content/uploads/2021/11/image-345.png)

Opening the Terraform continuous integration pipeline

3\. Click on **Edit** in the top right corner.

![Editing the Terraform continuous integration pipeline ](https://adamtheautomator.com/wp-content/uploads/2021/11/image-346.png)

Editing the Terraform continuous integration pipeline

The pipeline tasks for the sample project’s **Agent job 1** will be displayed. The tasks are [.NET core](https://www.tutorialsteacher.com/core/dotnet-core#:~:text=NET%20Core%20is%20a%20new,development%20platform%20maintained%20by%20Microsoft.&text=NET%20Core%20Framework%20can%20be,%2C%20microservices%2C%20game%2C%20etc) tasks, part of the sample project. These tasks’ role is to restore the dependencies and build, test, and publish the sample web application. This web application will then run on the architecture built with Terraform.

4\. Click on the task **Copy Terraform files to artifacts**. This job will copy your Terraform configuration files to a build artifact to build your infrastructure later.

![Editing the task to copy the Terraform configuration ](https://adamtheautomator.com/wp-content/uploads/2021/11/image-347.png)

Editing the task to copy the Terraform configuration

Under **Source Folder**, the selected folder should contain your configuration file. In this example, ensure that the _‘Terraform’_ folder is selected.

In this example under **Target Folder,** _$(build.artifactstagingdirectory)/Terraform_ is specified. This directory will store the build artifact.

> _The [build variable](https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services)_ `build.artifactstagingdirectory` _holds the local path on the [agent](https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/agents?view=azure-devops&tabs=browser) that stores copies of artifacts._

5\. Click on the task **Publish Artifact**. The **Artifact name** field is for the **name of the folder that will contain your artifact. The default artifact name is drop, but you can change it to any name. Under Artifact publish location confirm that Azure Pipelines is the selected.**

![Editing the task to publish the artifacts](https://adamtheautomator.com/wp-content/uploads/2021/11/image-348.png)

Editing the task to publish the artifacts

6\. To run the pipeline, click on **Queue** at the top of the page. A dialog window will appear, as shown in the image below. At the bottom of the window, click **Run**.

![Running the continuous integration pipeline.](https://adamtheautomator.com/wp-content/uploads/2021/11/image-349.png)

Running the continuous integration pipeline.

7\. Under **Jobs,** click on the job **Agent job 1**. Recall that **Agent job 1** is the job in the sample project that you triggered. Verify that all the tasks succeeded, indicated by a green checkmark. Click on the back arrow to return to the previous page.

![The status of the continuous integration pipeline](https://adamtheautomator.com/wp-content/uploads/2021/11/image-350.png)

The status of the continuous integration pipeline

8\. Navigate to the published artifacts. Go back to **Pipelines → Terraform-CI,** and under **Runs,** click on the most recently run job. Under **Summary,** click on **1 published; 1 consumed**. The text **1 published; 1 consumed** specifies that one artifact is [published](https://docs.microsoft.com/en-us/azure/devops/pipelines/artifacts/build-artifacts?view=azure-devops&tabs=yaml#how-do-i-publish-artifacts) and one is [consumed](https://docs.microsoft.com/en-us/azure/devops/pipelines/artifacts/build-artifacts?view=azure-devops&tabs=yaml#how-do-i-consume-artifacts). This example only had one build artifact.

![Navigating to the published artifacts](https://adamtheautomator.com/wp-content/uploads/2021/11/image-351.png)

Navigating to the published artifacts

9\. Under **Published,** verify that your artifact is listed. In this example, the artifact is under the **drop** folder or the folder you specified in step 5. The folder should contain a zip file storing the web application and your configuration file, _webapp.tf._

![Viewing the published artifacts](https://adamtheautomator.com/wp-content/uploads/2021/11/image-352.png)

Viewing the published artifacts

## Building Infrastructure with Terraform via an Azure CD Pipeline

Now that you have published your Terraform configuration to an artifact, it is time to use it in a [continuous delivery](https://www.atlassian.com/continuous-delivery/pipeline#:~:text=As%20the%20name%20suggests%2C%20a,make%20their%20way%20to%20production) (CD) pipeline.

In the CD pipeline, Terraform will build the resources you specified in your Terraform configuration. You will then use these resources to deploy the web application in the sample project.

> _The infrastructure built is part of the [staging environment](https://umbraco.com/knowledge-base/staging-environment/) that replicates the production environment where your website will be running. By running your application in a staging environment, you can ensure it will run correctly in the production environment before releasing it to the user._

1\. In your Azure DevOps project, navigate to **Pipelines → Releases**.

![Navigating to the releases](https://adamtheautomator.com/wp-content/uploads/2021/11/image-353.png)

Navigating to the releases

2\. The sample project has its own CD pipeline. To access this pipeline, select **Terraform-CD** and click **Edit**.

![Navigating to the continuous deployment pipeline](https://adamtheautomator.com/wp-content/uploads/2021/11/image-354.png)

Navigating to the continuous deployment pipeline

3\. Navigate to the pipeline tasks by clicking on **1 job, 8 tasks** under **Dev** on the list of stages of the sample project’s CD pipeline.

![Navigating to the tasks in the Dev stage](https://adamtheautomator.com/wp-content/uploads/2021/11/image-355.png)

Navigating to the tasks in the Dev stage

4\. Select the task **Azure CLI to deploy Azure resources**. Under the **Azure subscription** dropdown menu, select your Azure subscription. The subscription will store the Terraform state file. Click **Authorize** and log in to your Azure account to allow the CD pipeline to use your subscription.

![Editing the 'Azure CLI to deploy Azure resources' task](https://adamtheautomator.com/wp-content/uploads/2021/11/image-356.png)

Editing the _‘Azure CLI to deploy Azure resources’_ task

5\. Select the task **Azure PowerShell script to get the storage key**. Under the **Azure Connection Type** dropdown, select **Azure Resource Manager**. Once again, choose your Azure subscription and authorize it.

![Editing the 'Azure PowerShell script to get the storage key' task](https://adamtheautomator.com/wp-content/uploads/2021/11/image-357.png)

Editing the _‘Azure PowerShell script to get the storage key’_ task

6\. The following task is **Replace tokens in terraform file**. The Replace Tokens extension will replace the placeholder values with the appropriate variable values. To check the replacement values, navigate to **Pipelines → Releases**. Select the pipeline **Terraform-CD,** click **Edit,** and select **Variables** at the top of the page.

![Viewing the variable values that will replace the values in the configuration file](https://adamtheautomator.com/wp-content/uploads/2021/11/image-358.png)

Viewing the variable values that will replace the values in the configuration file

7\. Select the **Terraform tool installer** task. This task installs Terraform. In the sample project Terraform version 0.12.3 will be installed, as shown in the image below.

![Editing the task to install Terraform](https://adamtheautomator.com/wp-content/uploads/2021/11/image-359.png)

Editing the task to install Terraform

8\. Select the **Terraform : init** task. This task invokes the [terraform init](https://www.terraform.io/docs/cli/commands/init.html) command that initializes a working directory with the configuration files.

Select your Azure subscription and authorize it. In the sample project under **Container,** the [container name](https://www.terraform.io/docs/language/settings/backends/azurerm.html#container_name) is _terraform_ by default. You can replace it with any name, but it must be the same value given to the argument **container\_name** in the configuration file. Under **Key,** the name of the Terraform state file is _terraform.tfstate_ by default. It must match the value of **key** in the configuration file.

![Editing the 'Terraform : init' task.](https://adamtheautomator.com/wp-content/uploads/2021/11/image-360.png)

Editing the _‘Terraform : init’_ task.

9\. Select the **Terraform : plan** task. This task invokes the [terraform plan](https://www.terraform.io/docs/cli/commands/plan.html) command, which creates a plan to alter the state of your resources to match your desired infrastructure.

Select your Azure subscription and authorize it. In the sample project, **azurerm** is the provider.

![Editing the 'Terraform : plan' task.](https://adamtheautomator.com/wp-content/uploads/2021/11/image-361.png)

Editing the _‘Terraform : plan’_ task.

10\. Select the **Terraform: apply** task. This task invokes [terraform apply](https://www.terraform.io/docs/cli/commands/apply.html) command, which executes the plan created in the **Terraform: plan** task.

Select your Azure subscription and authorize it. In this sample project, under **Additional command arguments,** ensure **\-auto-approve** is selected. This argument bypasses the prompt for confirmation so your pipeline can be fully automated.

![Editing the 'Terraform : apply -auto-approve' task.](https://adamtheautomator.com/wp-content/uploads/2021/11/image-362.png)

Editing the _‘Terraform : apply -auto-approve’_ task.

11\. Click on the **Azure App Service Deploy** task. Select your Azure subscription and authorize it. Recall that you published a zip file for the web application to a build artifact in your CI pipeline. Under **Package or folder,** the path to this file is specified. This task deploys this web application package to an Azure app service provisioned by Terraform Azure Devops.

![Editing the 'Azure App Service Deploy' task.](https://adamtheautomator.com/wp-content/uploads/2021/11/image-363.png)

Editing the _‘Azure App Service Deploy’_ task.

12\. With your pipeline setup, it is time to create a [release](https://docs.microsoft.com/en-us/azure/devops/pipelines/release/releases?view=azure-devops). In the top, right-hand corner click **Save** and then click **Create release**. A dialog window will appear. Under **Artifacts,** select the build version from your CI pipeline that contains the published artifacts. To find the build version, Navigate to **Pipelines → Pipelines** to view all the build versions. Click **Create**.

![Creating a release](https://adamtheautomator.com/wp-content/uploads/2021/11/image-364.png)

Creating a release

13\. Navigate to **Pipelines → Releases.** In **Terraform-CD** and under **Releases,** click on the release you just created. Under **Stages,** click on **Dev** to view the pipeline progress. When the pipeline succeeds, it will be indicated by a green checkmark, as shown in the following image.

![Viewing the progress of the continuous deployment pipeline](https://adamtheautomator.com/wp-content/uploads/2021/11/image-365.png)

Viewing the progress of the continuous deployment pipeline

14\. Log in to your Microsoft Azure Account. Navigate to **Home → Resource Groups**. Click on the **terraform** resource and then click on the terraform storage container. As specified in the pipeline, the _terraform.tfstate_ file will be stored here using your Azure subscription.

![Viewing the Terraform state file stored using the Azure subscription](https://adamtheautomator.com/wp-content/uploads/2021/11/image-366.png)

Viewing the Terraform Azure Devops state file stored using the Azure subscription

15\. Now it’s time to see your infrastructure in action! Under **Resource Groups,** navigate to the **PULTerraform** resource group. Under **Resources,** click on web application resource as shown in the following image.

![Navigating to the web application](https://adamtheautomator.com/wp-content/uploads/2021/11/image-367.png)

Navigating to the web application

16\. Click **Browse**. Your web application will open in a new tab on your browser, running on the infrastructure you just built using Terraform!

![The running web application](https://adamtheautomator.com/wp-content/uploads/2021/11/image-368.png)

The running web application

## Conclusion

Throughout this tutorial, you have learned how to integrate Terraform into your Azure DevOps project to automate the management of your infrastructure. Next time you need to build infrastructure, save time by letting Terraform Azure Devops manage it for you.

What infrastructure do you plan on building with Terraform now that you know helpful Terraform can be?

[Terraform](https://www.terraform.io/intro/index.html) is an [infrastructure as code (IaC)](https://en.wikipedia.org/wiki/Infrastructure_as_code) tool that allows you to build, change, and version infrastructure safely and efficiently. Keep reading, and you will learn how to integrate Terraform into your Azure DevOps project.

[Terraform](https://www.terraform.io/intro/index.html) is an [infrastructure as code (IaC)](https://en.wikipedia.org/wiki/Infrastructure_as_code) tool that allows you to build, change, and version infrastructure safely and efficiently. Keep reading, and you will learn how to integrate into your Terraform Azure DevOps project.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fterraform-azure-devops%2F&text=Gripping%20Terraform%20in%20Azure%20DevOps%20to%20build%20Infrastructure)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fterraform-azure-devops%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fterraform-azure-devops%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2023/02/terraform-best-practices.jpg)

### [Ensuring Success by Learning Terraform Best Practices](/terraform-best-practices/)

Discover Terraform best practices and ensure that your DevOps environment is being utilized safely and securely to it’s fullest potential!

![](https://adamtheautomator.com/wp-content/uploads/2022/08/Which-Tool-Terraform-vs-Cloudformation-in-the-Fight-for-AWS.jpg)

### [Which Tool? Terraform vs. Cloudformation in the Fight for AWS](/terraform-vs-cloudformation/)

Learn which tool, Terraform vs Cloudformation is right for infrastructure as configuration with AWS in this ATA Learning tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2022/07/A-Definitive-Guide-to-Leveraging-the-AWS-Terraform-IAM-Role.jpg)

### [A Definitive Guide to Leveraging the AWS Terraform IAM Role](/terraform-iam-role/)

Discover this definitive guide to the AWS Terraform IAM role and take your devops game to the next level in this ATA Learning tutorial.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
