terraformのaws_default_network_aclリソースを素人が安易に使ってはいけない

最初組み込んでたんだけど、調べてみるとなんかキワイ。

公式ドキュメント

カッコ書きは、Google翻訳を使いながら、適当に翻訳したものである。(残念英語力なので、間違ってたらゴメンね

https://www.terraform.io/docs/providers/aws/r/default_network_acl.html

概要

Each VPC created in AWS comes with a Default Network ACL that can be managed, but not destroyed. This is an advanced resource, and has special caveats to be aware of when using it. Please read this document in its entirety before using this resource.

AWSで作成された各VPCには、管理可能なデフォルトネットワークACLがありますが、削除することはできません。 これは高度なリソースであり、使用時に注意すべき特別な注意点があります。 このリソースを使用する前に、この文書全体をお読みください。」

にじみ出る玄人向け感。初っ端から、初心者お断りオーラ出してきてる。

The aws_default_network_acl behaves differently from normal resources, in that Terraform does not create this resource, but instead attempts to "adopt" it into management.

aws_default_network_aclは通常のリソースとは異なる動作をします。Terraformは、aws_default_network_aclはこのリソースを作成せず、その代わりに管理下に置くことを試みます。」

繰り返される、aws_default_network_acl氏による、オレは特別だぞアピール

When Terraform first adopts the Default Network ACL, it immediately removes all rules in the ACL. It then proceeds to create any rules specified in the configuration.

「Terraformはまず、デフォルトネットワークACLすべてのルールを直ちに削除します 。 次に、設定されたルールを組み込みます。」

デフォルトのネットワーク ACLって影響が甚大なのに、なんとも思い切った動きをしやがる。か、漢だ…。

This resource treats its inline rules as absolute; only the rules defined inline are created, and any additions/removals external to this resource will result in diffs being shown. For these reasons, this resource is incompatible with the aws_network_acl_rule resource.

「このリソースは、そのインラインルールを絶対のものとして扱います。 インラインで定義されたルールのみが作成され、このリソースの外部で追加/削除が行われると、差分が表示されます。 これらの理由から、このリソースはaws_network_acl_ruleリソースと互換性がありません。」

漢は黙ってインライン定義のaws_default_network_aclパイセンの、aws_network_acl_ruleとは違いますからアピール。そろそろウゼーな、コイツ

デフォルトネットワークACLでのサブネット管理

Within a VPC, all Subnets must be associated with a Network ACL. In order to "delete" the association between a Subnet and a non-default Network ACL, the association is destroyed by replacing it with an association between the Subnet and the Default ACL instead.

VPC内では、すべてのサブネットをネットワークACLに関連付ける必要があります。 サブネットとデフォルト以外のネットワークACLとの関連付けを「削除」すると、サブネットの関連付けはデフォルトACLと差し替えられ、もとの関連付けが破棄されます。」

AWSの仕様上、すべてのサブネットはネットワークACLに紐付けないといけないから、関連付けが明示されないサブネットは、暗黙的にデフォルトネットワークACLに紐付けられるっぽい。そりゃ、そうかって感じである。

When managing the Default Network ACL, you cannot "remove" Subnets. Instead, they must be reassigned to another Network ACL, or the Subnet itself must be destroyed. Because of these requirements, removing the subnet_ids attribute from the configuration of a aws_default_network_acl resource may result in a reoccurring plan, until the Subnets are reassigned to another Network ACL or are destroyed.

「デフォルトネットワークACLを管理する場合、サブネットを「削除」することはできません。 別のネットワークACLに改めて関連付けるか、サブネット自体を破棄する必要があります。 これらの要件のため、subnet_ids属性をaws_default_network_aclリソースの設定から削除すると、サブネットが別のネットワークACLに再度関連付けられるか破棄されるまで、 繰り返し差分が出る可能性があります。」

なんつーか、スッゲーじゃじゃ馬感あるな、コイツ

aws_default_network_acl の設定を削除する

Each AWS VPC comes with a Default Network ACL that cannot be deleted. The aws_default_network_acl allows you to manage this Network ACL, but Terraform cannot destroy it. Removing this resource from your configuration will remove it from your statefile and management, but will not destroy the Network ACL. All Subnets associations and ingress or egress rules will be left as they are at the time of removal. You can resume managing them via the AWS Console.

「各VPCには、削除不可のデフォルトネットワークACLが紐付いている。 aws_default_network_acl を使用すると、このネットワークACLを管理できますが、Terraformは削除までは行なえません。 このリソースを設定から削除すると、stateファイルから削除され、管理下からは外れますが、ネットワークACLは破棄されません。 すべてのサブネットの関連付けとインバウンド/アウトバウンドのルールは、削除時のまま残ります。 その後も、AWS コンソールで管理を再開することができます。」

仕様上、デフォルトネットワークACLの削除はできないから、こんな振る舞いになるらしい。分かるっちゃあ、分かるけど、なんとも微妙な振る舞いである

結論

aws_default_network_aclはterraform上の挙動が複雑なので、安易に使わないほうが良さげ。そもそも、ネットワークACL自体が、AWSでは制約が大きく扱いが難しい。

ネットワークACLの設定を変更したい場合は、aws_network_aclリソースを使って、別途ネットワークACLを定義する。ちゃんと検証してないが、おそらく意図したとおりに動くと思う。少なくとも、aws_default_network_aclよりはマシな挙動をするはずだ。