(CDK) How to change subnets' routetables

0

I'm trying to define custom CfnRouteTable for subnets in the VPC to use, because it's redundant and inefficient for each subnet to have its own RouteTable.

However, it appears the RouteTable property for ISubnet is read-only. How is it supposed to be done?

Edited by: icelava on Jul 27, 2021 12:29 AM

icelava
질문됨 3년 전2177회 조회
1개 답변
0

Don't know if this is the proper way or not, but had to go through quite a convoluted way to re-associate the subnets to another routetable.

Sample procedure.

// Custom route table and routes.
var customRtName = "CustomRouteTable";
var elbRouteTable = new CfnRouteTable(vpc, customRtName,
new CfnRouteTableProps
{
VpcId = vpc.VpcId,
});
elbRouteTable.Node.AddDependency(vpc.PublicSubnets);
elbRouteTable.Node.AddDependency(vpc.IsolatedSubnets);

// Looks like the ultimate name given to the custom RouteTable won't have "CustomRouteTable" in the output template;  
// only goes as far as its parent scope "Ec2SetupStack/cdk_ec2_vpc"; it has to be manually revised.  
var revisedName = vpc.Stack.StackName _ "/" _ vpc.Node.Id _ "/" _ customRtName;  
Amazon.CDK.Tags.Of(elbRouteTable).Add("Name", revisedName);  

var internetRoute = new CfnRoute(elbRouteTable, "InternetRoute",  
new CfnRouteProps  
{  
    RouteTableId = elbRouteTable.Ref,  
    DestinationCidrBlock = internetCidr,  
    GatewayId = vpc.InternetGatewayId  
});  
internetRoute.Node.AddDependency(elbRouteTable);  

this.ReAssociateRouteTable(vpc, vpc.PublicSubnets, elbRouteTable);  
this.ReAssociateRouteTable(vpc, vpc.IsolatedSubnets, elbRouteTable);  

}

private void ReAssociateRouteTable(Construct scope, ISubnet[] subnets, CfnRouteTable routeTable)
{
foreach (var subnet in subnets)
{
var routeTableAssoc = new CfnSubnetRouteTableAssociation(scope, subnet.Node.Id _ "_" _ routeTable.Node.Id,
new CfnSubnetRouteTableAssociationProps
{
SubnetId = subnet.SubnetId,
RouteTableId = routeTable.Ref
});
}
}

Edited by: icelava on Jul 27, 2021 2:29 AM - add routetable dependency on subnets to avoid association conflict on deploying brand new stack.

icelava
답변함 3년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