Skip to content

(vpc): context lookup does not occur/complete before vpc is referenced in other constructs #22025

@peterwoodworth

Description

@peterwoodworth

Describe the bug

This is a bit of a weird bug. If I try to lookup a vpc, and then try to select subnets based on that vpc, it will fail under certain conditions. When I try to use subnetFilters when selecting subnets following a lookup in my code, I will always get the error Cluster requires at least 2 subnets, got 0 before cdk.context.json is created. However if I instead try to filter by subnetType instead of with subnetFilters, a lookup will be successfully performed and the code will run normally.

Expected Behavior

For context lookup to occur before the vpc is referenced elsewhere

Current Behavior

Lookup does not complete and errors occur due to lack of subnets found

Reproduction Steps

Here's a minimal reproduction stack. Comment out subnetFilter and remove the comment on subnetType to create cdk.context.json. Once this file is created, you can remove the comment on subnetFilter` and successfully filter subnets.

    const vpc = ec2.Vpc.fromLookup(this, 'vpc',{
      vpcName: 'MyVpc'
    });

    const docDbCluster = new docdb.DatabaseCluster(this, 'DocDB', {
      masterUser: {
          username: 'myusername',
          excludeCharacters: ':*/?#[];%@"\'\\',
          secretName: `mysecretname`,
      },
      instanceType: ec2.InstanceType.of(
          ec2.InstanceClass.BURSTABLE3,
          ec2.InstanceSize.MEDIUM,
      ),
      vpcSubnets: {
          subnetFilters: [ 
            ec2.SubnetFilter.availabilityZones(["us-east-1a", "us-east-1b"]),
          ],
          // subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
      },
      vpc: vpc,
    });

Possible Solution

Lookup should fully complete before code referencing that lookup is ran. However, I'm not immediately sure why this is only occurring conditionally based on how subnets are selected from a looked up Vpc.

Additional Information/Context

May be related to #21690

CDK CLI Version

2.41.0

Framework Version

No response

Node.js Version

16

OS

mac

Language

Typescript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

@aws-cdk/aws-ec2Related to Amazon Elastic Compute CloudbugThis issue is a bug.effort/mediumMedium work item – several days of effortp2

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions