question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[aws-ec2] Add way to filter VPC subnets by tags

See original GitHub issue

A way to filter VPC subnets by tags. This is important to allow proper setup of systems with the CDK that have not been built with the CDK. No all systems are built with the best practices as these tend the change as the years go on anyways. Moving from Terraform, the VPC’s are not setup according to AWS standards (compared to the CDK) and this has some serious consequences as to “fix” the issue would require rebuilding system. That’s not ideal or really necessary if we had some control over which subnets to use VIA a filter.

Use Case

I am running into issues with codepipeline due to VPC subnets not being able to be filtered by a tag. I am coming from Terraform, having built most of our VPCs with: https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/2.25.0

This creates the subnets like:

olyott-sand-db-us-east-1a
olyott-sand-db-us-east-1b
olyott-sand-db-us-east-1c
olyott-sand-elasticache-us-east-1a
olyott-sand-elasticache-us-east-1b
olyott-sand-elasticache-us-east-1c
olyott-sand-private-us-east-1a
olyott-sand-private-us-east-1b
olyott-sand-private-us-east-1c
olyott-sand-public-us-east-1a
olyott-sand-public-us-east-1b
olyott-sand-public-us-east-1c

The issues I am having is that the db and elasticache subnets seem to be set to private. This leaves me with issues in the pipeline as this will build and deploy ECS to the DB and the cache subnets, which will always fail. These subnets behave as isolated but they are not identified as isolated.

It’s unrealistic to expect replacing all existing PROD VPCs with the new (proper) VPC using CDK. Especially since I cannot truly import like we can in Terraform. This leaves me in a state where I cannot use the CDK for codepipelines on any existing system. As the builds and deploys will work, at best. half the time.

What we need is a way to filter the subnets by the tag name, or any other tag for more control.

Proposed Solution

I am new to the CDK but perhaps a filterBy(tag, regexPattern|string) to the VPC for subnets. What I would like to do is use it like:

const _privateSubnets = vpc.privateSubnets.filterByTag('Name', /*(private)*/);

I tried to see if I could build this myself but I don’t see the metric possible to filter by. Perhaps a CloudFormation limitation?

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

This is a 🚀 Feature Request

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:15
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

15reactions
moelholmcommented, Jan 21, 2021

If your issue is that types are incorrectly recognized, you can use the aws-cdk:subnet-type tag to correct those. See here:

https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ec2-readme.html#importing-an-existing-vpc

A combination of aws-cdk:subnet-type and aws-cdk:subnet-name should allow you enough flexibility to achieve most VPC selections you need. If that’s not sufficient for you, #10112 will help once it lands.

That solution primarily works for subnets managed by CDK. I have a situation where the VPC was created by others. And now I want to filter by the name Tag. I believe this issue is still relevant. I can work a fix?

6reactions
rumesh-athucommented, Jan 26, 2022

Here is the fix which works for me.

Firstly, Add a new tag to each subnet group as below.

Tag

<KEY | VALUE>

For Private subnets

<subnet-groupname | private>

For DB subnets

<subnet-groupname | db>

Please follow the same for other subnets groups as well

Secondly, Use ec2.Vpc.fromLookup to retrieve VPC object as below

use above new subnet tag KEY in subnetGroupNameTag

import { Vpc } from "aws-cdk-lib/aws-ec2";

const vpc = Vpc.fromLookup(this, "vpc", {
  vpcId: "vpc-1234567890",
  subnetGroupNameTag: "subnet-groupname",
});

Thirdly, Add subnetGroupName to vpc.selectSubnet

use above new subnet tag VALUE in subnetGroupName

vpc.selectSubnets({
  subnetGroupName: "private",
  availabilityZones: [az],
  onePerAz: true,
}),

Referred documents VpcLookupOptions SubnetSelection

Note: Please run cdk context --clear to clear the cdk.context.json prior to verify this code

Read more comments on GitHub >

github_iconTop Results From Across the Web

List and filter your resources - Amazon Elastic Compute Cloud
List and filter your different AWS resources. ... instance in the Amazon EC2 console, or choose a subnet ID to open the subnet...
Read more >
Fetch private subnet through name filter
If you want to filter for subnets that contain private in the Name tag, you can use: aws ec2 describe-subnets --filters "Name=tag:Name ...
Read more >
Create an AWS VPC | Anthos clusters on AWS
All of these subnets are tagged for subnet auto-discovery. ... --filters 'Name=tag:Name,Values= AMC_PREFIX ... aws ec2 create-route-table --vpc-id $VPC_ID \
Read more >
describe-subnets — AWS CLI 2.9.10 Command Reference
For more information, see Your VPC and subnets in the Amazon Virtual Private Cloud ... aws ec2 describe-subnets \ --filters "Name=tag:CostCenter,Values=123" ...
Read more >
How to Tag Subnets in AWS CDK
create a VPC with 2 subnet groups - PUBLIC and PRIVATE_ISOLATED; define a reusable function for tagging subnets; tag the subnets. The code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found