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
andPRIVATE_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.
- We created a VPC that has 2 subnet groups – 1
PUBLIC
and 1PRIVATE_ISOLATED
. Because we set themaxAzs
prop to 3, this configuration will create a total of 6 subnets. Each subnet group creates a subnet in every availability zone. - We defined a
tagAllSubnets
a function that takes 3 parameters:
subnets
– an array of subnets to tagtagName
– the name of the tag to apply on the subnets in the arraytagValue
– the value of the tax you would have to customize this function to the tagging conventions your organization follows.
- We used the
tagAllSubnets
function to addName
andenv
tags to our subnets. TheName
of a subnet is now going to look likecdk-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.
Each subnet is associated with a route table, so the subnet tags also got applied to the route tables:
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
:
span 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.