aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Using VPC in Cluster with CDK 0.32.0 does not work: Error: There are no 'Private' subnets in this VPC. Use a different VPC subnet selection

Open markusl opened this issue 7 years ago • 5 comments

In CDK 0.31.0 our stack looked like this:

const vpcProps: ec2.VpcNetworkImportProps = {
  vpcId: 'vpc-xxxx',
  availabilityZones: ['eu-central-1a', 'eu-central-1b', 'eu-central-1c'],
  publicSubnetIds: ['subnet-xxxx', 'subnet-xxxx', 'subnet-xxxx'],
  privateSubnetIds: ['subnet-xxxx', 'subnet-xxxx', 'subnet-xxxx'],
};
    const cluster = ecs.Cluster.fromClusterAttributes(this, `Cluster`, {
      clusterName: clusterName,
      vpc: vpcProps,
      securityGroups: [ ]
    });

with CDK 0.32.0 we are forced to use a VPC with cluster import:

    const cluster = ecs.Cluster.fromClusterAttributes(this, `Cluster`, {
      clusterName: clusterName,
      vpc: vpcProps,
      securityGroups: [ ]
    });

which causes an error Error: There are no 'Private' subnets in this VPC. Use a different VPC subnet selection.

Somehow the cluster seems to lose the specified private subnets with this new model. Is there any workaround for this functionality?

markusl avatar May 24 '19 13:05 markusl

I think the problem is that the class ImportedCluster uses the supplied property vpc as VpcAttributes to import the VPC but the type is IVpc.

hoegertn avatar May 24 '19 19:05 hoegertn

Is this a bug in the CDK or is there a way to change the code to make it work again with the latest version?

markusl avatar May 25 '19 12:05 markusl

I think it is a bug, but I managed to patch around it:

        const cluster = Cluster.fromClusterAttributes(this, 'Cluster', {
            vpc: <any>{
                vpcId: 'someid',
                availabilityZones: ['eu-central-1a', 'eu-central-1b', 'eu-central-1c'],
                publicSubnetIds: [
                    'someid',
                    'someid',
                    'someid',
                ],
            },
            clusterName: 'someid',
            securityGroups: []
        });

So I used VpcAttributes and told TypeScript to ignore the type mismatch.

hoegertn avatar May 25 '19 12:05 hoegertn

I also ran into this when using the VPC directly across stacks (in same app) on 0.31.0. The workaround was manually exporting/importing the VPC across stacks.

AlexCheema avatar May 27 '19 09:05 AlexCheema

We reuse cluster for ECS Fargate services.

Importing a VPC is required to use an existing Cluster (ecs.Cluster.fromClusterAttributes).

When importing an existing VPC, ec2.Vpc.fromLookup and ec2.Vpc.fromVpcAttributes both results in the same error:

There are no 'Private' subnets in this VPC. Use a different VPC subnet selection.

This was using AWS CDK 0.32.

Previously in AWS CDK 0.31 I was able to work around this by first calling ec2.VpcNetwork.import and entering vpcId, availabilityZones, publicSubnetIds and privateSubnetIds details.

Once the existing VPC was imported, I was able to import the cluster, without the error using:

cluster = ecs.Cluster.fromClusterAttributes(this,
                configuration.clusterName(), {
                    clusterName: configuration.clusterName(), 
                    securityGroups: [securityGroup],
                    vpc: {
                        vpcId: existingVpc.vpcId,
                        availabilityZones: existingVpc.availabilityZones,
                        privateSubnetIds: existingVpc.selectSubnets({ subnetType: SubnetType.Private }).subnetIds,
                        publicSubnetIds: existingVpc.selectSubnets({ subnetType: SubnetType.Public }).subnetIds
                    }
                });

EduardTheThird avatar May 27 '19 13:05 EduardTheThird