aws CDK PublicSubnet not creating internet gateway

Viewed 495

CDK is version 1.61.1

Rather then create subnets inline in the vpc resource I'm trying to create them using the PublicSubnet construct. I do not want the vpc construct to create any subnets. If I try this:

from aws_cdk import (
    aws_s3 as s3,
    aws_ec2 as ec2,
    core,
)

class HelloCdkStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)


        ## VPC
        vpc = ec2.Vpc(self, "vpc1",
            cidr                 = "172.20.0.0/23",
            nat_gateways         = 0,
            enable_dns_hostnames = True,
            enable_dns_support   = True,

            subnet_configuration = []
            )

        subnet = ec2.PublicSubnet(self, "subnet1",
            vpc_id                  = vpc.vpc_id,
            availability_zone       = vpc.availability_zones[0],
            cidr_block              = "172.20.0.0/24",
            map_public_ip_on_launch = True
        )

The subnet and route table gets created but no internet gateway or default route:

vpcA2121C38:
  Type: AWS::EC2::VPC
  Properties:
    CidrBlock: 172.20.0.0/23
    EnableDnsHostnames: true
    EnableDnsSupport: true
    InstanceTenancy: default
    Tags:
      - Key: Name
        Value: hello-cdk/vpc
  Metadata:
    aws:cdk:path: hello-cdk/vpc/Resource
subnetSubnet39D20FD5:
  Type: AWS::EC2::Subnet
  Properties:
    CidrBlock: 172.20.0.0/24
    VpcId:
      Ref: vpcA2121C38
    AvailabilityZone:
      Fn::Select:
        - 0
        - Fn::GetAZs: ""
    MapPublicIpOnLaunch: true
    Tags:
      - Key: Name
        Value: hello-cdk/subnet
  Metadata:
    aws:cdk:path: hello-cdk/subnet/Subnet
subnetRouteTable8BC76A23:
  Type: AWS::EC2::RouteTable
  Properties:
    VpcId:
      Ref: vpcA2121C38
    Tags:
      - Key: Name
        Value: hello-cdk/subnet
  Metadata:
    aws:cdk:path: hello-cdk/subnet/RouteTable
subnetRouteTableAssociation94267163:
  Type: AWS::EC2::SubnetRouteTableAssociation
  Properties:
    RouteTableId:
      Ref: subnetRouteTable8BC76A23
    SubnetId:
      Ref: subnetSubnet39D20FD5
  Metadata:
    aws:cdk:path: hello-cdk/subnet/RouteTableAssociation

In fact there appears to be no difference in created resources if I use PublicSubnet, PrivateSubnet or Subnet contructs. If I add a public subnet inside the vpc construct the gateway does get created.

0 Answers
Related