Search code examples
terraformterraform-provider-gcp

Terraform plan: Saved plan is stale


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.


Solution

  • 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