HandsOnAzure

Plan ≠ Apply: The Terraform CI/CD Pattern Most Azure DevOps Pipelines Get Wrong

On this page
  1. Why “plan then apply” is not the same as “apply the plan”
  2. The CI/CD split, done with intent
  3. Two things that bite with the plan artifact
  4. Stop storing the service principal secret
  5. The shape of a pipeline that gets it right

Here is a question that exposes most Terraform pipelines: when you click “approve” on a release, are you approving the plan you reviewed, or a brand new one?

In the majority of Azure DevOps setups I have seen, the answer is “a brand new one.” The pipeline runs terraform plan in a build stage, a human looks at the output, approves the release, and then the release stage runs terraform apply, which re-plans from scratch against whatever the state looks like at that moment. The thing you reviewed and the thing you applied are two different evaluations, separated by minutes, hours, or in some teams, days.

Most of the time it does not bite you. And then one day someone runs a manual change, another pipeline touches shared state, or a data source returns something new between the two runs, and apply does something you never saw in the plan. On infrastructure, that is exactly the surprise you cannot afford.

This article is about closing that gap properly, and about the two other things people get wrong on the same pipeline: the CI/CD split itself, and authentication.

Why “plan then apply” is not the same as “apply the plan”

terraform plan produces a diff between your configuration and the current state. terraform apply, when run with no arguments, produces its own fresh plan and then executes it. Those are two independent calculations.

The fix is built into Terraform and almost nobody uses it: save the plan to a file and apply that exact file.

# In CI — produce a binary plan artifact
terraform plan -out=tfplan

# In CD — apply the exact plan that was reviewed, no re-evaluation
terraform apply tfplan

When you run terraform apply tfplan, Terraform does not re-plan. It executes precisely the actions recorded in that file. If the world has changed since the plan was created (state was modified, a resource drifted), Terraform refuses with “saved plan is stale” and forces you to re-plan. That refusal is not an inconvenience; it is the safety guarantee you actually wanted. You either apply exactly what was reviewed, or you review again. There is no silent third option.

The CI/CD split, done with intent

People say “CI/CD for Terraform” and then build one pipeline that does everything. Separate the two concerns deliberately:

CI (Build): fast, runs on every PR, never touches real infrastructure beyond reading state:

- script: terraform fmt -check -recursive
  displayName: Formatting
- script: terraform init -backend-config=...
  displayName: Init
- script: terraform validate
  displayName: Validate
- script: tflint --recursive
  displayName: Lint
- script: |
    tfsec . --soft-fail=false
  displayName: Security scan
- script: terraform plan -out=tfplan -input=false
  displayName: Plan
- publish: tfplan
  artifact: tfplan
  displayName: Publish plan artifact

CD (Release): gated by an approval, applies the saved plan, nothing else:

- download: current
  artifact: tfplan
- script: terraform init -backend-config=...
  displayName: Init
- script: terraform apply -input=false tfplan
  displayName: Apply reviewed plan

The CD stage does not lint, does not re-plan, does not make decisions. It downloads the artifact, initialises the same backend, and applies. The decision was made in CI and frozen into the artifact.

Put the approval on an Azure DevOps Environment rather than a classic release gate. Environments give you per-environment approval policies, branch controls, and a deployment history you can audit later.

Two things that bite with the plan artifact

The plan file is sensitive. A saved plan contains the full resource diff, including values Terraform marks as sensitive, resolved into plaintext inside the binary. Treat tfplan as a secret. Keep it in the pipeline artifact store with appropriate access, do not email it around, and do not commit it. This is the most overlooked part of the pattern.

The plan is bound to its inputs. The artifact assumes the same provider versions, the same modules, and the same backend state lineage. If your CD stage runs terraform init and pulls newer providers than CI used, you can get mismatches. Commit your .terraform.lock.hcl and use it in both stages so the provider set is identical end to end. (That lockfile discipline is a whole topic of its own, covered in Pin, Don’t Freeze.)

Stop storing the service principal secret

While you are in here, fix authentication. The classic Azure DevOps service connection stores a service principal client secret: a long-lived credential sitting in your project, rotating only when someone remembers to rotate it.

Azure DevOps now supports workload identity federation on the Azure Resource Manager service connection. It uses short-lived OIDC tokens exchanged at runtime, so there is no secret stored anywhere. You configure a federated credential on the app registration that trusts the specific service connection, and the pipeline authenticates with a token that lives for minutes.

The practical effect: nothing to rotate, nothing to leak, and the AzureRM provider picks it up automatically when the pipeline runs under that service connection, with no ARM_CLIENT_SECRET in your variables at all. If you are still pasting SP secrets into variable groups, this is the single highest-value change you can make to the pipeline.

The shape of a pipeline that gets it right

Put together, the rule is simple and the payoff is real:

  • CI validates, scans, and produces a plan artifact, the reviewable unit of change
  • A human approves that artifact, via an Environment gate
  • CD applies the exact saved plan, or fails loudly if the world moved
  • Authentication is federated, so there is no stored secret anywhere in the chain

The version you reviewed becomes the version you ship. That is the entire point of a pipeline, and it is the part most Terraform setups quietly skip.