当其私有子网已存在时,使用 CDK(打字稿)向路由 table 添加条目

Adding entry to route table with CDK (typescript) when its private subnet already exists

是否可以在打字稿中使用 CDK 添加条目到导入的私有子网路由 table?我正在导入 VPC:

import ec2 = require('@aws-cdk/aws-ec2');
vpc = ec2.Vpc.fromVpcAttributes(...)

fromVpcAttributes 上的文档:https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.Vpc.html#static-from-wbr-vpc-wbr-attributesscope-id-attrs), and its private subnets are therefore being imported as an array of ISubnets. I want to set up VPC Peering targets/destinations in each of these private subnets' route tables, and the most common way to do this seems to be via the Subnet's addRoute method (https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.Subnet.html#add-wbr-routeid-options). This works when the subnets are newly made, such as here: https://qiita.com/is_ryo/items/66dfe6c4b6dda4bd1eeb,但我的私有子网没有这种方法,因为它们是作为 ISubnets 导入的。有没有办法导入这些子网改为子网?或者,在这种情况下添加条目的更好方法?

我今天遇到了类似的情况,我可以通过实例化新的 CloudFormation Route 资源来解决这个问题:

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

您需要知道这些路由的对等连接 ID。在上面的示例中,它在同一堆栈中创建时被引用:

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

希望对您有所帮助!