How to Tag Subnets in AWS CDK

avatar

Borislav Hadzhiev

Last updated: Apr 15, 2022

banner

Check out my new book

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 PRIVATE_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-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: 0, 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 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:

shell
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 section

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:

shell
npx aws-cdk destroy

Further Reading #

Use the search field on my Home Page to filter through my more than 3,000 articles.