互联网网关与路由表
连接互联网网关以启用出站互联网访问,并为公有子网配置路由表。
互联网网关与路由表 是 CoddyKit 上的免费 AWS Solutions Architect 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AWS Solutions Architect 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AWS Solutions Architect 课程共包含 4 节课。
Internet Gateway:VPC 通往互联网的大门
Internet Gateway (IGW) 是一种横向扩展、冗余且高度可用的 VPC 组件,用于启用 VPC 与互联网之间的通信。它会为具有公有 IPv4 地址的实例执行网络地址转换(NAT):对于出站流量,将实例的私有 IP 转换为其 Elastic IP 或自动分配的公有 IP;对于入站流量,则反向转换。每个 VPC 只能有一个 IGW;将 IGW 挂载到 VPC 并不会自动赋予实例互联网访问权限,您还必须更新路由表,并确保实例具有公有 IP。
# Create and attach an internet gateway
aws ec2 create-internet-gateway \
--tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value=prod-igw}]'
aws ec2 attach-internet-gateway \
--internet-gateway-id igw-12345678 \
--vpc-id vpc-12345678Route Tables:流量方向
route table 是一组规则(routes),用于确定来自 subnet 或 gateway 的网络流量应被转发到哪里。每个 VPC 都有一个主路由表,除非您明确将其他路由表与某个 subnet 关联,否则所有 subnet 默认使用该路由表。每条 route 都有一个目标地址(CIDR 块)和一个目标对象(local、internet gateway、NAT gateway、对等连接等)。当多个 route 与数据包的目标匹配时,最具体的 route(最长前缀匹配)优先。
# Create a route table for public subnets
aws ec2 create-route-table \
--vpc-id vpc-12345678 \
--tag-specifications 'ResourceType=route-table,Tags=[{Key=Name,Value=public-rt}]'将 subnet 设为公有
如果 subnet 关联的路由表包含一条指向 Internet Gateway 的 0.0.0.0/0(所有互联网流量)route,则该 subnet 为公有 subnet。将 subnet 设为公有需要三个步骤:(1) 创建自定义路由表,并添加一条 0.0.0.0/0 → IGW route;(2) 将该路由表与 subnet 关联;(3) 在 subnet 上启用自动分配公有 IPv4,使其中启动的实例自动获得公有 IP。只有同时完成这三个步骤,才能创建可正常工作的公有 subnet。
# Add internet route to the public route table
aws ec2 create-route \
--route-table-id rtb-12345678 \
--destination-cidr-block 0.0.0.0/0 \
--gateway-id igw-12345678
# Associate the route table with a public subnet
aws ec2 associate-route-table \
--route-table-id rtb-12345678 \
--subnet-id subnet-public-1a
# Enable auto-assign public IP for the subnet
aws ec2 modify-subnet-attribute \
--subnet-id subnet-public-1a \
--map-public-ip-on-launchLocal Route
每个路由表都会自动包含一条local route(例如 10.0.0.0/16 → local),用于启用 VPC 内所有资源之间的通信。这条 route 不能删除或修改。它确保 VPC 中任意 subnet 的实例都可以使用私有 IP 地址彼此通信,而无需经过任何 gateway。对于目标位于 VPC CIDR 范围内的流量,local route 始终优先于任何自定义 route。
# Describe the routes in a route table
aws ec2 describe-route-tables \
--route-table-ids rtb-12345678 \
--query 'RouteTables[0].Routes'
# Typical output shows:
# {DestinationCidrBlock: '10.0.0.0/16', GatewayId: 'local'}
# {DestinationCidrBlock: '0.0.0.0/0', GatewayId: 'igw-12345678'}Route 优先级与最长前缀匹配
当一个数据包可以匹配路由表中的多条 route 时,AWS 会选择前缀最具体的 route(匹配的前缀最长)。例如,如果您有一条 10.0.0.0/8 → peering route 和一条 10.1.0.0/16 → VPN route,那么目标为 10.1.0.5 的数据包会同时匹配两者,但 /16 route 更具体(前缀更长),因此优先。如果 routes 的前缀长度相同,则更具体的 gateway 类型优先级更高:local > VGW propagated > static。在设计包含 peering、VPN 和 DX 连接的复杂网络时,理解这一点非常重要。
IPv6 与仅出站 Internet Gateway
VPC 除了支持 IPv4,还支持 IPv6 CIDR 块(AWS 提供的 /56)。对于 IPv6 互联网访问,IGW 同时处理 IPv4 和 IPv6。若只需要 IPv6 出站访问(允许实例发起出站 IPv6 连接,但阻止入站连接),请使用 Egress-Only Internet Gateway——它在功能上类似于面向 IPv4 私有 subnet 的 NAT gateway,但专用于 IPv6。向私有 subnet 添加 ::/0 → eigw-xxxxxxxx route,即可启用 IPv6 出站访问,同时避免实例暴露于入站 IPv6 连接。
# Create an Egress-Only IGW for IPv6
aws ec2 create-egress-only-internet-gateway \
--vpc-id vpc-12345678
# Add IPv6 route in private subnet route table
aws ec2 create-route \
--route-table-id rtb-private \
--destination-ipv6-cidr-block '::/0' \
--egress-only-internet-gateway-id eigw-12345678Gateway Route Tables
Gateway route tables 是直接与 Internet Gateway 或 Virtual Private Gateway 关联的路由表(而不是与 subnet 关联)。它们支持入站路由,可以在流量从互联网进入 VPC、到达 EC2 实例之前对其进行检查。这适用于需要查看所有入站流量的内联安全设备(IDS/IPS、防火墙虚拟设备):配置 IGW 路由表,将入站流量发送到 Gateway Load Balancer 终端节点,由它将流量分发给安全设备;检查完成后,安全设备再将流量转发到目标 EC2 实例。
使用 VPN/DX 进行 Route Propagation
通过 VPN 或 Direct Connect 连接本地网络时,可以借助route propagation 将本地网络的 routes 自动传播到 VPC 路由表。在 Virtual Private Gateway(VGW)对应的路由表上启用传播后,本地路由器通过 BGP 通告的所有 routes 都会自动出现在路由表中,无需手动添加 route。这对于本地 subnet 经常变化的动态环境尤其有用。对于静态 VPN 连接,则需要手动添加本地 CIDR routes。
# Enable route propagation from a Virtual Private Gateway
aws ec2 enable-vgw-route-propagation \
--route-table-id rtb-12345678 \
--gateway-id vgw-12345678多个路由表的最佳实践
最佳实践是为每个层级创建独立的路由表:公有 subnet 使用一个路由表(包含 IGW route),私有应用 subnet 使用一个路由表(包含 NAT gateway route),私有数据 subnet 使用一个路由表(不包含互联网 route,仅使用 VPC endpoints)。这样,即使配置错误的安全组允许出站流量,数据层实例也无法意外访问互联网,因为其路由表根本没有通向外部的路径。分离路由表还便于按层级审计网络访问模式。
VPC Endpoint Routes
创建 S3 或 DynamoDB Gateway VPC Endpoint 后,AWS 会自动向指定的路由表添加一条 route,其目标地址是该服务的托管前缀列表,目标对象是 VPC endpoint。这条 route 可确保来自这些 subnet、发往 S3 或 DynamoDB 的流量通过 AWS 的私有网络传输,而不是经过互联网。Gateway endpoints 免费;如果实例产生大量 S3 流量,还可以显著降低 NAT gateway 成本。在生产环境的 VPC 中,请始终将 S3 和 DynamoDB gateway endpoints 添加到私有 subnet 的路由表中。
# Create S3 gateway endpoint and add to route tables
aws ec2 create-vpc-endpoint \
--vpc-id vpc-12345678 \
--service-name com.amazonaws.us-east-1.s3 \
--route-table-ids rtb-private-app rtb-private-data用于流量工程的 Blackhole Routes
blackhole route 是其目标不可达的 route,专门用于丢弃流量。当 route 的目标(例如 VPN 连接或 Transit Gateway 挂载)被删除但 route 仍然存在时,AWS 会创建 blackhole routes。您也可以主动创建 blackhole routes,以阻止 VPC 内部访问特定的 IP 范围。这是一种不同于安全组和 NACL 的网络层流量控制机制。在 AWS 中,当 route 引用的 endpoint 或 gateway 不可用时,可以在控制台中看到 blackhole routes。
快速检查
测试您对本课 AWS Solutions Architect(SAA-C03)相关概念的理解。
课程回顾
本课您学习了:Internet Gateway 可以启用 VPC 与互联网之间的通信,并且必须挂载到 VPC,同时在 subnet 的路由表中通过 0.0.0.0/0 route 指向它;路由表通过目标地址与目标对象的配对来控制流量方向,并按照最长前缀匹配确定优先级;以及为不同 subnet 层级分别配置路由表,可以通过架构设计强制实施流量隔离。接下来我们将学习用于私有 subnet 互联网访问的 NAT Gateways。
常见问题解答
「互联网网关与路由表」课时是免费的吗?
是的 — 「互联网网关与路由表」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AWS Solutions Architect 课程的其余内容,请升级到 CoddyKit PRO。 AWS Solutions Architect 课程共包含 4 节课。
「互联网网关与路由表」这节课中我会学到什么?
连接互联网网关以启用出站互联网访问,并为公有子网配置路由表。 你通过在浏览器中直接运行的动手代码来练习 AWS Solutions Architect,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AWS Solutions Architect 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AWS Solutions Architect 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「互联网网关与路由表」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AWS Solutions Architect 课中编写并运行代码吗?
能。每节 AWS Solutions Architect 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- VPC 架构与 CIDR 块
- 互联网网关与路由表
- NAT 网关与私有子网
- 网络 ACL 与安全组