Closed1

[記事未満] OpenTofu で state blob is already locked と言われた

mnodmnod

前提

環境

$ tofu -v
OpenTofu v1.6.0
on linux_arm64
+ provider registry.opentofu.org/hashicorp/azurerm v3.100.0
+ provider registry.opentofu.org/hashicorp/http v3.4.2

バックエンドとして Azure を使っている

terraform {
  required_version = ">=0.12"

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>3.50"
    }
    http = {
      source  = "hashicorp/http"
      version = "~> 3.0"
    }
  }
  backend "azurerm" {
    resource_group_name  = "management"
    storage_account_name = "xxxx"
    container_name       = "terraform-zenn-test"
    key                  = "terraform.tfstate"
  }
}

provider "azurerm" {
  features {}
}
provider http {
}

現象

以下のメッセージが出た

$ tofu apply -lock=false "zenntest.out"
:

│ Error: blobs.Client#PutBlockBlob: Failure responding to request: StatusCode=412 -- Original Error: autorest/azure: Service returned an error. Status=412 Code="LeaseIdMissing" Message="There is currently a lease on the blob and no lea8$
se ID was specified in the request.\nRequestId:329fec96-301e-0045-0537-961cfc000000\nTime:2024-04-24T11:09:21.9120640Z"
│
│
╵
╷
│ Error: Failed to save state
│
│ Error saving state: blobs.Client#PutBlockBlob: Failure responding to request: StatusCode=412 -- Original Error: autorest/azure: Service returned an error. Status=412 Code="LeaseIdMissing" Message="There is currently a lease on the
│ blob and no lease ID was specified in the request.\nRequestId:329fede0-301e-0045-3d37-961cfc000000\nTime:2024-04-24T11:09:22.5525859Z"
╵
╷
│ Error: Failed to persist state to backend
│
│ The error shown above has prevented OpenTofu from writing the updated state to the configured backend. To allow for recovery, the state has been written to the file "errored.tfstate" in the current working directory.
│
│ Running "tofu apply" again at this point will create a forked state, making it harder to recover.
│
│ To retry writing this state, use the following command:
│     tofu state push errored.tfstate
│
╵

言われたとおりにしたがだめだった。

$ tofu state push errored.tfstate
╷
│ Error: Error acquiring the state lock
│
│ Error message: state blob is already locked
│ Lock Info:
│   ID:        308335c9-f575-3f32-ceb5-daf195b45e8a
│   Path:      terraform-zenn-test/terraform.tfstate
│   Operation: OperationTypePlan
│   Who:       docker@9d43bf16a58b
│   Version:   1.6.0
│   Created:   2024-04-24 09:28:07.564728641 +0000 UTC
│   Info:
│
│
│ OpenTofu 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.
╵

対応

強制的にロック解除して対応。

$ tofu force-unlock 308335c9-f575-3f32-ceb5-daf195b45e8a
Do you really want to force-unlock?
  OpenTofu will remove the lock on the remote state.
  This will allow local OpenTofu commands to modify this state, even though it
  may still be in use. Only 'yes' will be accepted to confirm.

  Enter a value: yes

OpenTofu state has been successfully unlocked!

The state has been unlocked, and OpenTofu commands should now be able to
obtain a new lock on the remote state.

その後 state をpush。

$ tofu state push errored.tfstate

tofu state list で反映されたことを確認できた。

このスクラップは12日前にクローズされました