Terraform "Saved plan is stale" when applying a plan file in CI
A common CI pattern is plan -out=tfplan in one job and apply tfplan in another. If the state changed in between, the saved plan no longer reflects the current state and Terraform refuses to apply it as stale.
What this error means
apply of a saved plan file fails with "Error: Saved plan is stale" stating the given plan can no longer be applied because the state was changed by another operation.
Error: Saved plan is stale
The given plan file can no longer be applied because the state was changed by
another operation after the plan was created.Common causes
Another apply changed state between plan and apply
A concurrent run or a manual change mutated state after the plan was saved, so the saved plan is out of date.
A long gap let drift or a refresh change the state
Time between the plan and apply stages allowed the recorded state serial to advance, invalidating the plan.
How to fix it
Re-plan, then apply the fresh plan
- Regenerate the plan against current state with plan -out.
- Apply the freshly created plan file immediately.
- Keep plan and apply close together to avoid drift.
terraform plan -out=tfplan
terraform apply tfplanSerialize plan and apply for one state
Gate plan and apply behind a concurrency group so no other run can change state between the two stages.
concurrency:
group: terraform-apply-${{ github.ref }}
cancel-in-progress: falseHow to prevent it
- Run plan and apply in the same locked window per state.
- Avoid concurrent applies against the same backend key.
- Minimize the delay between saving and applying a plan.