110. ネットワークACLは番号順の検問所である — ステートレスゆえに戻りの通行証も書く
セキュリティグループの隣にもう1つある防壁「ネットワークACL」を、サブネットの入口に立つ番号順の検問所として図で追っていきます。
① サブネット境界の検問所
- 公式: 「A network ACL allows or denies specific inbound or outbound traffic at the subnet level」
- 公式: 「We evaluate the network ACL rules when traffic enters and leaves the subnet, not as it is routed within a subnet」— サブネット内で完結する通信はNACLの評価対象外
- 根拠: https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html
SGがインスタンス1台ごとの門番なのに対し、NACLはサブネットという区画の境界に立つ検問所。同じサブネット内のホスト同士のやり取りには手を出さず、境界を「入る便・出る便」だけを見る。
② ステートレスの正体 — 過去を覚えない
- 公式: 「NACLs are stateless, which means that information about previously sent or received traffic is not saved. If, for example, you create a NACL rule to allow specific inbound traffic to a subnet, responses to that traffic are not automatically allowed.」
- 対してSG: 「Security groups are stateful … responses are automatically allowed regardless of outbound security group rules.」
- 公式の例文: 「Network ACLs are stateless. Therefore, you must include a rule that allows responses to the inbound traffic.」
- 根拠: https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html , https://docs.aws.amazon.com/vpc/latest/userguide/nacl-examples.html
ステートレスとは「個々のパケットだけを見て、前後の文脈を持たない」ということ。だから行きを許可しても戻りは別物として再び検問される——応答を通すルールは利用者が自分で書く。これは仕組みを単純にする代わりに文脈の面倒を利用者側に移す設計で、メッセージング編のat-least-once(配送は単純化し、重複排除は受け手の仕事)と同じ責任の移し方だ。
③ 戻りはエフェメラルポートに届く
- TCPは接続ごとに、リクエストを開始したクライアント側が一時的な高番号ポート(エフェメラルポート)を選んで開き、サーバの応答はそのポート宛てに返る。公式: 「The client that initiates the request chooses the ephemeral port range. The range varies depending on the client's operating system.」— 多くのLinuxカーネルは32768–61000、Windows Server 2008以降は49152–65535、NATゲートウェイ・ELB・Lambdaは1024–65535
- 公式: 「If an instance in your VPC is the client initiating a request, your network ACL must have an inbound rule to enable traffic destined for the ephemeral ports specific to the operating system of the instance.」— 公式のカスタムNACL例では、この戻りを通すインバウンドルールにポート範囲 32768–65535(Custom TCP, ALLOW)を使用
- 公式の実務指針: 「In practice, to cover the different types of clients … you can open ephemeral ports 1024-65535.」— 多様なクライアントを全てカバーする広い範囲として1024–65535を挙げ、その中の危険なポートは(許可より小さい番号の)拒否ルールで先に締め出せ、とする
- 根拠: https://docs.aws.amazon.com/vpc/latest/userguide/custom-network-acl.html , https://docs.aws.amazon.com/vpc/latest/userguide/nacl-examples.html
「行きの443を開けたのに繋がらない」の正体はこれ。応答は443ではなく、クライアントが開いた高番号ポート(エフェメラルポート)に返る。SGなら記憶があるので自動で通るが、NACLはインバウンドでエフェメラル範囲を明示的に許可しないと戻れない——範囲はクライアントのOS依存で、公式例は32768–65535、全部をカバーする実務値は1024–65535(NATゲートウェイもこの範囲を使う)。API Gateway編・キャッシュ編で暗黙に使っていたTCP接続の裏側の種明かしでもある。
④ 番号順・最初の一致で打ち切り
- 公式: 「Each rule has a number from 1 to 32766. We evaluate the rules in order, starting with the lowest numbered rule … If the traffic matches a rule, the rule is applied and we do not evaluate any additional rules.」
- 公式の推奨: 「We recommend that you start by creating rules in increments (for example, increments of 10 or 100) so that you can insert new rules later on.」
- 「*」ルール: 「These rules ensure that if a packet doesn't match any of the other numbered rules, it's denied.」「You can't delete a rule where the rule number is an asterisk.」
- 比較表(公式): SGは「Evaluates all rules before deciding whether to allow traffic」、NACLは「Evaluates rules in ascending order until a match for the traffic is found」
- 根拠: https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html , https://docs.aws.amazon.com/vpc/latest/userguide/default-network-acl.html , https://docs.aws.amazon.com/vpc/latest/userguide/infrastructure-security.html
NACLの競合解決は「人間が付けた番号の昇順で、最初に一致したものが勝ち」。番号を10刻み・100刻みで振るのは、後から間に新ルールを挿し込めるようにする実務知恵だ。これは認証暗号編のポリシー評価順序、ルートテーブルの最長一致と並ぶ「競合解決の第3のパターン」——今度は優先度を明示的な番号で人間が決める。しかもSGにはできない「拒否(DENY)」が書けるのがNACLの持ち味。
⑤ 必ず1つ付き、SGの後ろに控える層
- 関連付け: 「Each subnet in your VPC must be associated with a network ACL. If you don't explicitly associate a subnet with a network ACL, the subnet is automatically associated with the default network ACL.」「A default network ACL is configured to allow all traffic to flow in and out.」
- 使い回し: 「You can associate a network ACL with multiple subnets. However, a subnet can be associated with only one network ACL at a time. When you associate a network ACL with a subnet, the previous association is removed.」— 付け替えると前の関連付けは外れる
- 立ち位置: 「Use security groups as the primary mechanism for controlling network access … Network ACLs can be effective as a secondary control (for example, to deny a specific subset of traffic) or as high-level subnet guard rails … they can be used as defense-in-depth in case an instance is ever launched without the correct security group.」
- 例外: 「Network ACLs can't block DNS requests to or from the Route 53 Resolver (also known as the VPC+2 IP address or AmazonProvidedDNS).」「Network ACLs can't block traffic to the Instance Metadata Service (IMDS).」
- 根拠: https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html , https://docs.aws.amazon.com/vpc/latest/userguide/default-network-acl.html , https://docs.aws.amazon.com/vpc/latest/userguide/infrastructure-security.html
サブネットは必ず1つのNACLを持ち、何もしなければ全許可のデフォルトが付く。公式も正直に「主役はSG、NACLは追加の防御層」と位置づける——特定のトラフィックを明示的に締め出したり、SGを付け忘れたインスタンスへのガードレールとして効く。ただしRoute 53 Resolver(VPC+2)やIMDS宛てはNACLでも止められない、という例外も押さえておく。
⑥ 4行でまとめるSGとNACLの違い
- 公式の比較表そのまま — Level of operation: Instance level / Subnet level
- Rule type: Allow rules only / Allow and deny rules
- Rule evaluation: Evaluates all rules before deciding whether to allow traffic / Evaluates rules in ascending order until a match for the traffic is found
- Return traffic: Automatically allowed (stateful) / Must be explicitly allowed (stateless)
- 根拠: https://docs.aws.amazon.com/vpc/latest/userguide/infrastructure-security.html
この4行が本レッスンの全て。①②③④のどれか1つでも「NACLはこうだ」と即答できれば、SGとの対比で仕組みを理解できている。特に④「ステートレスだから戻りも自分で書く」が、ステートレス・パケットフィルタリングというCS基礎の核心——前レッスンのSGと対にして初めてこの一枚が完成する。