Search code examples
amazon-web-servicesaws-cloudformationamazon-ecsamazon-vpcaws-cloudformation-custom-resource

RouteTable is created twice with cloudformation - ECS


I am trying to create vpc using cloudformation, with a single public subnet, route table and internet gateway. Problem is two route tables are created in association with the vpc even if I have included just 1 route table.

enter image description here

Below is my code

Description: "Create a VPC with a public subnet, Internet Gateway, and a public route table"

Parameters:
  VpcCIDR:
    Type: String
    Description: "CIDR block for the VPC (e.g., 10.0.0.0/16)"

  PublicSubnetCIDR:
    Type: String
    Description: "CIDR block for the public subnet (e.g., 10.0.1.0/24)"

Resources:
  MyVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VpcCIDR
      Tags:
        - Key: Name
          Value: MyVPC

  MyInternetGateway:
    Type: AWS::EC2::InternetGateway

  MyVPCGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref MyVPC
      InternetGatewayId: !Ref MyInternetGateway

  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref MyVPC
      Tags:
        - Key: Name
          Value: PublicRouteTable

  PublicRoute:
    Type: AWS::EC2::Route
    DependsOn: MyVPCGatewayAttachment
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: "0.0.0.0/0"
      GatewayId: !Ref MyInternetGateway

  PublicSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: !Ref PublicSubnetCIDR
      AvailabilityZone:
        Fn::Select:
        - 0
        - Fn::GetAZs: {Ref: 'AWS::Region'}
      Tags:
        - Key: Name
          Value: PublicSubnet

  PublicSubnetRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnet
      RouteTableId: !Ref PublicRouteTable

Outputs:
  VpcId:
    Value: !Ref MyVPC
    Description: "VPC ID"
  PublicRouteTableId:
    Value: !Ref PublicRouteTable
    Description: "Public Route Table ID"
  PublicSubnetId:
    Value: !Ref PublicSubnet
    Description: "Public Subnet ID"

Solution

  • A VPC will always have a default route table:

    When you create a VPC, it automatically has a main route table. When a subnet does not have an explicit routing table associated with it, the main routing table is used by default.