Terraform Apply Fails - Saved plan is stale

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.
    1. Save the current (“old”) state in a plain text file – terraform show -no-color tfplan > tfplan.old.txt
    2. Export a new terraform plan in a plain text – terraform plan -no-color > tfplan.new.txt
    3. 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

This option will skip the refresh, which Harness does before the apply step; for more details, please refer: Add Terraform Scripts - Harness.io Docs

4 Likes