Piyush Dharmani
Piyush Dharmani

Reputation: 53

how to fix terraform remote state and local state mismatch

my teammate is working with terraform v0.12.28,i started working with him but i was using v0.13.x and made some changes, we use state file which is stored in GCS bucket.

now my teammate asked me to downgrade as he has written some modules which are compatible with v0.12.28,i did downgrade my version but the remote state is still in v0.13.x.

now we are stuck with remote state having higher version. what is the safest way to fix this? we are in initial phase so we can destroy the whole infrastructure but still wanted to know a workaround for this.

Upvotes: 5

Views: 8702

Answers (1)

mariux
mariux

Reputation: 3117

If you have versioning enabled in your backend bucket, I would suggest to

  • recover an older version of the state,
  • fix any diffs (e.g. import created resources since this version), and
  • start working normally from thereon.

Unluckily this is a manual process and afaik there are no terraform commands supporting you in reverting the state to an earlier version.

additional notes

For the future my suggestion would be:

  • Make sure versioning is enabled in your state-backend
  • Make sure to pin your terraform version by specifying it as a constraint.

e.g. by creating a versions.tf:

terraform {
  required_version = "0.12.28"
}

You can also pin provider verisons in there (e.g. if you need to stick with aws provider 2.x for any reason)

terraform {
  required_version = "0.12.28"

  required_providers {
    aws = ">= 2.58, < 3.0"
  }
}

Upvotes: 2

Related Questions