Terraform

Terraform Cloud利用時のTerraform State Lockの解除方法

開発環境などで気楽にTerraform実行しているときに、「あ、間違えたっ!」とあわてて “Ctrl+C” で強制終了することがあり、終了タイミングによっては、Terraformの状態情報に排他ロックがかかったままになることがあります。

このロックの挙動は、使用されているバックエンドによって異なるらしく、今回はバックエンドにリモート管理の方法の一つである Terraform Cloud を利用したときの、ロック解除方法をメモっておきます。

スポンサーリンク

事象

排他ロックがかかった状態では、以降のコマンドがエラーとなり実行できなくなります。

僕が使用している2つのTerraformバージョン間で、ロックになったときのエラーメッセージの内容が異なっていたため、両方載せておきます。(tgenvtfenvを使用している環境になります。)

古めのTerraform

エラーメッセージに”lock ID” が出力されますが、このIDを用いてロック解除操作を行います Terraformクラウドの場合、組織名/ワークスペース名 がロックIDになるようです。

$ tgenv list
...
* 0.26.7 (set by /Users/zoo200/old/.terragrunt-version)
$
$ tfenv list
...
* 0.13.5 (set by /Users/zoo200/old/.terraform-version)
$
$ terragrunt apply
...
[terragrunt] 2022/02/27 20:57:21 Running command: terraform apply

Error: Error locking state: Error acquiring the state lock: workspace already locked (lock ID: "organization-name/workspace-name")

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.


[terragrunt] 2022/02/27 20:57:25 Detected 1 Hooks
[terragrunt] 2022/02/27 20:57:25 Hit multiple errors:
exit status 1
$

新しめのTerraform

Lock Info に 03c34xxx-yyyy-zzzz などが表示されますが、TerraformCloudの場合、このIDではないですこちらの記事によるとDynamoDBをバックエンドに利用しているときは、このLock InfoのIDがロック解除用のIDになるようです。

$ tgenv list
* 0.31.0 (set by /Users/zoo200/new/.terragrunt-version)
...
$
$ tfenv list
* 1.0.2 (set by /Users/zoo200/new/.terraform-version)
...
$
$ terragrunt apply
...
Acquiring state lock. This may take a few moments...
╷
│ Error: Error acquiring the state lock
│
│ Error message: workspace already locked (lock ID: "organization-name/workspace-name")
│ Lock Info:
│   ID:        03c34xxx-yyyy-zzzz
│   Path:
│   Operation: OperationTypeApply
│   Who:       zoo200
│   Version:   1.0.2
│   Created:   2022-02-27 12:31:59.547666 +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.
╵
ERRO[0013] 1 error occurred:
	* exit status 1

$

ロック解除方法

force-unlock コマンドで解除します。引数にロックIDである、組織名/ワークスペース名 を指定します。

## forceオプションを指定すると yesの入力確認がなくなります
## terragrunt force-unlock -force organization-name/workspace-name
$
$ terragrunt force-unlock organization-name/workspace-name
...
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!

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

ちなみにLock Infoに記載してあるIDを指定すると、以下の通りエラーとなりました。

$ terragrunt force-unlock 03c34xxx-yyyy-zzzz
...
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

Failed to unlock state: lock ID "03c34xxx-yyyy-zzzz" does not match existing lock ID "organization-name/workspace-name"
ERRO[0054] 1 error occurred:
	* exit status 1

$

Tips

おそらくあまり使うことはないと思いますが、Terraform Cloudのコンソール画面でもロックの解除ができます。

ログイン後、当該組織を選択すると以下のように、ロックされている当該ワークスペースの横に鍵アイコンが表示されます。

そのワークスペースをクリック → [ Settings ]タブ → [ Locking ] → [ Unlock organization-name/workspace-name ] → [ Yes,unlock workspace ] を押下することでロック解除できます。

今回は以上です〜ノシ

参考

(*ゝω・)ノ ァリガトネー

公式ドキュメント Command: force-unlock
terraform state lock の解除方法