Aws vpc default route table in CloudFormation
Solution 1:
Nah you can't, there's nothing to refer to anyway (e.g. logical ID). Just create your own main table ;-).
This is probably one of the reason it can't be used:
One way to protect your VPC is to leave the main route table in its original default state (with only the local route), and explicitly associate each new subnet you create with one of the custom route tables you've created. This ensures that you must explicitly control how each subnet's outbound traffic is routed.
Solution 2:
You can define each component by yourself in case you need to implement that setup via CloudFormation. Just create your own VPC, Internet Gateway, Subnet and Route Table. Then you need to explicitly declare RouteTableAssociation for the specific subnet and create a public route for that table. Here's an example
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
This way you'll be able to use created route table (in the example above it's used to forward traffic from Internet Gateway)