如何在 Route Table Associations with AWS CDK 中迭代多个子网?
How to iterate over multiple subnets in Route Table Associations with AWS CDK?
我有一个子网列表,我想将其迭代到我的 public 和私有路由 table 中。这是我的 public RT:
的函数示例
// This will grab the public RT and associate all public subnets to the RT.
props.pubSubnetId.forEach((public_subnets) => {
const publicRTAssoc = new ec2.CfnSubnetRouteTableAssociation(this, "publicRTAssoc", {
routeTableId: props.pubRouteTableId,
subnetId: public_subnets
});
});
我没有发现我的代码有什么问题,但是当我 运行 cdk synth
时,我得到了这个错误:
Error: There is already a Construct with name 'publicRTAssoc' in CloudformationArchStack [CloudformationArchStack]
我认为迭代干扰了我函数中的 id
。非常感谢解决此问题的任何帮助。
你的怀疑是正确的。你只需要给每个路由 table 关联构造一个唯一的 id:
// This will grab the public RT and associate all public subnets to the RT.
props.pubSubnetId.forEach((public_subnets) => {
const publicRTAssoc = new ec2.CfnSubnetRouteTableAssociation(this, `publicRTAssoc_${public_subnets}`, {
routeTableId: props.pubRouteTableId,
subnetId: public_subnets
});
});
我有一个子网列表,我想将其迭代到我的 public 和私有路由 table 中。这是我的 public RT:
的函数示例// This will grab the public RT and associate all public subnets to the RT.
props.pubSubnetId.forEach((public_subnets) => {
const publicRTAssoc = new ec2.CfnSubnetRouteTableAssociation(this, "publicRTAssoc", {
routeTableId: props.pubRouteTableId,
subnetId: public_subnets
});
});
我没有发现我的代码有什么问题,但是当我 运行 cdk synth
时,我得到了这个错误:
Error: There is already a Construct with name 'publicRTAssoc' in CloudformationArchStack [CloudformationArchStack]
我认为迭代干扰了我函数中的 id
。非常感谢解决此问题的任何帮助。
你的怀疑是正确的。你只需要给每个路由 table 关联构造一个唯一的 id:
// This will grab the public RT and associate all public subnets to the RT.
props.pubSubnetId.forEach((public_subnets) => {
const publicRTAssoc = new ec2.CfnSubnetRouteTableAssociation(this, `publicRTAssoc_${public_subnets}`, {
routeTableId: props.pubRouteTableId,
subnetId: public_subnets
});
});