user10588932
user10588932

Reputation:

Why does CDK complain about missing public subnet groups?

This is my current CDK stack:

const vpc = new ec2.Vpc(this, "vpc-staging", {
  maxAzs: 1,
  enableDnsHostnames: true,
  enableDnsSupport: true,
  cidr: '10.10.0.0/16',
  subnetConfiguration: []
});

const publicSubnet = new ec2.Subnet(this, 'public-subnet', {
  cidrBlock: '10.10.10.0/24',
  vpcId: vpc.vpcId,
  mapPublicIpOnLaunch: true
})

To the above I am trying to add an ECS cluster like so:

const cluster = new ecs.Cluster(this, 'EcsCluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', {
  instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO)
})

When running cdk diff this is the error that I get:

(node:48942) ExperimentalWarning: The fs.promises API is experimental
/Users/me/src/wow/aws/node_modules/@aws-cdk/aws-ec2/lib/vpc.js:201

throw new Error(`There are no '${subnetType}' subnet groups in this VPC. Available types: ${availableTypes}`);
^

Error: There are no 'Public' subnet groups in this VPC. Available types:

What is it that that I am missing from my config?

Upvotes: 1

Views: 2445

Answers (2)

Pedreiro
Pedreiro

Reputation: 1804

The mapPublicIpOnLaunch parameter has a default set to true, so you could simply not set any subnet at all by simply removing the subnetConfiguration line from the vpc and deleting the subnet object to leave the default creation.

If you really want to set them, add subnetType: ec2.SubnetType.PUBLIC to your subnet.

Also, I think it's better if you keep the subnet config on the vpc construct to wire everything on the vpc creation:

 const vpc = new ec2.Vpc(this, "vpc-staging", {
    maxAzs: 1,
    enableDnsHostnames: true,
    enableDnsSupport: true,
    cidr: '10.10.0.0/16',
    subnetConfiguration: [
       {
        cidrMask: 24,   // this is optional as it divides equally if not set
        name: 'public-subnet',
        subnetType: ec2.SubnetType.PUBLIC,
      },
      {
        cidrMask: 24,
        name: 'private-subnet',
        subnetType: ec2.SubnetType.PRIVATE,
      },
    ...
  ]
});

Also, I'm not 100% sure, but this code might fail because ECS need at least two availability zones to work.

Upvotes: 0

Marcin
Marcin

Reputation: 238687

mapPublicIpOnLaunch: true is not sufficient for a subnet to be considered Public.

You also need an Internet Gateway which is attached to your VPC. In addition, route tables should be setup to route internet traffic 0.0.0.0/0 to the gateway.

General information about VPC, public and private subnets is here.

Hope this helps.

Upvotes: 1

Related Questions