我是否遗漏了什么,但是没有办法通过 CloudFormation 将路由添加到随 VPC 配置的默认路由表中?
答案1
不,你不能,反正也没有什么可参考的(例如逻辑 ID)。只需创建你自己的主表 ;-)。
这可能是它不能使用的原因之一:
保护 VPC 的一种方法是保留主路由表处于其原始默认状态(仅具有本地路由),并且 明确关联每个新子网使用您创建的自定义路由表之一创建。这可确保您必须明确控制每个子网的出站流量的路由方式。
答案2
如果您需要通过 CloudFormation 实现该设置,您可以自行定义每个组件。只需创建您自己的 VPC、Internet 网关、子网和路由表。然后,您需要为特定子网明确声明 RouteTableAssociation 并为该表创建公共路由。以下是示例
AWSTemplateFormatVersion: '2010-09-09'
Description: Example
Resources:
myInternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: "Name"
Value: "a_gateway"
myVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/24
EnableDnsSupport: true
EnableDnsHostnames: true
InstanceTenancy: default
# Attach Internet gateway to created VPC
AttachGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId:
Ref: myVPC
InternetGatewayId:
Ref: myInternetGateway
# Create public routes table for VPC
myPublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref myVPC
Tags:
- Key: "Name"
Value: "public_routes"
# Create a route for the table which will forward the traffic
# from the gateway
myDefaultPublicRoute:
Type: AWS::EC2::Route
DependsOn: AttachGateway
Properties:
RouteTableId: !Ref myPublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref myInternetGateway
# Subnet within VPC which will use route table (with default route)
# from Internet gateway
mySubnet:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ""
CidrBlock: 10.0.0.0/25
MapPublicIpOnLaunch: true
VpcId:
Ref: myVPC
# Associate route table (which contains default route) to newly created subnet
myPublicRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref myPublicRouteTable
SubnetId: !Ref mySubnet
这样,您将能够使用创建的路由表(在上面的示例中,它用于转发来自 Internet 网关的流量)