Nick's Blog

Solve Terraform storage lock error in Azure

Today I’m working on a terraform creation for one of my clients. They using Azure Storage as their terraform backend.

When I was working on the AKS cluster creation, for some reason one of my terraform apply script just hang there. I have nothing to do but just kill the session. But not surprisingly, my terraform script got into the following error.

Error: Error loading state: failed to lock azure state: storage: service returned error: StatusCode=409, ErrorCode=LeaseAlreadyPresent, ErrorMessage=There is already a lease present.
RequestId:e612bef7-601e-0015-448e-25c2fe000000
Time:2019-06-18T04:32:09.2426669Z, RequestInitiated=Tue, 18 Jun 2019 04:32:08 GMT, RequestId=e612bef7-601e-0015-448e-25c2fe000000, API Version=2016-05-31, QueryParameterName=, QueryParameterValue=
Lock Info:
  ID:        a1b4e58c-5c69-160c-7cef-797cac16bdbc
  Path:      STORAGEACCOUNT/BLOBFILE:VERSION
  Operation: OperationTypeApply
  Who:       Nicks-MacBook-Pro.local
  Version:   0.12.2
  Created:   2019-06-18 04:30:59.017852 +0000 UTC
  Info:

So as usual, I did try to unlock for the tfstate by using

terraform force-unlock LOCK_ID

But I still not able to fix the problem, the following error still present.

Failed to load state: failed to lock azure state: storage: service returned error: StatusCode=409, ErrorCode=LeaseAlreadyPresent, ErrorMessage=There is already a lease present.
RequestId:8f55e360-401e-0046-546f-25def1000000
Time:2019-06-18T04:35:45.6470849Z, RequestInitiated=Tue, 18 Jun 2019 04:35:45 GMT, RequestId=8f55e360-401e-0046-546f-25def1000000, API Version=2016-05-31, QueryParameterName=, QueryParameterValue=
Lock Info:
  ID:        e1b4e58c-5c69-160c-7rrf-797cac16bdbc
  Path:      STORAGEACCOUNT/BLOBFILE:VERSION
  Operation: OperationTypeApply
  Who:       MacBook-Pro.local
  Version:   0.12.2
  Created:   2019-06-18 04:30:59.017852 +0000 UTC
  Info:

After a bit of googling, I finally went on to Azure Portal, Go to the Storage Account, Find the Blob > statefile (following by the Path that provide by the error message) > Click Break Lease > And then OK

My terraform works again.