Terraform plan: Saved plan is stale
Asked Answered
B

2

9

How do I force Terraform to rebuild its plans and tfstate files from scratch?

I'm considering moving my IAC from GCP's Deployment Manager to Terraform, so I thought I'd run a test, since my TF is pretttty rusty. In my first pass, I successfully deployed a network, subnet, firewall rule, and Compute instance. But it was all in a single file and didn't scale well for multiple environments.

I decided to break it out into modules (network and compute), and I was done with the experiment for the day, so I tore everything down with a terraform destroy

So today I refactored everything into its modules, and accidentally copypasta-ed the network resource from the network module to the compute module. Ran a terraform plan, and then a terraform apply, and it complained about the network already existing.

And I thought that it was because I had somehow neglected to tear down the network I'd created the night before? So I popped over to the GCP console, and yeah, it was there, so...I deleted it. In the UI. Sigh. I'm my own chaos engineer.

Anyway, somewhere right around there, I discovered my duplicate resource and removed it, realizing that the aforementioned complaint about the "network resource already existing" was coming from the 2nd module to run.

And I ran a terraform plan again, and it didn't complain about anything, so I ran a terraform apply, and that's when I got the "stale plan" error. I've tried the only thing I could think of - terraform destroy, terraform refresh - and then would try a plan and apply after that,

I could just start fresh from a new directory and new names on the tfstate/tfplan files, but it bothers me that I can't seem to reconcile this "stale plan" error. Three questions:

  1. Uggh...what did I do wrong? Besides trying to write good code after a 2-hour meeting?
  2. Right now this is just goofing around, so who cares if everything gets nuked? I'm happy to lose all created resources. What are my options in this case?
  3. If I end up going to prod with this, obviously idempotence is a priority here, so what are my options then, if I need to perform some disaster recovery? (Ultimately, I would be using remote state to make sure we've got the tfstate file in a safe place.

I'm on Terraform 0.14.1, if that matters.

Barrel answered 17/12, 2020 at 3:1 Comment(4)
It sounds like you may have some resource in your config that is defined in such a way that it will have constant drift. You can generate two plans and see what differs between the two unexpectedly to identify what in your config is causing the issue.Buggy
@MattSchuchard thanks for the suggestion! When you say "generate two plans," how would I do that? I have only one tfplan file.Barrel
That output file is binary encoded and not really human readable. You would want to generate two plans that are human readable, and not binary encoded to an output file.Buggy
@MattSchuchard alas, "terraform show -json myfile.tfplan" gives me the same "Saved plan is stale" error, does not output any json.Barrel
C
5

Saved plan is stale means out of date. Your plan is matching the current state of your infrastructure. Either the infrastructure was changed outside of terraform or used terraform apply without -save flag.

Way 1: To fix that you could run terraform plan with the -out flag to save the new plan and re-apply it later on.

Way 2: But more easily I would use terraform refresh and after that terraform apply

Coronet answered 12/12, 2022 at 21:45 Comment(2)
I tried to this, but the error is still there.Nevadanevai
terraform refresh is deprecated: developer.hashicorp.com/terraform/cli/commands/refreshHist
M
0

I created the infrastructure via the gcloud CLI first for testing purposes. As soon as it was proven as working, I transferred the configuration to gitlab and encountered the same issue in one of my jobs. The issue disappeared after I changed the network's and cluster's names.

Mystical answered 2/2, 2023 at 9:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.