How to Tag Subnets in AWS CDK

How to Tag Subnets in AWS CDK

In this post, we will give you information about How to Tag Subnets in AWS CDK. Here we will give you details about How to Tag Subnets in AWS CDKA and how to use it also give you a demo for it if it is necessary.

In AWS CDK (Cloud Development Kit), you can tag subnets just like you can tag other AWS resources. Tags are key-value pairs that provide metadata and help you organize and manage your resources. Tagging subnets in CDK is typically done using the aws_ec2.CfnSubnet resource and its tags property. Here’s how you can tag subnets in AWS CDK. We’ll look at an example of how we can add tags to subnets in AWS CDK.

Especially useful is the Name tag, which helps us distinguish between resources in the VPC management console.

Let’s look at an example where we:

  • Create a VPC with 2 subnet groups – PUBLIC and PRIVATE_ISOLATED
  • define a reusable function for tagging subnets
  • tag the subnets

The code for this article is available on GitHub

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

export class CdkStarterStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // create VPC
    const vpc = new ec2.Vpc(this, 'my-cdk-vpc', {
      cidr: '10.0.0.0/16',
      natGateways: ,
      maxAzs: 3,
      subnetConfiguration: [
        {
          name: 'public-subnet-1',
          subnetType: ec2.SubnetType.PUBLIC,
          cidrMask: 24,
        },
        {
          name: 'isolated-subnet-1',
          subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
          cidrMask: 28,
        },
      ],
    });

    //  define function that tags subnets
    const tagAllSubnets = (
      subnets: ec2.ISubnet[],
      tagName: string,
      tagValue: string,
    ) => {
      for (const subnet of subnets) {
        cdk.Tags.of(subnet).add(
          tagName,
          `${tagValue}-${subnet.availabilityZone}`,
        );
      }
    };

    //  tag subnets
    const {stackName} = cdk.Stack.of(this);
    tagAllSubnets(vpc.publicSubnets, 'Name', `${stackName}/public`);
    tagAllSubnets(vpc.isolatedSubnets, 'Name', `${stackName}/isolated`);

    tagAllSubnets(vpc.publicSubnets, 'env', 'staging');
    tagAllSubnets(vpc.isolatedSubnets, 'env', 'dev');
  }
}

Let’s go over the code snippet.

  1. We created a VPC that has 2 subnet groups – 1 PUBLIC and 1 PRIVATE_ISOLATED. Because we set the maxAzs prop to 3, this configuration will create a total of 6 subnets. Each subnet group creates a subnet in every availability zone.
  2. We defined a tagAllSubnets a function that takes 3 parameters:
  • subnets – an array of subnets to tag
  • tagName – the name of the tag to apply on the subnets in the array
  • tagValue– the value of the tax you would have to customize this function to the tagging conventions your organization follows.
  1. We used the tagAllSubnets function to add Name and env tags to our subnets. The Name of a subnet is now going to look like cdk-stack/public-us-east-1a.

Let’s provision the resources:

npx aws-cdk deploy

After a successful deployment, we can see that the Name tags have been applied to the subnets.

subnets name tag

Each subnet is associated with a route table, so the subnet tags also got applied to the route tables:

route tables tagged

The only route table that didn’t get tagged is the main one, which has no subnet associations.

The complete tag section of a subnet shows both of the tags we have added – Name and env:

subnet tag sectionspan class=”break-words”>The most important thing when tagging AWS resources is to follow a convention.

Clean up

To delete the resources we have provisioned, run the destroy command:

npx aws-cdk destroy

If you’d like to read more on using tags in AWS CDK, check out the following article.

Conclusion for How to Tag Subnets in AWS CDK

Hope this code and post will help you implement How to Tag Subnets in AWS CDK. if you need any help or any feedback give it in the comment section or if you have a good idea about this post you can give it in the comment section. Your comment will help us to help you more and improve us.

For More Info See:: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

4  +  3  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US