Skip to main content
This guide explains how Terraform is executed inside a CI/CD pipeline—covering the path from a developer commit to a production deployment. It describes the enterprise-grade workflow for Infrastructure as Code (IaC) using Terraform and Azure DevOps, including state management, plan review, approval gates, and secure execution.

Why run Terraform in CI/CD?

Running Terraform inside CI/CD (instead of locally) ensures changes are:
  • Auditable and versioned via Git.
  • Consistently validated and formatted.
  • Reviewed using the exact execution plan that will be applied.
  • Applied by controlled service identities with least-privilege access.
  • Safe from state corruption by using a shared remote backend and locking.
This approach reduces human error, prevents configuration drift, and provides a clear audit trail of what changed, when, and why.

Developer workflow (high level)

  1. Developers write Terraform code locally (resources, modules, variables).
  2. They commit changes to a version-controlled repository (e.g., Azure Repos), which becomes the source of truth.
  3. A CI/CD pipeline is triggered by the commit or a pull request.
  4. The pipeline runs standardized validation, formatting, and planning steps, then publishes the generated plan as an artifact for reviewers.
  5. After review and approval, the pipeline applies the saved plan using the pipeline agent and service connection (not from developer laptops).
  6. Optionally promote using staged environments (dev → staging → production) with gates and automated tests between promotions.

Typical pipeline stages and their purpose

Enforce least-privilege on the service connection used by your pipeline. Avoid using broad contributor permissions; scope the identity to only the required resource groups and actions.

Approval gates and plan review

Before applying changes to sensitive environments, require a plan review step. This is commonly implemented as:
  • The Plan stage publishes the tfplan artifact.
  • A reviewer (senior engineer or platform team) inspects the plan using terraform show -no-color tfplan or Azure DevOps artifact viewer.
  • A manual approval gate (or an automated policy) allows or blocks the Apply stage.
  • The Apply stage downloads the exact saved plan artifact and executes terraform apply -input=false tfplan.
This guarantees the pipeline applies exactly what was reviewed—no surprises or drift between review and execution.

Local commands (for context)

Use these locally for development, formatting, and quick validation. Note: production changes should be applied through CI/CD.

Example Azure DevOps pipeline (simplified)

This YAML demonstrates separation of Validate, Plan, and Apply stages. The Plan stage publishes the tfplan artifact and the Apply stage uses an environment (which can be protected with approvals in Azure DevOps).
Always save and publish the exact tfplan produced by the plan stage and use that saved plan in the apply stage. This ensures reviewers are approving the exact changes that will be applied, preventing drift between review and execution.
The image depicts a DevOps workflow involving a user creating infrastructure as code, storing it in Azure Repos, and passing it through a CI/CD pipeline with approvers, leading to testing, production, planning, and application stages.

Summary

Running Terraform in CI/CD with remote state, state locking, controlled service identities, saved plan artifacts, and approval gates delivers a robust, auditable, and repeatable IaC workflow. This reduces risk, enforces review and compliance, and provides a reliable promotion path from development to production.

Watch Video