Introduction
When we use Terraform’s saved plan mode with -refresh=true
enabled, sometimes it might throw an exception with the following message:
│
│ 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.
As the error description suggests, the state was modified after the plan was created. This error can occur for many different reasons: an outside change, resources with dynamic naming using timestamp/random numbers, etc.
Solutions
- Identify the cause and fix it, there are many ways to do that, but one of the simplest is to compare the old plan with the new one.
- Save the current (“old”) state in a plain text file – terraform show -no-color tfplan > tfplan.old.txt
- Export a new terraform plan in a plain text – terraform plan -no-color > tfplan.new.txt
- Find the difference on what was changed your favorite IDE tool or from the CLI – diff tfplan.old.txt tfplan.new.txt
- Skip Terraform Refresh when inheriting Terraform plan
- You will find this option in your Terraform provisioner section, as shown below:
- You will find this option in your Terraform provisioner section, as shown below:
This option will skip the refresh, which Harness does before the apply step; for more details, please refer: Add Terraform Scripts - Harness.io Docs