- Lab
- A Cloud Guru
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.
Path 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
azurerm
remote 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-data
with 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-dev
of 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-data
variable group and a local variable with the service connection name. - Create a validation stage which runs a
terraform fmt -check
andterraform validate
against the code using theTerraformInstaller@0
andbash
orpwsh
tasks. - Create a planning stage that installs Terraform, initializes the configuration, and creates an execution plan saved to a local file. Use the
TerraformInstaller@0
using theTerraformTaskV4@4
tasks. - Publish the execution plan file as a pipeline artifact using the
PublishPipelineArtifact@1
task. - 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 apply
action 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-data
variable 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@4
tasks. - 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-data
variable 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-plan
pipeline. - Navigate to the Repos section of the project.
- Create a new branch called
first-deploy
. - Edit the
terraform.tfvars
file to give theresource_group_name
the correct value. - Commit the edit directly to the
first-deploy
branch. - Create a pull request to merge
first-deploy
intomain
. - Verify the
pr-plan
pipeline fires and completes successfully. Adjust as needed. - Merge the pull request and verify that the
merge-apply
pipeline fires and completes successfully. Adjust as needed.
- Go to the
What's a lab?
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.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.