How to Build Infrastructure with Terraform in Azure DevOps

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

Microsoft Azure DevOps is a cloud-based service that helps software developers and IT teams easily configure, test, deploy, and monitor apps. In this blog post we will demonstrate how to build infrastructure with Terraform in Microsoft Azure DevOps.

The “azure devops terraform pipeline yaml example” is an article that explains how to build infrastructure with the Terraform tool. It also provides a YAML file that can be used as an example for building infrastructure pipelines in Azure DevOps.

How to Build Infrastructure with Terraform in Azure DevOps

Stop spending time manually maintaining your infrastructure if you’re a DevOps engineer. Instead, let Terraform take care of it!

Terraform is an infrastructure as code (IaC) tool that lets you create, edit, and version infrastructure in a secure and efficient manner. Continue reading to find out how to include Terraform into your Azure DevOps project.

Prerequisites

Here’s what you’ll need to follow along with this tutorial:

It might take up to 2-3 business days for Azure DevOps Parallelism Requests to be granted.

Putting Together a Terraform Configuration

You must first construct a Terraform configuration before you can begin developing infrastructure using Terraform.

A Terraform configuration is a set of one or more configuration files that inform Terraform how to manage your desired infrastructure. JSON and the declarative Hashicorp Control Language (HCL) are the configuration languages for Terraform.

Declarative languages enable you to specify your desired architecture rather than the processes required to construct it. With HCL, you specify the infrastructure you want to build, and Terraform takes care of the rest!

It’s time to start building your Terraform setup! The following examples will utilize a Terraform Azure DevOps project that includes a web application named PartsUnlimited. The source code, unit tests, and a Terraform configuration folder for the example web application will all be included in the sample project.

1. Sign in to your Azure DevOps account and go to the Azure DevOps Demo Generator. If you don’t already have one, create an Azure organization in your Azure DevOps account.

Choose the Terraform template after entering the project name and selecting your Azure organization. Make sure the Replace Tokens and Terraform extensions are installed. Finally, choose Create Project from the drop-down menu.

Creating an example Azure DevOps project Creating an example Azure DevOps project

2. After you’ve finished creating the project, choose Navigate to project.

To go to the Azure DevOps example project, click here. To go to the Azure DevOps example project, click here.

3. Select the Terraform branch from the Repos Branches menu.

Navigating to the 'terraform' branch Getting to the ‘terraform’ branch of the tree

4. Open webapp.tf, the Terraform configuration file written in HCL, by double-clicking on the Terraform folder.

Using Terraform to open a configuration file Using Terraform to open a configuration file

5. Replace the following code in the example webapp.tf file. To setup the Microsoft Azure Provider, you must add the Features blocks to the example configuration file. The Terraform state file will be saved remotely in your Azure storage account in this case. An app service plan and the PartsUnlimited web application are the two resources in the setup. The computing resources on which the web application will execute are defined by the app service plan.

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}” }

It’s worth noting that the prefix and suffix ‘__’ appear in several of the entries in this configuration file. These are just placeholders.

Using an Azure CI Pipeline to publish the Terraform Configuration

It’s now time to create the continuous integration (CI) pipeline! When code is sent to a repository, a continuous integration pipeline automates code integration by performing automated tests. The Terraform configuration you defined in the previous step will be published as a build artifact in your CI pipeline.

A compressed copy of your Terraform configuration is included in the build artifact. By publishing the configuration to a build artifact, the pipeline will save the precise version that you provide. Pulling the configuration straight from your repository, as an alternative, is not desirable since other users may modify it.

1. Go to Pipelines Pipelines in your Azure DevOps project.

To go to the Azure DevOps Pipelines, go to the Azure DevOps Pipelines page. To go to the Azure DevOps Pipelines, go to the Azure DevOps Pipelines page.

2. The example project has its own continuous integration workflow. Click Pipelines and then Terraform-CI to go to this pipeline.

Terraform's continuous integration pipeline is now open. Terraform’s continuous integration pipeline is now open.

3. In the upper right corner, choose Edit.

The Terraform continuous integration pipeline is being modified. The Terraform continuous integration pipeline is being modified.

The pipeline tasks for Agent job 1 in the example project will be presented. The tasks are part of the example project and are.NET core tasks. The purpose of these tasks is to rebuild, test, and publish the example web application once the dependencies have been restored. This web application will then operate on the Terraform architecture.

4. Select the job. Terraform files should be copied to artifacts. This task will convert your Terraform configuration files to a build artifact so that you may subsequently create your infrastructure.

