aws / aws/aws-cdk

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

Open
#22,025 22 comments 5 reactions 1 assignee Claimed by @shikha372 View on GitHub
@aws-cdk/aws-ec2 bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### 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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.