Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(aws-cdk-lib/aws-ec2): Misleading error "There are no <SubnetType> subnet groups in this VPC. Available types:" when no availability zones are provided #32451

Open
1 task
gravitylow opened this issue Dec 9, 2024 · 2 comments
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. effort/small Small work item – less than a day of effort p2

Comments

@gravitylow
Copy link

Describe the bug

When one uses the ec2.Vpc construct and supplies an empty list of availabilityZones, this results in no subnets actually being created in the VPC due to the loop here. If one then tries to create a VPC Endpoint or NAT Gateway in one of the VPCs they have configured, they will get an error such as There are no 'Private' subnet groups in this VPC. Available types:, which can be a bit confusing since there were private subnets configured (but they didn't end up being created).

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

When someone provides an empty availabilityZones list along with a subnet configuration, this should probably result in an error being thrown since this is a nonsensical configuration, which may lead to either missing subnets or misleading errors in further infra configuration down the line.

Current Behavior

The nonsensical configuration is silently accepted and leads to CDK not acting on the customer's configured subnets, or a different misleading error when later trying to create things in one of these subnets.

Reproduction Steps

The following VPC construct reproduces the error:

new ec2.Vpc(this, 'VPC', {
      vpcName: 'Test VPC',
      ipAddresses: ec2.IpAddresses.cidr('10.0.0.0/16'),
      defaultInstanceTenancy: ec2.DefaultInstanceTenancy.DEFAULT,
      availabilityZones: [],
      enableDnsHostnames: true,
      enableDnsSupport: true,
      subnetConfiguration: [
        {
          cidrMask: 24,
          name: 'public',
          subnetType: SubnetType.PUBLIC,
        },
        {
          cidrMask: 24,
          name: 'private',
          subnetType: SubnetType.PRIVATE_WITH_EGRESS,
        },
      ],
      gatewayEndpoints: {
        S3: {
          service: ec2.GatewayVpcEndpointAwsService.S3,
          subnets: [{ subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }, { subnetType: ec2.SubnetType.PUBLIC }],
        },
      },
    });

In this case providing availabilityZones: [], is explicit and is a clear bug, but the issue is not caught at that point in the construct initialization. Rather it is raised later when trying to create the requested S3 gateway endpoint.

In reality, the code which this example was extracted from retrieves the availability zones to use dynamically, which obscures the real issue from being this obvious.

Possible Solution

Add validation to throw an error when a Vpc is initialized with an empty availabilityZones list

Additional Information/Context

No response

CDK CLI Version

2.171.1 (build a95560c)

Framework Version

No response

Node.js Version

18.x

OS

MacOS Sonoma 14.7 (23H124)

Language

TypeScript

Language Version

No response

Other information

No response

@gravitylow gravitylow added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 9, 2024
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Dec 9, 2024
@khushail khushail added needs-reproduction This issue needs reproduction. p2 and removed needs-triage This issue or PR still needs to be triaged. labels Dec 10, 2024
@khushail khushail self-assigned this Dec 10, 2024
@khushail khushail added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-reproduction This issue needs reproduction. labels Dec 12, 2024
@khushail
Copy link
Contributor

Hi @gravitylow , thanks for reporting this issue.

I tried to repro this with slightly modifying the code initially to see whether the subnets are created , with not mentioning availability zones at all and here is my result -

code -

new ec2.Vpc(this, 'VPC', {
      vpcName: 'Test VPC',
      ipAddresses: ec2.IpAddresses.cidr('10.0.0.0/16'),
      defaultInstanceTenancy: ec2.DefaultInstanceTenancy.DEFAULT,
      enableDnsHostnames: true,
      enableDnsSupport: true,
      subnetConfiguration: [
        {
          cidrMask: 24,
          name: 'public',
          subnetType: SubnetType.PUBLIC,
        },
        {
          cidrMask: 24,
          name: 'private',
          subnetType: SubnetType.PRIVATE_WITH_EGRESS,
        },
      ],
      gatewayEndpoints: {
        S3: {
          service: ec2.GatewayVpcEndpointAwsService.S3,
          subnets: [{ subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }, { subnetType: ec2.SubnetType.PUBLIC }],
        },
      },
    });

and the console shows all subnets created -

Screenshot 2024-12-12 at 5 03 11 PM

while if I include the availabilityZones:[] in the code, i get the error -

Screenshot 2024-12-12 at 5 20 10 PM

On deep-diving the code, I see that in the CDK Docs , its mentioned for availabilityZone to be optional with no defaults which should be checked , as stated in solution.
When its not mentioned, the code works fine and subnets are created but when the list is empty, it leads to misocnfiguration and hence error in later stages. What you have mentioned in the scenario/solution makes sense.

So I agree it should be checked in the code if empty array is provided.

Thanks for raising this issue. I am marking this as P2 as we have a workaround to not mention it at all or mention with regions ..but it would be good to have this check at first place. Marking it as P2 means it won't be immediately addressed by the team but would be on their radar and contributions from community are welcome as well as from the team. Hope that helps!

@khushail khushail added p2 effort/small Small work item – less than a day of effort and removed p2 investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Dec 13, 2024
@khushail khushail removed their assignment Dec 13, 2024
@gravitylow
Copy link
Author

Thanks very much @khushail. Since this is a relatively small issue and you agree with the proposed solution I think I will take a stab at adding that check when I get some time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

No branches or pull requests

2 participants