pythonroutesaws-cdkroutetable

cdk python making route table entries


Is it possible to add route entries to a route table via cdk?

The way I tried is: using a vpc construct I tried to iterate over the public_subnets + private_subnets attributes lists to get the routeTables. But these return IRouteTable -- with which I can't seem to do any updates. Anyone know how to do it? Thanks.


Solution

  • You can able to solve this by instantiating new CloudFormation Route resources:

    vpc.privateSubnets.forEach(({ routeTable: { routeTableId } }, index) => {
      new CfnRoute(stack, 'PrivateSubnetPeeringConnectionRoute' + index, {
        destinationCidrBlock: '10.0.0.0/16',
        routeTableId,
        vpcPeeringConnectionId: peeringConnection.ref,
      })
    })
    

    You will need to know the ID of the peering connection for those routes. In the example above, it's referenced as it's created in the same stack:

    const peeringConnection = new CfnVPCPeeringConnection(
      stack,
      'PeeringConnection',
      {
        peerVpcId: peerVpc.vpcId,
        vpcId: vpc.vpcId,
      }
    )
    

    Consider [1] , [2] and [3] for more details

    [1] https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.Vpc.html#static-from-wbr-vpc-wbr-attributesscope-id-attrs

    [2] https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.Subnet.html#add-wbr-routeid-options

    [3] https://qiita.com/is_ryo/items/66dfe6c4b6dda4bd1eeb