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 aws_security_group のrule設定とどう折り合いをつけるか

Posted at

terraform の aws_security_group の長い注意書き

terraform でsecurity_groupを書いたことがある人なら一度は読んだことあるこのNote

Avoid using the ingress and egress arguments of the aws_security_group resource to configure in-line rules, as they struggle with managing multiple CIDR blocks, and, due to the historical lack of unique IDs, tags and descriptions. To avoid these problems, use the current best practice of the aws_vpc_security_group_egress_rule and aws_vpc_security_group_ingress_rule resources with one CIDR block per rule.

WARNING:
You should not use the aws_security_group resource with in-line rules (using the ingress and egress arguments of aws_security_group) in conjunction with the aws_vpc_security_group_egress_rule and aws_vpc_security_group_ingress_rule resources or the aws_security_group_rule resource. Doing so may cause rule conflicts, perpetual differences, and result in rules being overwritten.

要は以下の二つが書いてあります。

  • aws_vpc_security_group_(egress or ingress)_rule を使ってくれ
  • sg 系統のルールの記述は統一しろ

これ、なんで統一しなければいけないかというとデータの持ち方が違うから重複を検知できないからなんですよね。

security_groupe_rule(以下sgr)を表現するための書き方は現在できるものだと三種類あります。

手法 管理方法
aws_security_group内のingress/egressプロパティに記述 sg名のプロパティとして保存
aws_security_group_rule に記述 sg名とruleの詳細を_区切りで保存
aws_vpc_security_group_(egress or ingress)_rule に記述 security_group_ruleのIDとして保存

最初からsecurity_group_rule(terraform)のid つまりsgrから始まるもので管理していればよかったのですがsgrの導入は実は2021年であり2014年のterraformができた時よりかは遥かに最近の出来事なので古い管理手法と共存しているわけです。

このことにより混在させるとAWS側でたとえ同じものの変更だったとしてもterraform側は別なルールとして認識し不要なsgrを削除して再作成することが発生します。sgrが一瞬でも削除されるわけですから疎通ができないと言ったことが発生しうることになってしまうわけです。

どのようにterraformをより安全に管理するか

sgrで管理した方が他の二つよりも管理しやすいのでこちらに移したいというモチベーションが生まれます。さて、素朴に考えればmovedコマンドを使いたくなりますがmovedコマンドはterraform内のstateの解釈変更でありstateで管理しているプロパティが違うsgrの三種類の管理方法間では互換性はなさそうです。

この時はズバリremovedブロックとimportブロックを使うとうまくいきます。以下のようなコードを書き aws_vpc_security_group_ingress_rule の中身を追加すればうまくいきます。
現代であればClaude Codeにsgrだけxxxにしてという形で指示を出して手動で記述する、もしくは適切な権限でaws コマンドで調査させれば簡単に作ることができます。
https://developer.hashicorp.com/terraform/language
https://developer.hashicorp.com/terraform/language/block/import

removed{
  from =  aws_security_group_rule
  lifecycle {
    destroy = false
  }
}

import {
to = aws_vpc_security_group_ingress_rule
id = "sgr-xxxxx"
}

結局どう折り合いをつけるといいのか

個人的にはaws_vpc_security_group_(egress or ingress)_ruleを使うようにlinterで強制させるのが一番良いと考えています。コードの規模にもよりますが今であればClaude Codeに書き換えをお願いして現実的な工数で書き換えできるようになったところは増えたではと思います。
moduleでaws_security_group_ruleを使っている場合は状況がもう少し複雑にはなりますが、できる限り非推奨なルールから書き換えていくのが一番痛みが少ないのではと考えます。

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?