DEV Community

Mikaeel Khalid for AWS Community Builders

Posted on • Originally published at blog.mikaeels.com on

How to do Subnets Tagging in AWS CDK

Image description

Tagging subnets in AWS CDK is a great way to organize and manage your resources within your VPC. By using tags, you can easily identify and filter resources based on their purpose, environment, or any other criteria that are important to your organization. In this blog post, we will show you how to tag subnets in AWS CDK using a few examples.

First, let's start by creating a new VPC in AWS CDK. You can do this by using the Vpc class in the aws-cdk-lib package.

import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'my-stack');

const vpc = new ec2.Vpc(stack, 'my-vpc', {
  cidr: '10.0.0.0/16',
  natGateways: 0,
  subnetConfiguration: [
    {
      cidrMask: 24,
      name: 'public',
      subnetType: ec2.SubnetType.PUBLIC
    },
    {
      cidrMask: 24,
      name: 'private',
      subnetType: ec2.SubnetType.PRIVATE
    }
  ]
});

Enter fullscreen mode Exit fullscreen mode

In this example, we are creating a new VPC with a CIDR block of 10.0.0.0/16 and two subnets, one public and one private. The subnetConfiguration property is used to define the subnets, and the cidrMask property is used to specify the CIDR block for each subnet.

Next, we can add tags to the subnets using the addTags method. This method takes an object that contains the tag keys and values.

vpc.publicSubnet.addTags({
  'Environment': 'Prod',
  'Purpose': 'Web servers'
});

vpc.privateSubnet.addTags({
  'Environment': 'Prod',
  'Purpose': 'Database servers'
});

Enter fullscreen mode Exit fullscreen mode

In this example, we are adding two tags to the public subnet, "Environment" and "Purpose", and setting their values to "Prod" and "Web servers" respectively. We are also adding two tags to the private subnet, "Environment" and "Purpose", and setting their values to "Prod" and "Database servers" respectively.

Once you have tagged your subnets, you can use the AWS Management Console or the AWS CLI to filter and view your resources based on their tags. This can help you easily identify and manage your resources based on their purpose, environment, or any other criteria that is important to your organization.

In this way, you can use the addTags method to tag the subnets in your VPC. This is a powerful way to organize and manage your resources in AWS CDK, and it can help you easily identify and filter resources based on their purpose, environment, or any other criteria that is important to your organization.

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Best Practices for Running  Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK cover image

Best Practices for Running Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK

This post discusses the process of migrating a growing WordPress eShop business to AWS using AWS CDK for an easily scalable, high availability architecture. The detailed structure encompasses several pillars: Compute, Storage, Database, Cache, CDN, DNS, Security, and Backup.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay