0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Terraform】エラー「Error acquiring the state lock」の原因と対処法

Posted at

Terraformで terraform applyterraform destroy を実行した際、以下のような「State Lock」に関するエラーが出て操作ができなくなることがあります。

Error: Error acquiring the state lock

Error message: state blob is already locked
Lock Info:
  ID:        91b2c3d4-e5f6-4a1b-8c9d-0e1f23456789
  Path:      terraform.tfstate
  Operation: OperationTypeApply
  Who:       user@hostname
  Created:   2024-05-20 10:00:00.123456 +0000 UTC
  Version:   1.9.0
  ...

この記事では、このエラーが発生する原因と、force-unlock コマンドを使用した復旧手順について解説します。

1. エラーの原因

このエラーは、実行中に Ctrl+C で強制終了した等により 「前回のTerraformコマンドが正常に終了せず、State(状態)ファイルのロックが解除されないまま残っている」 ことから発生しているようです。

2. 対処手順

残ってしまったロックを手動で強制解除(Force Unlock)します。

手順 1: Lock ID を特定する

エラーログの中に Lock Info: というセクションがあります。その中の ID をコピーしてください。

今回の例では以下のIDです:
91b2c3d4-e5f6-4a1b-8c9d-0e1f23456789

手順 2: 強制解除コマンドを実行する

ターミナルで以下のコマンドを実行します。

terraform force-unlock <Lock ID>

実行例:

terraform force-unlock 91b2c3d4-e5f6-4a1b-8c9d-0e1f23456789

コマンドを実行すると確認メッセージが表示されます。問題なければ yes と入力して Enter を押してください。

Do you really want to force-unlock?
  Terraform will remove the lock on the remote state.
  This will allow local Terraform commands to modify this state, even though it
  may be still be in use.
  Only 'yes' will be accepted to confirm.

  Enter a value: yes

Terraform state has been successfully unlocked! と表示されれば完了です。

手順 3: 本来のコマンドを再実行する

ロックが解除されたので、失敗していたコマンド(applydestroy)を再度実行してください。

terraform destroy

以上で実行できるはずです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?