I've messed up the Terraform state of a config, so I'd like to replace the existing state with a previous version. The state is stored in S3, and I'm using a lock table. Here's what I tried:
I downloaded an old version of the state from S3 and uploaded it to S3, replacing the existing version. Result of tf plan
:
Error: Error loading state: state data in S3 does not have the expected content.
This may be caused by unusually long delays in S3 processing a previous state
update. Please wait for a minute or two and try again. If this problem
persists, and neither S3 nor DynamoDB are experiencing an outage, you may need
to manually verify the remote state and update the Digest value stored in the
DynamoDB table to the following value: XXXXXX
I updated the digest value stored in the DynamoDB table. Result of tf plan
:
Acquiring state lock. This may take a few moments...
Error: Error locking state: Error acquiring the state lock: ConditionalCheckFailedException: The conditional request failed
status code: 400, request id: XXXXXXX
Lock Info:
ID: XXXXXXX
Path: XXXXXX
Operation: OperationTypePlan
Who: XXXXXX
Version: 0.10.8
Created: 2017-11-07 23:54:50.505166265 +0000 UTC
Info:
Terraform acquires a state lock to protect the state from being written
by multiple users at the same time. Please resolve the issue above and try
again. For most commands, you can disable locking with the "-lock=false"
flag, but this is not recommended.
How can I roll back/revert/recover/restore the Terraform state to an older version?
The usual way to represent "rolling back" in Terraform is to put your configuration in version control and commit before each change, and then you can use your version control system's features to revert to an older configuration if needed. Not all changes can be rolled back purely by reverting a VCS change though.
Run the terraform state push command with the -ignore_remote_version option targeting the just-modified state file to upload to the target Workspace. If the file name is sv-zzz. tfstate , this command would look like terraform state push -ignore_remote_version ./sv-zzz.
Or delete them, or modify them – because Terraform compares the cloud infrastructure state with the expected one. And the expected state is kept in… a state file, right. So if the state file gets lost, Terraform will think it never created those resources in the first place and will try to duplicate everything.
Did you try force unlock command? It works for me when dealing with the locking issue.
terraform force-unlock LOCK_ID
The lock id is the id in above output:
ID: XXXXXXX
Updated by @alexsandarT
If this does not work, use terraform force-unlock -force LOCK_ID
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With