Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform remote state push -force fails lineage check for S3 backend #25761

Closed
ConnorGraham opened this issue Aug 6, 2020 · 4 comments · Fixed by #26190
Closed

Terraform remote state push -force fails lineage check for S3 backend #25761

ConnorGraham opened this issue Aug 6, 2020 · 4 comments · Fixed by #26190
Assignees
Labels
bug confirmed a Terraform Core team member has reproduced this issue core v0.12 Issues (primarily bugs) reported against v0.12 releases v0.13 Issues (primarily bugs) reported against v0.13 releases

Comments

@ConnorGraham
Copy link

Terraform Version

v0.12.29

Expected Behavior

The state should be pushed to the remote and forced over any existing state

Actual Behavior

$ tf state push -force B.tfstate
Failed to write state: cannot import state with lineage "c65f30fe-24db-19f7-43c8-bd29fb644be5" over unrelated state with lineage "bfeaceb5-75f6-5a22-5068-bc488243b268"

Steps to reproduce

I am using an s3 backend

terraform {
  backend "s3" {
    bucket = "my-terraform-state"
    region = "us-east-1"
  }
}
terraform workspace new A || terraform workspace select A
terraform apply -auto-approve
terraform state pull > A.state

terraform workspace new B || terraform workspace select B
terraform apply -auto-approve
terraform state pull > B.state

terraform workspace select A
terraform state push -force B.state

References

#5663
#15674
#24916

@ConnorGraham ConnorGraham added bug new new issue not yet triaged labels Aug 6, 2020
@ConnorGraham
Copy link
Author

For those who come across this issue. I worked around it by using jq to manually update the lineage and serial properties

A_LINEAGE=$(cat A.state | jq .lineage)
A_SERIAL=$(cat A.state | jq .serial)
B_LINEAGE=$(cat B.state | jq .lineage)
B_SERIAL=$(cat B.state | jq .serial)

mv A.state temp.json
jq -r ".lineage |= ${B_LINEAGE}" temp.json > A.state
jq -r ".serial |= ${B_SERIAL}" temp.json > A.state

mv B.state temp.json
jq -r ".lineage |= ${A_LINEAGE}" temp.json > B.state
jq -r ".serial |= ${A_SERIAL}" temp.json > B.state

terraform workspace select A
terraform state push B.state

terraform workspace select B
terraform state push A.state

@pselle pselle self-assigned this Aug 11, 2020
@pselle
Copy link
Contributor

pselle commented Aug 12, 2020

@ConnorGraham Not all backends support the -force flag, and I believe S3 is one of these backends.

This feature was added for the Terraform Cloud remote backend in this PR: https://github.com/hashicorp/terraform/pull/24696/files. I'm going to mark this as an enhancement for those who maintain the S3 backend, or should you be interested in implementing this feature for the S3 backend.

@pselle pselle changed the title Terraform remote state push -force fails lineage check Terraform remote state push -force fails lineage check for S3 backend Aug 12, 2020
@pselle pselle removed bug new new issue not yet triaged labels Aug 12, 2020
@pselle pselle removed their assignment Aug 12, 2020
@pselle
Copy link
Contributor

pselle commented Sep 9, 2020

An update here: I believe I made a mistake associating this with the remote backend work, and that this is a bug and should be classified as such. I am updating the issue accordingly, also adding the v0.13 label as this was reported on 0.13 as well.

I was able to replicate the issue you described with the configuration provided.

@pselle pselle added bug core v0.12 Issues (primarily bugs) reported against v0.12 releases v0.13 Issues (primarily bugs) reported against v0.13 releases confirmed a Terraform Core team member has reproduced this issue and removed backend/s3 enhancement labels Sep 9, 2020
@pselle pselle self-assigned this Sep 9, 2020
@ghost
Copy link

ghost commented Oct 13, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked as resolved and limited conversation to collaborators Oct 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug confirmed a Terraform Core team member has reproduced this issue core v0.12 Issues (primarily bugs) reported against v0.12 releases v0.13 Issues (primarily bugs) reported against v0.13 releases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants