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

Add CDK Example: How to do a lookup by subnet group name #830

Open
1 of 2 tasks
takashi-uchida opened this issue Apr 10, 2023 · 2 comments
Open
1 of 2 tasks

Add CDK Example: How to do a lookup by subnet group name #830

takashi-uchida opened this issue Apr 10, 2023 · 2 comments
Labels
feature-request A feature should be added or improved. language/python Related to Python examples p2

Comments

@takashi-uchida
Copy link

Describe the feature

The subnet group name can be used to refer to the intended subnet quickly.

Use Case

Many samples reference subnets created in the VPC stack, such as ec2.SubnetSelection is handled by a subnet type call, list, or import function.
However, in actual requirements, many requirements cannot be divided by subnet type, such as subnets for network firewalls, transit gateways, and so on.

Proposed Solution

subnet_group_name_tag and cdk.json

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Language

Python

@takashi-uchida takashi-uchida added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Apr 10, 2023
@ericzbeard ericzbeard removed the needs-triage This issue or PR still needs to be triaged. label Jul 14, 2023
@kaiz-io kaiz-io added language/python Related to Python examples p2 labels Oct 24, 2023
@rushali-aws
Copy link
Contributor

@takashi-uchida , I checked that in subnet Selection we can use subnetGroupName. During my testing , I noticed that CDK uses the tag "aws-cdk:subnet-name" for subnetGroupName.

I added the tag 'aws-cdk:subnet-name' to my subnets in a VPC with value 'Private-tag' and used the below code :

vpc = ec2.Vpc.from_lookup(self, "MyVpc",
            vpc_id='vpc-XXXXXXXXX'
        )

        subnet_ids = vpc.select_subnets(
            subnet_group_name="Private-tag"
        ).subnet_ids

        for subnet_id in subnet_ids:
            print("Subnet Ids: " + subnet_id)

I was able to check in my cdk.context.json file , under subnetGroups , there were subnets with this tag. And in the output of " print("Subnet Ids: " + subnet_id)" , I could see my subnets on which I added the tag.

You can add tag "aws-cdk:subnet-name" on the required subnets with same value and then use above way to select them by subnetGroupName.

@takashi-uchida
Copy link
Author

takashi-uchida commented Mar 20, 2024

Thank you. I have been handling it as follows.

self.vpc = ec2.Vpc(
            self,
            "vpc",
            vpc_name=f"vpc-{project_name}",
            ip_addresses=ec2.IpAddresses.cidr(vpc_cidr),
            max_azs=3,
            subnet_configuration=[
                ec2.SubnetConfiguration(
                    name=f"public-{project_name}",
                    cidr_mask=24,
                    subnet_type=ec2.SubnetType.PUBLIC,
                ),
                ec2.SubnetConfiguration(
                    name=f"private-{project_name}",
                    cidr_mask=24,
                    subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS,
                ),
                ec2.SubnetConfiguration(
                    name=f"isolated-{project_name}",
                    cidr_mask=24,
                    subnet_type=ec2.SubnetType.PRIVATE_ISOLATED,
                ),
                ec2.SubnetConfiguration(
                    name=f"tgw-{project_name}",
                    cidr_mask=24,
                    subnet_type=ec2.SubnetType.PRIVATE_ISOLATED,
                ),
            ],
        )
vpc = ec2.Vpc.from_lookup(
            self, "vpc", vpc_name=vpc_name, subnet_group_name_tag="aws-cdk:subnet-name"
        )


vpc_subnets=ec2.SubnetSelection(
                subnet_group_name="private-{project_name}"),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved. language/python Related to Python examples p2
Projects
None yet
Development

No branches or pull requests

4 participants