- Lab
 - 
                        Libraries: If you want this lab, consider one of these libraries.
 - Cloud
 
Build an Azure DevOps Pipeline for Terraform
Welcome to the Build an Azure DevOps Pipeline for Terraform lab. In this lab, we will cover six objectives: 1. Create an Azure DevOps Project 1. Prepare the Remote State Backend 1. Connect to the Azure Subscription 1. Create a Planning Pipeline 1. Create an Apply Pipeline 1. Deploy Infrastructure with the Pipelines By the end of this lab, you'll be ready to create and deploy Infrastructure as Code using Azure DevOps and Terraform.
Lab Info
Table of Contents
- 
                
                
Challenge
Create an Azure DevOps Project
In this objective, you will create a new Azure DevOps project and import the existing repository into Azure Repos.
- Create a new account or log into Azure DevOps. NOTE: If you are creating a new account, you may not be allocated any parallel jobs for pipelines. Check the additional information section of the lab for potential workarounds.
 - Create a new private project called 
tacowagon-network. - Inside the new project navigate to Repos.
 - Import the GitHub repository: https://github.com/ned1313/azure-simple-vnet.git
 
 - 
                
                
Challenge
Prepare the Remote State Backend
In this objective, you will retrieve the values to use for setting up the
azurermremote backend for the Terraform configuration. The values will be stored in a variable group in the Pipelines Library.- Log into the Azure CLI or Portal using the provided username and password credentials. (Do not use the service principal)
 - Retrieve the resource group and storage account names from the lab.
 - Retrieve the subscription ID, name, and tenant ID from the lab.
 - Create a variable group in the Pipelines Library called 
state-datawith entries for the following:- Resource group name
 - Storage account name
 - Storage account container name (
tfstate) - Storage account key
 - Client ID (Service Principal - Application Client ID)
 - Client secret (Service Principal - Secret)
 - Subscription ID
 - Tenant ID
 
 
 - 
                
                
Challenge
Connect to the Azure Subscription
In this objective, you will create and verify a service connection between the Azure DevOps project and the Azure Subscription using the provided service principal.
- Navigate to the Project settings.
 - Create a new Service Connection called 
tacowagon-devof type Azure Resource Manager. - Select Service principal (manual).
 - Fill out the fields using the provided service principal and values collected in the previous objective.
 - Verify and save the Service Connection.
 
 - 
                
                
Challenge
Create a Planning Pipeline
In this objective, you will create a pipeline in Azure DevOps that runs when a pull request is created targeting the main branch and produces an execution plan to save for deployment later.
- Install the Terraform extension from the Visual Studio Marketplace.
 - Create a new pipeline using Azure Repos Git and the project's repository.
 - Pick the Starter pipeline.
 - Change the name to 
pr-plan.yml. - Configure the pipeline to trigger on all branches except main.
 - Add the 
state-datavariable group and a local variable with the service connection name. - Create a validation stage which runs a 
terraform fmt -checkandterraform validateagainst the code using theTerraformInstaller@0andbashorpwshtasks. - Create a planning stage that installs Terraform, initializes the configuration, and creates an execution plan saved to a local file. Use the 
TerraformInstaller@0using theTerraformTaskV4@4tasks. - Publish the execution plan file as a pipeline artifact using the 
PublishPipelineArtifact@1task. - Rename the pipeline 
pr-plan. 
 - 
                
                
Challenge
Create an Apply Pipeline
In this objective, you will create a pipeline in Azure DevOps that runs when a commit is made on the main branch, running a
terraform applyaction using the stashed execution plan.- Create a new pipeline using Azure Repos Git and the project's repository.
 - Pick the Starter pipeline.
 - Change the name to 
merge-apply.yml. - Configure the pipeline to trigger only on the main branch.
 - Add the 
state-datavariable group and a local variable with the service connection name. - Create an apply stage that downloads the saved execution plan, installs Terraform, and applies the plan to the Azure subscription. Use the 
DownloadPipelineArtifact@2,TerraformInstaller@0, andTerraformTaskV4@4tasks. - Rename the pipeline 
merge-apply. 
 - 
                
                
Challenge
Deploy Infrastructure with the Pipelines
In this objective, you will promote a change to the Terraform configuration causing both pipelines to run and deploy the VNet in the target resource group.
- Go to the 
state-datavariable group and grant the two pipelines access to the group. - Navigate to the Repositories section of the Project Settings.
 - Add a Build Validation policy for the 
pr-planpipeline. - Navigate to the Repos section of the project.
 - Create a new branch called 
first-deploy. - Edit the 
terraform.tfvarsfile to give theresource_group_namethe correct value. - Commit the edit directly to the 
first-deploybranch. - Create a pull request to merge 
first-deployintomain. - Verify the 
pr-planpipeline fires and completes successfully. Adjust as needed. - Merge the pull request and verify that the 
merge-applypipeline fires and completes successfully. Adjust as needed. 
 - Go to the 
 
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.