Skip to main content

Spacelift should take terraform plan and pass into terraform apply to ensure it only plans once

When outputting terraform plan log output into a PR to allow approvers to easily review a PR, we assume these are the changes which are going to be applied to the state file which is being changed. However when the PR is merged into the default branch, the default behaviour of Spacelift is to replan off the default branch and apply those changes.

However, this poses a problem- if there are changes in the environment between the last plan outputting in the PR and the second plan after merge. This could mean that Spacelift is applying changes which have not been approved by the original PR reviewer. In the real world of AWS ClickOps, this is a very real issue where another Engineer could have made manual changes in the case of an incident to mitigate an issue and Spacelift would be reverting these changes.

Having an extra gate after merge and on apply to production environments would be fine at small scale, but not for those customers who are managing large scale infrastructure and want to follow a strict GitOps workflow, it is very important to make things easier to manage, not harder and the audit trail clearer.

Workaround
Problem
Status: ❌ Rejected15 comments

Log in to comment and vote

Comments15

  • Amaranth Dew

    •

    May 6, 2025

    This is essential to workflows and is a standard in terms of CI/CD flows the plan is leveraged as an artifact and applied. There should be no potential for changes to occur between when the plan is run and when the apply occurs.

  • Magenta Sun

    •

    May 7, 2025

    Thanks

    > What would “safe” look like for you?`

    We want to be able to apply the plan that the approver has approved in the MR, we don’t want to have to have another gate in the Spacelift UI (we want autodeploy) as we have a lot of infrastructure and this would be a bit of overhead.

    > Where does current behavior create risk, friction, or delay?
    Currently the plan posted in the MR is not what is actually applied. The second plan after merge to main is what is applied. However this adds adds the extra risk (for us) in the fact that the first plan which is viewed by the MR approver is not necessarily what is applied to the environment. I understand in a perfect world, our terraform would be perfectly idempotent and we would not have ClickOps in our environments- this is not the case, we are every much relying on Spacelift to help get us to this place but right now we cannot guarantee that between MR being opened and approved and then merged into main and replanned that there will not be manual changes in AWS environment for example or anything else.

    > What does a successful, trustable GitOps flow feel like—especially at scale?
    Ensuring the plan in the MR which the approver has reviewed and approved is what is actually applied.

  • Teal Astronaut

    •

    May 23, 2025

    I think it could work like this.

    1. We have a plan in the PR.

    2. When spacelift re-plans, its should compare the 2 plans.

    3. IF they are the same, then go ahead and apply.

    4. IF they are different, then it should stop, and it should be flagged.

  • Black Breeze

    •

    May 7, 2025

    Thanks for raising this—it’s a really important concern.

    You’re absolutely right that Spacelift plans twice, and in many ways, that’s a feature, not a flaw. The second plan ensures that the assumptions made during PR review still hold true at the time of apply. Infrastructure may have changed in the meantime—another PR could’ve landed, or someone may have ClickOps-ed something in response to an incident, as you noted.

    The messy reality is: environments drift, sometimes on purpose. And blindly trusting a stale plan can be just as dangerous as re-applying unreviewed changes.

    That said, if the goal is to make the apply feel as safe and trusted as the plan that was reviewed, maybe the answer isn’t skipping the replan—but making the difference between plans visible. Comparing checksums (or even full diffs) could flag when the apply would do something materially different than what was approved.

    Before we jump into solutions, though—can we step back and clarify the job you’re trying to get done here?

    • What would “safe” look like for you?

    • Where does current behavior create risk, friction, or delay?

    • What does a successful, trustable GitOps flow feel like—especially at scale?

    Understanding this will help us design something that’s both possible and actually solves the right problem.

  • Black Breeze

    •

    May 7, 2025

    Thanks!

    So to make sure we’re on the same page:
    - you don’t mind planning again (given that there’s a real value to it);
    - you’re concerned about the MR plan and the merge plan potentially being different;

    Is that correct?

  • Black Breeze

    •

    May 27, 2025

    Thanks for the detailed discussion on this feature request. I've done some deep research into how the broader Terraform community handles this challenge, and wanted to share what I learned. **The TL;DR: You're not alone in wanting this, but it's fundamentally at odds with how Terraform is designed to work.** The community consensus is clear: reusing PR-generated plans for later applies is inherently fragile because Terraform plans are point-in-time snapshots. Even HashiCorp acknowledges that Terraform "works best in partnership with a human operator" rather than in fully automated workflows. **The core challenges everyone faces:** - **Stale plans**: By merge time, the infrastructure state has often drifted - **Apply ≠ Plan success**: API limits, permissions, timing issues only surface during apply - **Concurrent PRs**: Multiple in-flight changes make any saved plan immediately outdated **What successful teams do instead:** 1. **Re-plan on merge** (most common): Accept that the PR plan was for review only, run fresh plan+apply on main 2. **Apply before merge** (Atlantis-style): Apply changes in the PR, then merge the code to match reality 3. **Strict serialization**: Force PRs to be up-to-date, lock applies, minimize the drift window **Where Spacelift can help:** - Our drift detection catches state changes between plan and apply - Stack locking prevents concurrent modifications - You can already do Atlantis-style workflows with PR comments - Our policies can enforce additional validation before applies **The hard truth**: The workflow you're describing fights against Terraform's fundamental design. Even with perfect tooling, you'd still hit "saved plan is stale" errors whenever state changes between plan and apply. Rather than building features that enable a problematic pattern, we're focused on making the proven patterns (like apply-before-merge or fresh plans) as smooth as possible. What specific pain points are you trying to solve? Maybe we can suggest a better approach that works *with* Terraform rather than against it.
    • Magenta Sun

      •

      May 30, 2025

      Appreciate the feedback, thanks