Copying the Terraform configuration by editing the job Copying the Terraform configuration by editing the job

Your configuration file should be in the specified folder under Source Folder. Make sure the ‘Terraform’ folder is chosen in this case.

$(build.artifactstagingdirectory)/Terraform is defined in this case under Target Folder. The build artifact will be stored in this directory.

build is a build variable. artifactstagingdirectory is the agent’s local path for storing artifact copies.

5. Select the job. Artifact should be published. The Item name field is used to provide the name of the folder in which your artifact will be stored. Drop is the default artifact name, but you may modify it to whatever you want. Confirm that Azure Pipelines is chosen under Artifact publish location.

Editing the task so that the artifacts may be published Editing the task so that the artifacts may be published

6. At the top of the page, click Queue to start the pipeline. As illustrated in the figure below, a dialog box will popup. Click Run at the bottom of the window.

The continuous integration pipeline is being run. The continuous integration pipeline is being run.

7. Select Agent job 1 from the Jobs menu. Remember that the task you initiated in the example project was Agent job 1. Verify that all of the tasks were completed successfully, as shown by a green checkmark. To return to the previous page, click the back arrow.

The pipeline's state in terms of continuous integration The pipeline’s state in terms of continuous integration

8. Go to the items that have been published. Return to Pipelines Terraform-CI and choose the most recently executed task under Runs. Select 1 published; 1 eaten from the Summary menu. One item is published, and one is consumed, according to the text 1 published; 1 devoured. There was just one build artifact in this case.

Getting to the artifacts that have been published Getting to the artifacts that have been published

9. Check that your item is listed under Published. The artifact in this case is in the drop folder, or the folder you selected in step 5. The folder should include a zip file containing the web application as well as your webapp.tf configuration file.

Viewing the items that have been published Viewing the items that have been published

Using an Azure CD Pipeline to build infrastructure with Terraform

It’s time to utilize your Terraform configuration in a continuous delivery (CD) pipeline now that you’ve published it as an artifact.

Terraform will construct the resources you specify in your Terraform setup in the CD pipeline. The web application in the example project will subsequently be deployed using these resources.

The infrastructure is part of the staging environment, which is a simulation of the production environment in which your website will function. You can confirm that your application will operate successfully in the production environment before releasing it to the user by testing it in a staging environment.

1. Go to Pipelines Releases in your Azure DevOps project.

Getting to the new releases Getting to the new releases

2. There is a CD pipeline in the example project. Select Terraform-CD and click Edit to go to this pipeline.

Getting to the pipeline for continuous deployment Getting to the pipeline for continuous deployment

3. Navigate to the pipeline tasks by selecting 1 job, 8 tasks under Dev from the example project’s CD pipeline’s list of stages.

Getting to the tasks in the development stage Getting to the tasks in the development stage

4. To deploy Azure resources, choose the job Azure CLI. Select your Azure subscription from the Azure subscription dropdown menu. The Terraform state file will be saved in the subscription. To authorize the CD pipeline to utilize your subscription, click Authorize and connect in to your Azure account.

Editing the 'Azure CLI to deploy Azure resources' task Changing the task “Azure CLI to deploy Azure resources”

5. Decide on a task To get the storage key, use the Azure PowerShell script. Select Azure Resource Manager from the Azure Connection Type selection. Select your Azure subscription and approve it once again.

Editing the 'Azure PowerShell script to get the storage key' task ‘Azure PowerShell script to acquire the storage key’ task is being edited.

6. Replace tokens in terraform file is the next job. Replace the placeholder values with the relevant variable values using the Replace Tokens extension. Navigate to Pipelines Releases to see the replacement values. Select Terraform-CD as the pipeline, click Edit, and then Variables at the top of the page.

Viewing the variable values that will replace the configuration file's values Viewing the variable values that will replace the configuration file’s values

7. Select the Terraform tool installation job from the drop-down menu. Terraform is installed with this job. Terraform version 0.12.3 will be installed in the example project, as indicated in the figure below.

Editing the Terraform installation job Editing the Terraform installation job

8. Select the Terraform: init task from the drop-down menu. This job runs the terraform init command, which creates a working directory and copies the configuration files into it.

Select and authorize your Azure subscription. Terraform is the default container name in the example project under Container. You may give it whatever name you like, but it must match the value of the container name option in the configuration file. By default, the Terraform state file is named terraform.tfstate under Key. It must be the same as the key value in the configuration file.

