Terraform: Lock not released when workspace delete fails

Created on 26 Jun 2018  ยท  5Comments  ยท  Source: hashicorp/terraform

Terraform Version

Terraform v0.11.7
+ provider.aws v1.22.0
+ provider.template v1.0.0

Terraform Configuration Files

...

Debug Output

% terraform workspace delete sbx 
Acquiring state lock. This may take a few moments...
Workspace "sbx" is not empty.

Deleting "sbx" can result in dangling resources: resources that
exist but are no longer manageable by Terraform. Please destroy
these resources first.  If you want to delete this workspace
anyway and risk dangling resources, use the '-force' flag.

On the second attempt to the delete the workspace I'm met with this:

terraform workspace delete -force sbx
Acquiring state lock. This may take a few moments...
Error locking state: Error acquiring the state lock: ConditionalCheckFailedException: The conditional request failed
    status code: 400, request id: PEAJIUNB8BCUPH79FQJ8TUAB73VV4KQNSO5AEMVJF66Q9ASUAAJG
Lock Info:
  ID:        3b3eafa9-6c5c-155e-00ea-b7611fdce385
  Path:      path-to-terraform-state/env:/sbx/network.tfstate
  Operation: workspace_delete
  Who:       age@age-VirtualBox
  Version:   0.11.7
  Created:   2018-06-26 07:27:37.633077905 +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.

Crash Output

Expected Behavior


I should be able to re-run terraform on the workspace to clean it up

Actual Behavior


I cannot acquire the lock for the workspace unless I manually delete it from dynamoDB

Steps to Reproduce

  • terraform init
  • terraform workspace new sbx
  • terraform apply --auto-approve
  • terraform destroy --auto-approve
  • terraform delete workspace sbx - fails due to state not being empty
  • terraform delete workspace sbx - fails due to state lock being present.
  • Additional Context

    References

    bug core v0.11

    Most helpful comment

    Fixed by #24085 and will go out in the next 0.12 release.

    All 5 comments

    I ran into this as well when attempting to delete a non-empty workspace. I was able to use terraform force-unlock after I realized what I had done.

    While we _can_ force unlock, the problem is that we shouldn't have to run force-unlock after the operation of destroying the state is finished and exits due to an error. It should unlock itself at the end of the command like other commands do as the user no longer needs the lock.

    It almost looks as if it's this bit of code here: https://github.com/hashicorp/terraform/blob/e68377b1f89df43a2920f0de6080f9d42eb1ca58/command/workspace_delete.go#L109-L123 that is locking the state, then it checks to see if the workspace is empty. I can probably take a better look later, but it might be just as simple as moving this block down below where it checks to see if there are resources within the block, or removing the statelock before exiting within the error checking block right below where I linked.

    Fixed by #24085 and will go out in the next 0.12 release.

    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.

    Was this page helpful?
    0 / 5 - 0 ratings

    Related issues

    ketzacoatl picture ketzacoatl  ยท  3Comments

    zeninfinity picture zeninfinity  ยท  3Comments

    ronnix picture ronnix  ยท  3Comments

    pawelsawicz picture pawelsawicz  ยท  3Comments

    sprokopiak picture sprokopiak  ยท  3Comments