How to Tag Subnets in AWS CDK

avatar

Borislav Hadzhiev

Wed May 05 20212 min read

A complete example of tagging subnets in AWS CDK. We provision a VPC with 2 subnet groups and tag every single subnet.

Tagging Subnets in AWS CDK #

In this article 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 ISOLATED
  • define a reusable function for tagging subnets
  • tag the subnets
The code for this article is available on GitHub
lib/cdk-starter-stack.ts
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';

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: 0,
      maxAzs: 3,
      subnetConfiguration: [
        {
          name: 'public-subnet-1',
          subnetType: ec2.SubnetType.PUBLIC,
          cidrMask: 24,
        },
        {
          name: 'isolated-subnet-1',
          subnetType: ec2.SubnetType.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 ISOLATED. Because we've 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 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 tag

    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:

lib/cdk-starter-stack.ts
npx 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 section

The most important thing when tagging AWS resources is to follow a convention.

Clean up #

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

shell
npx cdk destroy

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee