question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(ec2): cannot provide availabilityZones to VPC

See original GitHub issue

Describe 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:open
  • Created a year ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
peterwoodworthcommented, Aug 30, 2022

@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:

  1. cdk init in new folder
  2. Supply environment variables
  3. Add env to stack (both explicit and using env variables creates error)
  4. Add Vpc to stack
    new Vpc(this, 'vpc', {
      availabilityZones: ['us-east-1a']
    })
  1. cdk synth
0reactions
karrthcommented, Nov 22, 2022

I was also able to work around by adding this method to the NestedStack that contains the VPC construct (TypeScript):

get availabilityZones() {
  return this.customAvailabilityZones;
}

I set this.customAvailabilityZones in the NestedStack constructor and it seems to work.

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot issues with Amazon VPC interface endpoints
You can only select an Availability Zone that corresponds to Availability Zones enabled on the Network Load Balancer of the provider VPC. The ......
Read more >
Cannot route between subnets in different availability zones in ...
Cannot route between subnets in different availability zones in AWS VPC ... I have created a VPC on EC2 with 3 subnets. VPC:...
Read more >
Add or remove subnets for your Classic Load Balancer in a VPC
For load balancers in a VPC, we recommend that you add one subnet per Availability Zone for at least two Availability Zones. This...
Read more >
Availability Zone Archives - Jayendra's Cloud Certification Blog
AWS Networking Services · VPC Peering can be performed across VPC in the same account of different AWS accounts but only within the...
Read more >
How do I move my EC2 instance to another subnet ... - YouTube
AWS KC Videos: How do I move my EC2 instance to another subnet, Availability Zone, VPC, or region? Watch later. Share. Copy link....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found