Editing the 'Terraform : init' task. Changing the job ‘Terraform: init’.

9. Select the Terraform: plan task from the drop-down menu. The terraform plan command is used in this assignment to construct a plan to change the condition of your resources to fit your intended infrastructure.

Select and authorize your Azure subscription. The provider in the example project is azurerm.

Editing the 'Terraform : plan' task. The assignment ‘Terraform: plan’ is being edited.

Select the Terraform: apply task from the drop-down menu. This job performs the plan prepared in the Terraform: plan task using the terraform apply command.

Select and authorize your Azure subscription. Ensure that -auto-approve is chosen under Additional command options in this example project. This parameter skips the confirmation question, allowing you to completely automate your process.

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

11. Select the Deploy Azure App Service job. Select and authorize your Azure subscription. Remember that you added a zip file for the web application to your CI pipeline as a build artifact? The location to this file is given under Package or folder. This job installs the web application package to a Terraform-provided Azure app service.

Editing the 'Azure App Service Deploy' task. Changing the task ‘Azure App Service Deploy’.

12. Now that you’ve built up your pipeline, it’s time to make a release. Click Save in the upper right-hand corner, then Create release. A dialog box will pop up. Select the build version from your CI pipeline that includes the published artifacts under Artifacts. Navigate to Pipelines Pipelines to see all the build versions to discover the build version. Select Create from the drop-down menu.

Making a press release Making a press release

13. Go to Releases in Pipelines. Click on the release you just produced in Terraform-Releases CD’s section. To see the pipeline progress, go to Stages and choose Dev. A green checkmark will appear when the pipeline is successful, as illustrated in the figure below.

Keeping track of the continuous deployment pipeline's progress Keeping track of the continuous deployment pipeline’s progress

14. Go to your Microsoft Azure account and sign in. Go to Home -> Resource Groups -> Home -> Home -> Home -> Home -> Home -> Home The terraform resource should be selected first, followed by the terraform storage container. The terraform.tfstate file will be saved here using your Azure subscription, as indicated in the pipeline.

Using the Azure subscription to see the Terraform state file Using the Azure subscription to see the Terraform state file

15. It’s now time to put your infrastructure to the test! Navigate to the PULTerraform resource group under Resource Groups. Click on the web application resource under Resources, as seen in the figure below.

Getting to the web-based application Getting to the web-based application

16. Select Browse from the menu. Your web application will launch in a new tab in your browser, operating on the Terraform-built infrastructure!

The current state of the web application The current state of the web application

Conclusion

You learnt how to incorporate Terraform into your Azure DevOps project to automate infrastructure management during this course. Save time the next time you need to construct infrastructure by letting Terraform do it.

What infrastructure do you want to develop using Terraform now that you’ve seen how useful it can be?

Terraform is an infrastructure as code (IaC) tool that lets you create, edit, and version infrastructure in a secure and efficient manner. Continue reading to find out how to include Terraform into your Azure DevOps project.

Terraform is an infrastructure as code (IaC) tool that lets you create, edit, and version infrastructure in a secure and efficient manner. Continue reading to find out how to include Terraform into your Azure DevOps project.

Terraform is a tool that can be used to create and manage infrastructure in Azure DevOps. It has been designed for automation, scalability, and repeatability. The “terraform destroy azure devops pipeline” command will destroy the pipeline with all its associated resources.

Frequently Asked Questions

How do I use Terraform with Azure DevOps?

A: Terraform is a tool for creating and managing infrastructure with configuration files. Azure DevOps provides many cloud services that can be managed using Terraform, such as GitLab CI/CD or any other custom scripts you may have written in your build pipelines. Using the Azure CLI to connect to an instance of this service, you would set up the connection like so:

How do you deploy Azure infrastructure using Terraform?

A: Terraform is a tool in Azure that lets you define your infrastructure and automate the creation of it. You can also use this to manage existing resources, like creating new IaaS VMs or updating their specs.

How do I add a Terraform plugin in Azure DevOps?

A: In order to add a Terraform plugin, you first need to create the .tf file. This can be done by either downloading one of our pre-built templates for your language, or creating it from scratch. Once your template is created and saved in templates/terraform, then you will want to use Visual Studio Code (or another code editor) as an IDE that supports loading .TF files with relative paths so you are able get up and running quickly!

Related Tags

  • terraform azure devops best practices
  • azure devops infrastructure as code
  • azure terraform examples
  • azure devops terraform github
  • azure devops terraform medium

Table of Content