Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 6, 2022 04:44 pm GMT

AWS: Using Existing Subnets with Availability Zones in CDK with TypeScript

If you see this error, you are not alone.

Error: You cannot reference a Subnet's availability zone if it was not supplied. Add the availabilityZone when importing using Subnet.fromSubnetAttributes()

My CDK code is referencing a preexisting VPC with preexisting Subnets. Since the VPC and Subnets are preexisting, I know the IDs of each ahead of time.

The problem occurs because certain other CDK objects need the Availability Zone property to exist on the Subnet object, but the Subnet.fromSubnetId() method does not return that property. As the error message says, you need to use the Subnet.fromSubnetAttributes() method, which will populate both the SubnetId and Availability Zone properties.

const subnetIds = [    "Subnet1_id",    "Subnet2_id",];const azones = new Map<string, string>([    ["Subnet1_id, "s1_avaiability_zone"],    ["Subnet2_id, "s2_avaiability_zone"],]);const subnets = subnetIds.map(subnetId => Subnet.fromSubnetAttributes(this, subnetId, {    subnetId: subnetId,    availabilityZone: azones.get(subnetId)}));const subnetSelection = { subnets };

First, I construct an array of Subnet IDs. Then I construct a Map (key, value pairs) of Subnet IDs and their Availability Zones. Then using the map() function, an array of Subnet objects gets returned. These Subnet objects will contain the Availability Zone property because I am using Subnet.fromSubnetAttributes(). Finally, I'm putting the Subnet array into a wrapper object which is what is expected from subsequent CDK objects.

Shout out to Guy Morton whose article helped me to develop this solution. See https://python.plainenglish.io/importing-existing-vpc-and-subnets-into-a-cdk-python-project-a707d61de4c3.


Original Link: https://dev.to/ellismichael/aws-using-existing-subnets-with-availability-zones-in-cdk-with-typescript-e7h

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To