Skip to main content
Now that the pipeline is authored, let’s follow how it runs in a real-world CI/CD flow. Execution is more than running commands — it’s controlled infrastructure delivery with visibility, validation, and governance. This lesson walks through the lifecycle from code change to infrastructure deployment, showing the typical stages, required prerequisites, and example pipeline configuration for Azure DevOps. The flow begins when a developer commits Terraform code to the repository — a new resource, a configuration tweak, a variable change, or a module upgrade. Treating infrastructure as code enables the same review, audit, and automation patterns used for application code. A commit triggers the Azure DevOps pipeline. Triggers can be branch-based (for example, main or dev), pull-request-based, or manual. Azure DevOps provisions an agent (Microsoft-hosted or self-hosted) to run the pipeline job. Typical pipeline sequence:
  • Terraform init — connects to the remote backend, authenticates via the Azure service connection, downloads providers, and locks/prepares the state. If the backend is unreachable, init fails and the pipeline stops.
  • Terraform plan — reads remote state, inspects current infrastructure, and produces an execution plan. This is a read-only validation step; it does not change Azure. In production pipelines, the plan is often saved as a binary artifact using terraform plan -out=<planfile> so reviewers apply the exact reviewed artifact.
  • Approval — governance checkpoint where the plan is reviewed and risk is assessed. Azure DevOps supports manual approvals, environment checks, and role-based approvers.
  • Terraform apply — applies the reviewed plan, updates remote state, and releases the state lock.
The image is a flowchart illustrating an end-to-end CI/CD process, detailing steps from "Code Commit" to "Terraform Apply."
Prerequisites Before running the pipeline end-to-end in Azure DevOps ensure you have:
  • An Azure DevOps organization and project with a repository to store Terraform code.
  • A build agent: Microsoft-hosted or self-hosted (see options below).
  • A remote backend for Terraform state (example: Azure Storage account + container).
  • An Azure DevOps service connection (Service Principal) with least-privilege permissions for the operations your pipeline will perform.
Repository setup
  1. Create an Azure DevOps repository for your Terraform code.
  2. If needed, generate Git credentials (Windows Credential Manager often handles this automatically; macOS/Linux can use a personal access token or SSH key).
  3. Clone the repo locally, make a test change (for example, update README.md), commit and push to verify connectivity and confirm commits are tracked for audit purposes.
The image shows a screenshot of an Azure DevOps repository page titled "Terraform on Azure," including options to clone the repository via HTTPS or SSH and instructions for pushing an existing repository.
Open the repo in your IDE (e.g., Visual Studio Code), edit files, then commit and push. The repo will show the commit history and support code review workflows.
The image shows a Visual Studio Code interface with a README.md file open, displaying markdown instructions for introducing a Terraform project, including sections for Getting Started, Build and Test, and Contribute. The sidebar shows source control options and changes.
Build agents Build agents execute Terraform commands (init, plan, apply) in your pipeline. Choose between: Key self-hosted requirements: Azure CLI, Terraform binary, Git, and any other tooling your pipeline needs.
The image shows an Azure DevOps interface with a repository named "Terraform on Azure" featuring a README.md file. The file includes sections for "Introduction to Terraform," "Getting Started," "Build and Test," and "Contribute."
Creating a self-hosted agent (example using an Azure VM)
  1. Provision an Ubuntu VM in Azure. Configure VM name, resource group, region, size, and authentication (SSH).
  2. SSH into the VM:
  1. Install Azure CLI (official Microsoft method for Debian/Ubuntu):
  1. Install Terraform (HashiCorp APT repository):
  1. Verify installations:
Install and configure the Azure DevOps agent on the VM:
  • In Azure DevOps go to Project settings → Agent pools and create a new pool (for example, Terraform).
  • Register an agent for Linux (or your OS), copy the agent package URL, then download, extract, configure, and run the agent on the VM.
Typical agent setup commands:
During ./config.sh you will:
  • Accept license terms
  • Enter server URL (e.g., https://dev.azure.com/<ORG>/)
  • Provide a Personal Access Token (PAT) generated with agent registration scope
  • Specify the agent pool name (e.g., Terraform)
  • Choose an agent name (or accept the default)
  • Save and run the agent: ./run.sh — the agent will poll for jobs and run them when available.
The image shows a browser window with the Azure DevOps settings page, specifically the "Personal Access Tokens" section. A modal is open for creating a new personal access token, allowing customization of permissions and expiration settings.
Marketplace extension Install the Terraform extension (Terraform Tasks for Azure DevOps) from the Azure DevOps Marketplace to get prebuilt tasks and a tool installer. This simplifies YAML authoring by providing task templates for init/plan/apply.
The image is a screenshot of the Visual Studio Marketplace page for Terraform, an extension by Microsoft DevLabs, with installation details and features listed.
Pipeline YAML You can let Azure DevOps generate starter YAML or author your own. Example pipeline using the self-hosted pool Terraform and the official Terraform tasks:
Notes:
  • pool: Terraform targets the self-hosted agent pool you created.
  • backendServiceArm (for init) and environmentServiceNameAzureRM (for plan/apply) refer to the Azure service connection (example: tf-svc-01).
  • The backend storage account/container/key are provided to the init task so the remote state is configured during pipeline execution.
  • Remove the trigger block if you prefer to run pipelines manually (useful when approvals are required before execution).
Using -auto-approve bypasses interactive confirmation. Ensure you have robust approvals and artifact-based plan reviews before enabling automatic apply in production.
Working Terraform files Add your Terraform configuration to the repo. Example main.tf with a single resource group:
Declare the AzureRM backend in backend.tf (backend details will be provided by the pipeline):
Commit and push these files. When the pipeline runs, Azure Repos will check out the repository into the agent working directory, and the Terraform tasks will run against that workspace. Pipeline execution and output When the pipeline runs on the self-hosted agent it performs:
  • Checkout
  • terraform init to configure the remote backend
  • terraform plan to validate the changes
  • Approval gates (if configured)
  • terraform apply to create/update resources
Example trimmed output from the apply step:
Agent logs show lifecycle activity such as registration, listening for jobs, and job completion:
Once complete, your Azure infrastructure matches the Terraform configuration and the remote state file is updated.
The image shows a Microsoft Azure portal interface for creating a virtual machine, with options for availability zone, security type, image selection, VM architecture, and size.
What’s next?
  • Save the binary plan as a pipeline artifact and require that the exact plan artifact is applied to ensure auditable, deterministic changes.
  • Add approvals, environment checks, and role-based approvers before apply to enforce governance.
  • Harden self-hosted agents: apply least privilege, automate updates, and restrict network access; use Microsoft-hosted agents when possible to reduce maintenance.
  • Extend pipeline tasks with linting (tflint), validation (terraform validate), and automated tests for modules (terratest).
Keep Terraform state in a secured remote backend and ensure your Azure service connection has the minimum required permissions for the operations your pipeline performs.
References This completes an end-to-end example of running Terraform from Azure DevOps using a self-hosted agent. You now have a reproducible CI/CD flow from code commit to infrastructure apply, with checkpoints for visibility and governance.

Watch Video