You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[ec2-vpc]: SubnetSelection with AZs with vpc contruct in same application #27540
We identified an issue: the .selectSubnets() method doesn't function with availabilityZones when the VPC is within the same application, example snippet:
Upon verification, it's apparent that the AZ value acts as the TOKEN placeholder during the stack's synthesis, leading the above selectedSubnets to return empty. Notably, this operates correctly if the VPC is imported from an existing one.
Should this be intended behavior, might we request an update to the documentation, specifically regarding the availabilityZones parameter within the SubnetSelection interface, to prevent future confusion?
If you create a new VPC in the app, you will not now the selected subnetIds until deployment so you will only get the tokens.
I'm still quite confused here. What is the correct solution then to select subnets by availabilityZones? I am creating a VPC in one stack and then trying to do subnet selection in a separate stack so the VPC and subnets should already be created at this point.
Can you specify which document statement should be improved?
In addition to documentation, shouldn't this throw an error at compile time letting you know you're doing something wrong using the Token.isUnresolved function? Other constructs will throw: ValidationError: All arguments to TK must be concrete (no Tokens)
I found the answer - it turns out that if we are using intrinsics (i.e. Fn::GetAZs) then we cannot use vpc.selectSubnets with availabilityZones. Instead, use explicit AZ names as strings.
Documents synthesis-time matching of literal AZ names against subnet AZs, and shows an explicit-AZ VPC example. Verified both the working example and the unresolved-token empty selection; Rosetta and all 187 VPC tests pass.
The PR includes a closing reference for this issue; closure will follow merge.
Activity
pahud commentedon Oct 17, 2023
If you create a new VPC in the app, you will not now the selected subnetIds until deployment so you will only get the tokens.
Can you specify which document statement should be improved?
annguyen36 commentedon Oct 17, 2023
Hi pahud,
Thanks for confirmation.
I would suggest add this in the availibilityZones prop description in this doc https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.SubnetSelection.html#availabilityzones
AlJohri commentedon May 24, 2025
I'm still quite confused here. What is the correct solution then to select subnets by availabilityZones? I am creating a VPC in one stack and then trying to do subnet selection in a separate stack so the VPC and subnets should already be created at this point.
In addition to documentation, shouldn't this throw an error at compile time letting you know you're doing something wrong using the Token.isUnresolved function? Other constructs will throw:
ValidationError: All arguments to TK must be concrete (no Tokens)Potentially related issues:
AlJohri commentedon May 27, 2025
I found the answer - it turns out that if we are using intrinsics (i.e.
Fn::GetAZs) then we cannot usevpc.selectSubnetswithavailabilityZones. Instead, use explicit AZ names as strings.caglarkc commentedon Oct 15, 2026
Implemented in #39283.
Documents synthesis-time matching of literal AZ names against subnet AZs, and shows an explicit-AZ VPC example. Verified both the working example and the unresolved-token empty selection; Rosetta and all 187 VPC tests pass.
The PR includes a closing reference for this issue; closure will follow merge.