(ec2): cannot provide availabilityZones to VPC
See original GitHub issueDescribe the bug
I cannot use the availabilityZones prop for VPC without it throwing an error about my stack not being able to use those AZs. I can only use dummy1a
etc.
Error message:
Given VPC 'availabilityZones' us-east-1a must be a subset of the stack's availability zones dummy1a,dummy1b,dummy1c
The docs state that:
The number of Availability Zones that are available depends on the region and account of the Stack containing the VPC. If the region and account are specified on the Stack, the CLI will look up the existing Availability Zones and get an accurate count.
However, specifying the environment on the stack will not force a lookup for viable AZs even when supplying AZs to the availabilityZones prop
The described workaround overriding get availabilityZones()
works, however that should not be necessary to be able to specify the AZs to use in a new VPC
Expected Behavior
VPC to use supplied AZs
Current Behavior
Throws error because the usable Stack AZs are dummy values
Reproduction Steps
Specify a new VPC
new Vpc(this, 'vpc', {
availabilityZones: ['us-east-1a']
})
In the stack I’ve supplied an account number as well as the region. Interestingly enough, not supplying the environment to the stack will lead to successful synth
Possible Solution
The context lookup is not triggered for Stack.availabilityZones, but it should be triggered in this case, or there should be no check to make sure that the availability zones are valid.
Additional Information/Context
The issue goes away if you specify the availability zones based off the availability zones from the stack
const vpc = new ec2.Vpc(this, 'TestVPC', {
cidr: '10.0.0.0/16',
availabilityZones: cdk.Stack.of(this).availabilityZones.sort().slice(0,1)
})
This will ensure a lookup occurs
CDK CLI Version
2.38
Framework Version
No response
Node.js Version
.
OS
.
Language
Typescript
Language Version
No response
Other information
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:7 (1 by maintainers)
@gumonet you’re right that the context file isn’t getting created, the issue is that this is occurring during synth and no lookup is occurring at all for some reason. If a lookup were actually happening this wouldn’t be an issue, but I and some others have been encountering this issue so there is some inconsistency somewhere which is preventing a lookup from occurring.
@pradoz thanks for looking into this, but I can still reproduce this on latest version. It’s possible you already have the context file in your testing environment
Steps to reproduce:
cdk init
in new foldercdk synth
This also helped me create VPCs in a nested stack with cross account deploys, where the nested stack was unable to resolve the availability zones in the second account. Adding this function and providing the data from the parent stack as an argument made it work as expected.
I was also able to resolve this problem by manually adding the availability zones for the second account in the context file, but I’d like to avoid that manual step as much as possible