aws-cdk: (vpc): context lookup does not occur/complete before vpc is referenced in other constructs
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
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 4
- Comments: 16 (7 by maintainers)
I think I was wrong.
I just tested this without the existence of cdk.context.json and it can successfully synthesize.
Let’s first discuss the EKS issue here
And you are seeing
Actually I can synthesize this even before cdk.context.json is generated
Looking at the code here
https://github.com/aws/aws-cdk/blob/4034adb5e4453435b959fde5eea16a7824f21e73/packages/aws-cdk-lib/aws-eks/lib/cluster.ts#L630-L636
and
https://github.com/aws/aws-cdk/blob/4034adb5e4453435b959fde5eea16a7824f21e73/packages/aws-cdk-lib/aws-eks/lib/cluster.ts#L1557-L1559
this option requires
privateSubnets.length != 0which comes from herehttps://github.com/aws/aws-cdk/blob/4034adb5e4453435b959fde5eea16a7824f21e73/packages/aws-cdk-lib/aws-eks/lib/cluster.ts#L1537C5-L1537C48
and looks like this function doesn’t think you are having private subnets hence the error.
https://github.com/aws/aws-cdk/blob/4034adb5e4453435b959fde5eea16a7824f21e73/packages/aws-cdk-lib/aws-eks/lib/cluster.ts#L1933-L1962
Now, let’s experiment a little bit like this:
(please update the sample above with your subnet group name and VPC ID)
Now, try
cdk difforcdk synth. It should print out the number of the private subnets and their subnet IDs like this:Can you verify if it’s returning any private subnet IDs? I guess for some reason selectPrivateSubnets() just doesn’t find any private subnets based on its logic.
Hi @peterwoodworth, Could you help please, I’m very stuck on this one.
I have the same issue where
cdk.context.jsonisn’t getting populated with the AZs for myenv. The difference with me is I’m trying to select a subnet to use to create an EC2 instance. I’ve tried usingvpc.select_subnets(subnet_type=ec2.SubnetType.PUBLIC)and I get back['dummy1a', 'dummy1b', 'dummy1c']. Then if I try and narrow it down to one subnet when creating the EC2 instance:I get an
IndexError: list index out of range.doing `print(self.availability_zones)’ returns the expected result, but doesn’t cache it in the context file.
Thank you in advance and let me know if you want me to create a seperate ticket for this. Scott
Thanks for posting your findings @arewa, this is a big help!