Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I roll back the Terraform state of my config in S3?

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:

  1. 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
    
  2. 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?

like image 696
Evan Kroske Avatar asked Nov 08 '17 00:11

Evan Kroske


People also ask

How do you roll back Terraform state?

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.

How do I downgrade my Terraform state?

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.

What happens if you lost Terraform state file?

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.


1 Answers

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

like image 66
BMW Avatar answered Oct 20 '22 00:10

BMW