簡體   English   中英

如何使用 AWS CDK 迭代路由表關聯中的多個子網?

[英]How to iterate over multiple subnets in Route Table Associations with AWS CDK?

我有一個子網列表,我想迭代到我的公共和私有路由表中。 這是我的公共 RT 的 function 示例:

// 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]

我相信迭代會干擾我的 function 中的id 將不勝感激任何幫助解決這個問題。

你的懷疑是正確的。 您只需要為每個路由表關聯構造一個唯一的 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
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM