Skip to content

InsightsInfrastructure as Code

Applying the Terraform plan that was reviewed, not a new one

How to save a plan in CI, review it, and apply that exact file after approval, including what a plan file contains, what must match between plan and apply, and what goes wrong when two plans compete for the same state.

Published November 15, 20267 min read

A pull request shows a plan with two resources changing. A reviewer approves it. The merge triggers a pipeline that runs terraform apply -auto-approve, which plans again from scratch. In the hour between review and apply, someone changed a security group by hand and another pull request merged. The second plan is not the one anyone read, and it has nine changes.

Terraform has a mechanism for this: save the plan to a file, review that file, and apply it. This article covers how to wire that into CI, what the file contains, what has to match between the two steps, and the failure modes that remain.

Plan, review, apply the same artifact

terraform plan -out writes the plan to a file. terraform apply with that file performs exactly the operations in it. HashiCorp's apply documentation says Terraform runs a saved plan's operations without prompting for confirmation, and that -auto-approve has no effect, because passing the file counts as the approval. You also cannot add -var or -replace at apply time, since those decisions are already in the file.

plan.sh
terraform init -input=false -lockfile=readonly
terraform plan -input=false -lock-timeout=5m -out=tfplan
terraform show -no-color tfplan > plan.txt
apply.sh
terraform apply -input=false -lock-timeout=5m tfplan

Two small details. Name the file tfplan or similar, not plan.tf: the plan documentation warns that a .tf suffix makes Terraform try to parse it as configuration. And terraform show on the file gives the human-readable plan that the reviewer reads, so the text in the pull request comes from the artifact that will be applied.

pull requestplan -outreviewapply tfplan
fig. 1 — one plan is produced, reviewed and applied; apply never plans again

What a plan file contains

Treat it as a secret. The plan documentation says a saved plan includes the full configuration, planned values, plan options and input variables, and that sensitive values are stored in cleartext even though the terminal output hides them. The terraform show documentation adds that -json output prints sensitive values in plain text.

In practice:

  • Store the plan as a private pipeline artifact with a short retention period, not in a public build log or an attachment on a pull request.
  • Post the redacted text of the plan to the pull request, not the file and not the JSON.
  • Prefer environment variables for provider credentials, so they do not end up in the file as variables. Keeping secrets out of state covers the ephemeral values that reduce what Terraform records at all.

What must match between plan and apply

The part teams skip is that the apply step is not a fresh checkout. HashiCorp's guidance on automating Terraform describes it as archiving the whole working directory after plan, including the .terraform directory that init created, and extracting it at the same absolute path before apply. The reason is that the plan can contain absolute paths to child modules and data files.

Must be the sameWhy
Terraform and provider versionsAn error is produced if Terraform or any plugin is upgraded between plan and apply
Operating system and CPU architectureA plan made on one platform cannot be applied on another, for example Windows to Linux
Working directory path and .terraform contentsThe plan references modules and data files by absolute path
Credentials' target accountTerraform cannot check that plan and apply credentials reach the same account

This is why a committed lock file with hashes for every platform matters here: if the apply job runs init again on a different runner image, it may resolve different bits than the plan did. The safest layout is one job that archives the directory and a second job that restores it, both on the same runner image and with the Terraform version pinned.

.github/workflows/terraform.yml
name: terraform
on:
  pull_request:
  push:
    branches: [main]
 
jobs:
  plan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: terraform init -input=false -lockfile=readonly
      - run: terraform plan -input=false -lock-timeout=5m -out=tfplan
      - run: tar -czf plan.tgz .
      - uses: actions/upload-artifact@v4
        with:
          name: plan-${{ github.sha }}
          path: plan.tgz
          retention-days: 3
 
  apply:
    if: github.ref == 'refs/heads/main'
    needs: plan
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: plan-${{ github.sha }}
      - run: tar -xzf plan.tgz
      - run: terraform apply -input=false -lock-timeout=5m tfplan

The environment: production line is where a required reviewer is configured, so a human approves the specific plan the first job produced. This is a sketch: a real pipeline also needs credentials (see OIDC for CI), the workflow's tool installation and a decision on whether the plan is made on the pull request or after the merge.

Plan on the pull request, or after the merge?

The two choices trade off differently.

  • Plan on the pull request, apply that file after merge. The reviewer sees the real plan, but the plan was computed against the state of that day. If anything else was applied in between, the file is out of date. HashiCorp's guidance is to keep only one plan outstanding at a time, because applying a plan invalidates other plans made against the same state, and those must be recomputed. Its documentation does not give a specific error message for this, so test what your version does rather than assuming.
  • Plan after the merge, hold for approval, then apply. The plan is as fresh as it can be, and the approval is attached to what will actually run. The cost is that the review of the code happened before anyone saw the plan.

A workable compromise: plan on the pull request so the reviewer sees the effect, then re-plan on main after the merge and apply that second file after approval. If the second plan differs from the first, the approver sees a different diff than the reviewer did, and that difference is information.

Detecting "nothing to do"

terraform plan -detailed-exitcode changes the exit codes: 0 means succeeded with no changes, 1 is an error and 2 means succeeded with changes present. Use it to skip the approval step when there is nothing to approve, and to treat 1 as a failed job:

plan-gate.sh
set +e
terraform plan -input=false -detailed-exitcode -out=tfplan
code=$?
set -e
 
case "$code" in
  0) echo "no changes"; echo "changes=false" >> "$GITHUB_OUTPUT" ;;
  2) echo "changes=true" >> "$GITHUB_OUTPUT" ;;
  *) exit "$code" ;;
esac

Without the case, a shell with set -e treats exit code 2 as a failure, and a pipeline that was meant to report "changes pending" goes red for the wrong reason. The same exit code is what scheduled drift detection builds on.

Locks and concurrency

Both commands take the state lock. Set -lock-timeout so a job waits for a running apply rather than failing at once, and avoid -lock=false, which the plan documentation calls dangerous when others may run commands against the same workspace. On the S3 backend, use_lockfile = true stores the lock in the bucket, and DynamoDB-based locking is documented as deprecated; see structuring Terraform for many environments for the backend setup.

Locking protects the state, not your pipeline's logic. Make the CI itself serialise runs per stack, for example with a concurrency group per directory, so two applies do not queue on the lock with stale plans. In GitHub Actions, concurrency with cancel-in-progress: false queues the job rather than cancelling the one in flight.

What goes wrong

  • Apply re-plans anyway. A step that runs terraform apply without a file, -auto-approve or not, plans again. Check the apply job for a missing tfplan argument.
  • The artifact is a world-readable log. Plan text with sensitive values hidden is fine to post. The file and show -json are not.
  • Approval of a stale plan. A plan waiting three days for a reviewer may no longer match reality. Set a short artifact retention and make the job re-plan if the approval comes late.
  • Version drift between jobs. A latest Terraform image in the apply job is the quickest way to produce the upgrade error described above. Pin the version in both.
  • Saved plans and -target. A plan limited with -target is a deliberate partial plan, which Terraform warns about. Keep it out of the normal pipeline.
  • Refresh-only runs. terraform plan -refresh-only updates state to match changes made outside Terraform and cannot be combined with other planning modes. Review it as its own change, not as part of a normal one.

Where to start

  1. Find every pipeline step that runs terraform apply without a plan file.
  2. Pin the Terraform version and commit the lock file with hashes for the runner platform.
  3. Produce tfplan once, archive the working directory including .terraform, and give the artifact a short retention.
  4. Show the reviewer the output of terraform show on that file, and nothing else.
  5. Apply the restored file behind an approval, with -lock-timeout set and one run per stack at a time.
  6. Add -detailed-exitcode so a no-change run needs no approval.

Sources: HashiCorp documentation for terraform plan, terraform apply, terraform show, automating Terraform and the S3 backend.