Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I have a list of subnets that I would want to iterate into my public and private route table. Here is an example of my function for my 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
  });
});

I see nothing wrong with my code but when I run cdk synth, I get this error:

Error: There is already a Construct with name 'publicRTAssoc' in CloudformationArchStack [CloudformationArchStack]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I believe the iteration is interfering with the id in my function. Would appreciate any help in solving this problem.

>Solution :

Your suspicions were correct. You just need to give each route table association construct a unique 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
  });
});

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading