Azure DevOps: Deploying Azure Resources using Terraform
By: Arun Kumar Singh
Tags: Azure Devops, Infrastructure As Code, Ci Cd Pipeline, Terraform, Pipeline

In the previous post of this series, I covered the basic standard method to deploy cloud resources using Terraform. In this, post we will discover it using Azure DevOps.

What do you need to start?

Access to Azure Cloud Subscription

Azure DevOps

Please note I assume that you possess basic knowledge of Azure DevOps and Terraform.

Azure DevOps

Azure DevOps is an automation CI/CD tool with a variety of powerful features. Azure DevOps provides multiple integrated features which can help you manage the whole Software Development Cycle.

Azure Repos

Azure Pipelines

Azure Boards

Azure Test Plans

Azure Artifacts

You can work in the cloud using Azure DevOps Services or on-premises using Azure DevOps Server . In this demo, we will use Azure DevOps Services in the cloud.

Terraform LifeCycle

Preparation

What we are planning to do?

I will set up a DevOps pipeline for Terraform-supported Azure Cloud resources deployment. The same task was done manually in the earlier post.

What will be the steps?

Prepare Azure DevOps for use Prepare a DevOps pipeline to deploy Azure Resources using Terraform Verify pipeline run Verify Cloud Resources

Implementation

Prepare Azure DevOps for Terraform Extention

By default, there is a possibility that you don’t find any extension for Terraform available in Azure DevOps. In that case, you need to access Market Place and install the same.

Install process is easy and will not take up a minute. Once you are done, search for the extension in the task sidebar of the Azure Pipelines web editor .

Upload code to Git repo

Azure DevOps support many types of repositories for the pipeline. I will be keeping my codebase in Azure Git.

Repo Structure

DevOps Pipeline Creation

Now we are good to start the pipeline creation part. We will write a YAML pipeline using the Azure Pipelines web editor and save the pipeline in the repository itself. The pipeline will have 2 stages, one for deployment and another for removal.

Both stages will be governed using the parameters and conditions functionality of Azure DevOps. I am putting one more condition to control pipeline run. This condition will ensure that pipeline can be executed using specific branch.

Stage 1: Deployment — TERRAFORM_RUN

This stage will use Terraform Tasks available in Azure DevOps for the execution of steps. ex: init, plan and apply. Terraform task in Azure DevOps is pretty decent and performs well, in not too complex scenarios. If you are not comfortable using it, the option of using bash scripts is always open.

The first task ensures the terraform utility is available on Azure DevOps Agent nodes for execution.

- task: TerraformInstaller@0

inputs:

terraformVersion: '1.0.2'

The second task supports the deployment process taking the required inputs. In the previous post, we used Azure Blob Container as a backend for storing state files. We can pass the same backend details directly here. This task will be used for all of our terraform operations.

- task: TerraformTaskV2@2

displayName: Terraform Init

inputs:

provider: 'azurerm'

command: 'init/apply/plan/destroy etc'

workingDirectory: 'directory where your tf config reside'

backendServiceArm: 'tf backend details'

backendAzureRmResourceGroupName: 'tf backend details'

backendAzureRmStorageAccountName: 'tf backend details'

So stitching all tasks together will give us our first stage of deployment -

Did you notice that I am using Deployment Job. Why? Because I want to use another very useful Azure DevOps Environment concept. The environment allows you to manage Deployment History , Permissions and Traceability of commits and work items on the pipeline.

Please note, As per 2021/sprint-188 update from Microsoft, pipeline runs will not automatically create an environment if it does not exist and if the user context is not known. In such cases, the pipeline will fail with an Environment not found error. You need to pre-create the environments with the right security and checks configuration before using it in a pipeline.

The Pipeline also uses Publish artifact job. I am publishing the terraform plan output for the pipeline run so that it can be downloaded at any time for further assessment.

Stage 2: Destroy — TERRAFORM_DESTROY

The second stage of the pipeline will destroy the config deployed using terraform destroy command. We can use the above-mentioned tasks in this stage as well with few changes.

Execution

Execution of pipeline: Deploy

Now we are at the final step. Execution of pipeline.

As we have selected the option of Deploy, so pipeline will run for the first stage only because of conditional access placed. For me, it took not more than 2 mins to deploy all the required VNet configurations.

In the output section of Azure DevOps, we can see that it has published the artifact as well, which you can download for viewing.

I suggest, visit each step of the pipeline and verify the output.

You can verify the creation of VNet in Azure Portal as well.

Execution of pipeline: Destroy

Now it’s time to test another stage of the pipeline. This stage is similar to the Deploy stage except Destroy command.

TERRAFORM DESTROY

Pipeline execution will run step by step and performed the ops successfully. You can verify the logs by traversing each step output.

Azure DevOps Stage

You can verify the removal of VNet in Azure Portal as well.

That’s all for this post. I will try to cover few more scenarios using Terraform and Azure DevOps in the future. Till then,

Stay Safe and Keep learning. 🌎

PS: If this article has been useful to you, please leave few claps! (you are allowed up to 50)