aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.53k stars 3.86k forks source link

aws-ec2: SubnetFilter byIds does not work as expected when using vpc.selectSubnets() on non-private/mixed subnet types. #30826

Open sbidy opened 2 months ago

sbidy commented 2 months ago

Describe the bug

Related Issue #24427 - the issue seems not being fixed or was reverted.

When creating a subnet selection with only the SubnetFilter.byIds(), the selection is empty if the subnets are not of type PRIVATE_WITH_EGRESS.

Expected Behavior

Being able to select subnet by IDs in a subnet selection when using SubnetFilter.byIds regardless of the subnet type

Current Behavior

When creating a subnet selection with only the SubnetFilter.byIds, the selection if empty if the subnets are not of type PRIVATE_WITH_EGRESS.

Reproduction Steps

The const subnets = vpc.selectSubnets({ subnetFilters: [ec2.SubnetFilter.byIds(["subnet-0000000000"]) });will return [] if the subnet ids I filter for (here the ["subnet-0000000000"] ) is type = public. If I do a const subnets = vpc.selectSubnets({ subnetFilters: [ec2.SubnetFilter.byIds(["subnet-0000000000"])], subnetType: ec2.SubnetType.PUBLIC }); it will find the subnet and return["subnet-0000000000"]

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.144.0 (build 5fb15bc)

Framework Version

No response

Node.js Version

v22.2.0

OS

Linux 5.15.153.1-microsoft-standard-WSL2

Language

TypeScript

Language Version

No response

Other information

No response

sbidy commented 2 months ago

The changes from #24625 seems to be there and not reverted. https://github.com/aws/aws-cdk/blob/8d55d864183803e2e6cfb3991edced7496eaadeb/packages/aws-cdk-lib/aws-ec2/lib/subnet.ts#L155

khushail commented 2 months ago

@sbidy thanks for reporting this. Looks like this change was ommitted from the final commit -https://github.com/aws/aws-cdk/pull/24625/commits/0577713897fa02db48bb5d36b5057184d617576b

sbidy commented 2 months ago

@khushail I will check if the changes from the commit fixing the issue. And if yes, I will resubmit the PR.

ajupatil commented 1 month ago

@khushail I used all possible types of subnets but CDK still did not find the subnets. The code exits with error on last else if as there are no public subnets.

let subnets = vpc.selectSubnets({ subnetFilters: [ec2.SubnetFilter.byIds(props.vpcSubnets)], }); if (subnets.subnetIds.length == 0) { console.log("Found subnets 👉", subnets.subnetIds); // try to find isolated ones subnets = vpc.selectSubnets({ subnetFilters: [ec2.SubnetFilter.byIds(props.vpcSubnets)], subnetType: ec2.SubnetType.PRIVATE_ISOLATED, }); } else if (subnets.subnetIds.length == 0) { console.log("Found subnets 👉", subnets.subnetIds); // try to find private egress ones subnets = vpc.selectSubnets({ subnetFilters: [ec2.SubnetFilter.byIds(props.vpcSubnets)], subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS, }); } else if (subnets.subnetIds.length == 0) { console.log("Found subnets 👉", subnets.subnetIds); // try to find public ones subnets = vpc.selectSubnets({ subnetFilters: [ec2.SubnetFilter.byIds(props.vpcSubnets)], subnetType: ec2.SubnetType.PUBLIC, }); }

khushail commented 1 month ago

@sbidy , @ajupatil , I tried to filter the ids and here is my code which gave me the subnet id using selectSubnet(), I have public subnets in my VPC which were printed -

Code -

    const vpc = ec2.Vpc.fromLookup(this, 'Vpc',{
      vpcId : "vpc-09b4a3571058b37a6"
    });
    const subnets = vpc.selectSubnets({
      subnetFilters: [ ec2.SubnetFilter.byIds(['subnet-0427cfeac57da7453'])]
    })

    new cdk.CfnOutput(this, 'SubnetIds', { value: subnets.subnetIds.join(',') })
  }

Snapshot for output filtered -

Screenshot 2024-07-31 at 12 29 39 PM

Please let me know if this does not work for you. Thanks!

charliejllewellyn commented 3 weeks ago

I am also seeing this on cdk v2 1.154.1.

        subnet_selection = ec2.SubnetSelection(
            subnet_filters=[ec2.SubnetFilter.by_ids(public_subnet)]
        )

[Error at /resource/subnet_name_a/ec2-listener] Did not find any subnets matching '{"subnetFilters":[{"subnetIds":["subnet-0d481159fe35ef54h"]}]}', please use a different selection.

If I replace the above with a private subnet ID it returns correctly.