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-ec2: SubnetFilter byIds does not work as expected when using vpc.selectSubnets() on non-private/mixed subnet types. #30826

Open
sbidy opened this issue Jul 11, 2024 · 6 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

@sbidy
Copy link

sbidy commented Jul 11, 2024

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 sbidy added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 11, 2024
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Jul 11, 2024
@sbidy
Copy link
Author

sbidy commented Jul 11, 2024

The changes from #24625 seems to be there and not reverted.

return subnets.filter(subnet => this.subnetIds.includes(Token.asString(subnet.subnetId)));

@khushail khushail added the needs-reproduction This issue needs reproduction. label Jul 11, 2024
@khushail khushail self-assigned this Jul 11, 2024
@khushail khushail added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-triage This issue or PR still needs to be triaged. needs-reproduction This issue needs reproduction. labels Jul 11, 2024
@khushail
Copy link
Contributor

khushail commented Jul 11, 2024

@sbidy thanks for reporting this. Looks like this change was ommitted from the final commit -0577713

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

sbidy commented Jul 12, 2024

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

@ajupatil
Copy link

ajupatil commented Jul 29, 2024

@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 khushail added p1 p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed p2 p1 labels Jul 29, 2024
@khushail
Copy link
Contributor

khushail commented Jul 31, 2024

@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!

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jul 31, 2024
@charliejllewellyn
Copy link

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.

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

4 participants