Traffine I/O

日本語

2023-07-17

Error acquiring the state lock

Error: Error acquiring the state lock

terraform planterraform applyのようなコマンドの実行中に、以下のエラーが発生する場合があります。

bash
$ terraform plan
╷
│ Error: Error acquiring the state lock
│
│ Error message: ConditionalCheckFailedException: The conditional request failed
│ Lock Info:
│   ID:        03d3b219-5ea1-47b9-71a5-3e93e7102fdf
│   Path:      terraform.tfstate
│   Operation: OperationTypeApply
│   Who:       ryuseikakujo@whos-who.local
│   Version:   1.5.2
│   Created:   2023-07-16 19:17:52.561678 +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.

もしDynamoDBでステートロック管理を構成している場合、apply操作中にステートがロックされ、複数のユーザーが同時に処理されないようになっています。通常、applyが完了するとロックは解除されます。しかし、操作が途中で中断されると、ステートファイルがロックされたままになります。

解決策

terraform force-unlockコマンドを使用して、ロックを手動で解除します。

bash
$ terraform force-unlock --help
Usage: terraform [global options] force-unlock LOCK_ID

  Manually unlock the state for the defined configuration.

  This will not modify your infrastructure. This command removes the lock on the
  state for the current workspace. The behavior of this lock is dependent
  on the backend being used. Local state files cannot be unlocked by another
  process.

Options:

  -force                 Don't ask for input for unlock confirmation.

LOCK_IDに入力するIDの値は、エラーメッセージ中で見つけることができます。(私の場合、03d3b219-5ea1-47b9-71a5-3e93e7102fdfです)

bash
$ terraform force-unlock 03d3b219-5ea1-47b9-71a5-3e93e7102fdf

また、各コマンドを-lock=falseオプションを付けて実行することで、ロックを解除することもできます。

bash
$ terraform apply --lock=false

参考

https://stackoverflow.com/questions/62189825/terraform-error-acquiring-the-state-lock-conditionalcheckfailedexception
https://github.com/hashicorp/terraform/issues/14513#issuecomment-465375001

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!