Closed9

terraformで管理するeventbridgeルールが削除できない

not75743not75743

destroyの様子

コマンドが終了しないため、Ctrl + Cで強制停止しました

$ terraform destroy
// 略
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 1m0s elapsed]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 1m10s elapsed]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 1m20s elapsed]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 1m30s elapsed]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 1m40s elapsed]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 1m50s elapsed]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 2m0s elapsed]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 2m10s elapsed]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 2m20s elapsed]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 2m30s elapsed]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 2m40s elapsed]
^C
Interrupt received.
Please wait for Terraform to exit or data loss may occur.
Gracefully shutting down...

Stopping operation...
╷
│ Error: deleting EventBridge Rule (spot-interruption-rule): ValidationException: Rule can't be deleted since it has targets.
│       status code: 400, request id: 0a9fe7a4-31c2-4352-b860-3d230f8be96f
│ 
│ 
╵
╷
│ Error: execution halted
│ 
│ 
╵
╷
│ Error: execution halted
$

stateを確認してもeventbridgeルールだけ残っています。

$ terraform state list
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule
$

怪しい

この表示が怪しいです

│ Error: deleting EventBridge Rule (spot-interruption-rule): ValidationException: Rule can't be deleted since it has targets.
│       status code: 400, request id: 0a9fe7a4-31c2-4352-b860-3d230f8be96f

ターゲットが存在しているから削除できない!ということでしょうか
うーんでもlambdaはdestroyされてるんだよな

not75743not75743

destroyを再度試す

もう一度destroyをしますが同様に削除できません

$ terraform destroy
// 略
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Destroying... [id=spot-interruption-rule]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 10s elapsed]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 20s elapsed]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Still destroying... [id=spot-interruption-rule, 30s elapsed]
^C
Interrupt received.
Please wait for Terraform to exit or data loss may occur.
Gracefully shutting down...

Stopping operation...
╷
│ Error: deleting EventBridge Rule (spot-interruption-rule): ValidationException: Rule can't be deleted since it has targets.
│       status code: 400, request id: 547b139f-7862-4f4a-a82a-615482f7f5e5
│ 
│ 
╵
╷
│ Error: execution halted
│ 
│ 
╵
╷
│ Error: execution halted
│ 
│ 
╵
not75743not75743

手動でターゲットを登録していた...

EventBridgeルールにターゲット登録されているLambda関数がありました。
過去に手動で追加したものを忘れていた...?
こいつの可能性が高いため、削除してみます。

コンソール上からは削除できなさそうなのでawscliで

# ターゲットIDを調べる
$ aws events list-targets-by-rule --rule spot-interruption-rule

# ターゲットIDを指定して削除する
$ aws events remove-targets --rule spot-interruption-rule --ids "<TargetID>"
not75743not75743

解決

やったね

$ terraform destroy
// 略
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Destroying... [id=spot-interruption-rule]
module.SpotInterruptionNotice.aws_cloudwatch_event_rule.spot-interruption-rule: Destruction complete after 1s

Destroy complete! Resources: 1 destroyed.
not75743not75743

結論

  • Terraform環境で手動でリソース追加した場合、それを忘れないようにしましょう
  • そもそも手動で追加しないようにしましょう

いやー、些細な問題ですがなんとなく思い知りました

このスクラップは2023/06/27にクローズされました