115. 1つのリクエストはVPCの中で何度も審査される — DNS・ルート・NACL・SGを貫く旅の地図
プライベートサブネットのアプリが放つ3種類のリクエストが、VPCの中でどんな検問を何度もくぐって目的地に着き、そして帰ってくるのか——本編で組み立ててきた全部品を1枚の地図に置いて追っていきます。
① 出発点は同じ、行き先で分かれる三叉路 — まず名前を引く
- Resolverの居場所は「primary private IPV4 CIDR range provisioned to your VPC plus two」= VPC CIDRの先頭+2、および 169.254.169.253(IPv4)/ fd00:ec2::253(IPv6)。クエリはリンクローカルアドレス経由で運ばれ「are not visible on the network」。
- 「The Amazon Route 53 Resolver only supports recursive DNS queries.」
- プライベートホストゾーンの名前解決には enableDnsHostnames と enableDnsSupport の両方を true にする必要がある。
- 注意: 「You cannot filter traffic to or from the Amazon DNS server using network ACLs or security groups.」——DNSクエリ自体は後述の検問の対象外。
どの経路でも旅の一歩目は同じ「名前を引く」。Resolverは分散データベースDNSのリゾルバそのもので、VPC+2番地に常駐している。返ってきたIPが公開のAWS帯なのか・外部のグローバルIPなのか・VPC内のプライベートIPなのかで、この先の運命が三叉路に分かれる。
② 最初の審査はルートテーブル — 「最も具体的な行」が勝つ
- 「we direct traffic using the most specific route that matches the traffic. This is known as the longest prefix match.」(例: 10.10.2.15/32 は 10.10.2.0/24 に優先)
- 経路A: 「If there is a route that sends all internet traffic (0.0.0.0/0) to an internet gateway, the endpoint route takes precedence for traffic destined for the service ... in the current Region.」プレフィックスリストはリージョン固有なので、他リージョンのS3宛はIGWへ行く。
- ルートの優先順位: ①最長プレフィックス ②静的ルート ③プレフィックスリストルート ④伝播ルート。ただし「more specific routes always take priority irrespective of whether they are propagated routes, static routes, or routes that reference prefix lists.」——具体性が最優先。
- 静的ルートを使う対象(伝播ルートに勝つ側): internet gateway / NAT gateway / Network interface / Gateway VPC endpoint / VPC peering など。「the static route takes priority.」
- local ルートはVPC内通信のための既定ルート(「A default route for communication within the VPC」)で、VPC CIDR宛はVPC内で処理される。
ルートテーブルは「宛先IPで一番具体的な行を引く検索テーブル」。DynamoDB編でパーティションキーからハッシュで置き場所を引いたのと同じく、ここでもキー(宛先IP)から行き先(ターゲット)を一意に引く。0.0.0.0/0は「他のどの行にも負けた時だけ選ばれる、最も一般的な受け皿」。
③ 三経路の目的地までの道のり — 出ない道、AWS網内の道、2段変換の道
- 経路A: 「Gateway VPC endpoints provide reliable connectivity to Amazon S3 and DynamoDB without requiring an internet gateway or a NAT device」。ゲートウェイエンドポイントはPrivateLinkを使わない別種。
- 経路B: プライベートサブネットは定義上IGWへのルートを持たない。「To enable instances in the private subnet to send traffic ... you would add a NAT device ... and route traffic in the private subnet to the NAT device.」NATは送信元アドレスを付け替える「変換表」。
- 経路Bの2段変換は公式の記述通り: 「Both private and public NAT gateways map the source private IPv4 address of the instances to the private IPv4 address of the NAT gateway, but in the case of a public NAT gateway, the internet gateway then maps the private IPv4 address of the public NAT gateway to the Elastic IP address associated with the NAT gateway.」——NATゲートウェイが自分のプライベートIPへ、その先のIGWがElastic IPへ変換する。
- 補足: 「While traffic to Amazon S3 or DynamoDB traverses the internet gateway, it does not leave the AWS network.」——ゲートウェイエンドポイントを使わずIGW経由にした場合でも、S3宛はAWS網の外に出ない。
- 経路C: local ルートにより、同一VPC内はプライベートIPで直接通信できる。
同じ「1つのアプリ」から出ても、行き先で通る部品数がまるで違う。S3宛は最短(AWS網内で完結)、外部API宛は最長(NAT→IGWと2段の変換を越える)、RDS宛はそもそもVPCから出ない。NATは、API Gateway編・S3編で見た「入口で名前や住所を付け替えるプロキシ」と同じ発想の「出口の変換表」。
④ どの経路にも共通の二重検問 — サブネット境界のNACLとENI手前のSG
- Rule type: SG=「Allow rules only」/ NACL=「Allow and deny rules」
- Rule evaluation: SG=「Evaluates all rules before deciding whether to allow traffic」/ NACL=「Evaluates rules in ascending order until a match for the traffic is found」
- Return traffic: SG=「Automatically allowed (stateful)」/ NACL=「Must be explicitly allowed (stateless)」
- Level: SG=Instance level(ENI単位)/ NACL=Subnet level(サブネット単位)
- 経路AでSGはプレフィックスリストIDを宛先に指定できる(pl-xxxx TCP 443)。NACLはプレフィックスリストを参照できず(「You can't reference prefix lists in network ACL rules」)、サービスのCIDRを直に書く必要がある。
- 前述の通り、Amazon DNS server宛のトラフィックはNACL/SGでフィルタできない(①の名前引きはこの二重検問の対象外)。
ルートテーブルが「どこへ送るか(到達)」を決めた後、NACLとSGが「送っていいか(許可)」を二重に審査する。メッセージング編で「配信保証はどこに状態を持つかで決まる」と見たのと同じ問いがここにも現れる——NACLは状態を持たない(だから往復を別々に書く)、SGは状態を持つ(だから往路の記憶で復路を通す)。
⑤ 4つの「競合解決の規則」が1つの旅の中で全部使われる
- ①「the most specific route that matches ... longest prefix match」
- ②「If the destination of a propagated route is identical to the destination of a static route, the static route takes priority.」かつ「more specific routes always take priority irrespective of ... propagated routes, static routes, or routes that reference prefix lists.」
- ③「Evaluates rules in ascending order until a match for the traffic is found」
- ④「Evaluates all rules before deciding whether to allow traffic」+「Allow rules only」
4つの規則は一見バラバラだが、「複数の候補から1つの結論をどう選ぶか」という同じ問いへの4通りの答え。①②は「1つだけ選ぶ(検索)」、③は「最初に見つけたら止める(順序依存)」、④は「全部見てOKが1つでもあれば通す(順序非依存)」——CIDRが2進数の区画割りだからこそ「具体的=プレフィックスが長い」という比較が成り立ち、①の最長一致が意味を持つ。
⑥ 復路は「状態」に支えられて初めて帰ってくる — ステートレスとステートフルの役割分担
- SG:「Return traffic ── Automatically allowed (stateful)」——往路を通した接続の戻りは自動で許可。
- NACL:「Return traffic ── Must be explicitly allowed (stateless)」——戻りポート(エフェメラルポート)のインバウンド許可を別途書く必要がある。
- NATは送信元付け替えの変換表を持つゆえに、応答をどのプライベートIPへ戻すか復元できる。「When sending response traffic to the instances ... the NAT gateway translates the address back to the original source IP address.」
- 経路A(S3)・経路C(RDS)も、SGのステートフル性により応答の戻りは自動で通る。
- コースの軸で回収: CIDR=2進数の区画割り(だから「具体的」を長さで測れる)/ ルートテーブル=最長一致検索 / SG・NACL=ステートフル・ステートレスのパケットフィルタリング / NAT=送信元・宛先を書き換える変換表 / Resolver=分散データベースDNSのリゾルバ。マネージドVPCの中身も、IPアドレッシング・ルーティング・パケットフィルタリング・NAT・DNSという教科書のネットワーク基礎でできていた。
- キャッシュ編の総括が「速さの層」をブラウザからDB前段まで地図にしたのに対し、この回は同じ1リクエストを「到達と許可の層」で地図にした——可観測性編の相関IDでリクエストを追ったのと同じ視点を、今度はインフラの側から実践したことになる。
往路と復路は非対称。復路がちゃんと帰ってこられるのは、NATの変換表とSGのコネクション追跡という2つの「状態」が往路で刻まれたおかげ。状態を持たない部品(ルートテーブル・NACL)と状態を持つ部品(NAT・SG)の役割分担こそVPCの設計であり、DynamoDB編以来くり返してきた「状態を持つ場所を設計する」という問いの、ネットワーク版の答え。