I am creating a default VPC using AWS CDK, and I want to update the main route table the Vpc construct creates with a Name tag. Below is the sample code of how I'm creating the VPC:
from aws_cdk import aws_ec2 as ec2
from constructs import Construct
class Vpc(Construct):
def __init__(self, scope: Construct, construct_id: str, **kwargs):
super().__init__(scope, construct_id)
vpc = self.create_vpc()
# TODO: Add Name tag to main VPC route table here
def create_vpc(self) -> ec2.Vpc:
vpc_name = "TEST-VPC"
vpc = ec2.Vpc(
self, 'VPC',
cidr='10.10.0.0/24',
vpc_name=vpc_name
)
return vpc
Is it possible to get the main route table and tag it?
Perhaps this doesn't strictly answer the question, but it's a viable solution for anyone that needs to add a route to subnets using CDK.
const vpcPeerConnectionId = 'hard-coded-id'
const vpcCidr = '10.22.0.0/16'
this.vpc.privateSubnets.forEach((subnet, index) => {
let routeTableId = subnet.routeTable.routeTableId
new CfnRoute(this, 'PrivateSubnetPeeringConnectionRoute' + index, {
destinationCidrBlock: vpcCidr,
routeTableId: routeTableId,
vpcPeeringConnectionId: vpcPeerConnectionId
})
})
Example uses a VPC Peering connection. For other uses see this document:
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.CfnRoute.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With