aws_ec2: vpc.fromLookup doesn't set env.region correctly
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the bug
The context is that the bug fix for #20496 and #20530 did not resolve the problem with the Route53.add_vpc() call not setting the region for the VPC correctly. The addVpc function is now using vpc.env.region if it is set, but the problem is that it is not set correctly to match the value in cdk.context.json and set the region from the stack instead.
### Expected Behavior
The template should have the correction region set for the VPC.
```
"PrivateDnsE0FF4F9B": {
"Type": "AWS::Route53::HostedZone",
"Properties": {
"Name": "slurmiad.local.",
"VPCs": [
{
"VPCId": "vpc-nnnnnnnnnnnnnnnnn",
"VPCRegion": "us-east-1"
},
{
"VPCId": "vpc-nnnnnnnnnnnnnnnnn",
"VPCRegion": "us-west-2"
},
{
"VPCId": "vpc-nnnnnnnnnnnnnnnnn",
"VPCRegion": "eu-west-1"
}
]
},
"Metadata": {
"aws:cdk:path": "slurmiad/PrivateDns/Resource"
}
},
```
### Current Behavior
The resulting CFN template has the correct VpcIds, but the incorrect regions:
```
"PrivateDnsE0FF4F9B": {
"Type": "AWS::Route53::HostedZone",
"Properties": {
"Name": "slurmiad.local.",
"VPCs": [
{
"VPCId": "vpc-nnnnnnnnnnnnnnnnn",
"VPCRegion": "us-east-1"
},
{
"VPCId": "vpc-nnnnnnnnnnnnnnnnn",
"VPCRegion": "us-east-1"
},
{
"VPCId": "vpc-nnnnnnnnnnnnnnnnn",
"VPCRegion": "us-east-1"
}
]
},
"Metadata": {
"aws:cdk:path": "slurmiad/PrivateDns/Resource"
}
},
```
### Reproduction Steps
I'm creating a Route53.HostedZone for use in 3 VPCs that are located in 3 different regions.
The VPCs aren't part of the stack and are created in CDK using
```
ec2.Vpc.from_lookup(
self, f"Vpc{region_dict['Region']}",
region = region_dict['Region'],
vpc_id = region_dict['VpcId'])
```
This causes an update to cdk.context.json where the VPC ids and regions are correct.
Extract from cdk.context.json:
```
"vpc-provider:account=nnnnnnnnnnnn:filter.vpc-id=vpc-nnnnnnnnnnnnnnnnn:region=us-west-2:returnAsymmetricSubnets=true": {
"vpcId": "vpc-nnnnnnnnnnnnnnnnn",
"vpcCidrBlock": "10.3.0.0/16",
"availabilityZones": [],
"subnetGroups": [
{
"name": "Private",
"type": "Private",
"subnets": [
{
"subnetId": "subnet-nnnnnnnnnnnnnnnnn",
"cidr": "10.3.64.0/18",
"availabilityZone": "us-west-2a",
"routeTableId": "rtb-nnnnnnnnnnnnnnnnn"
},
{
"subnetId": "subnet-nnnnnnnnnnnnnnnnn",
"cidr": "10.3.128.0/18",
"availabilityZone": "us-west-2b",
"routeTableId": "rtb-nnnnnnnnnnnnnnnnn"
},
{
"subnetId": "subnet-nnnnnnnnnnnnnnnnn",
"cidr": "10.3.192.0/18",
"availabilityZone": "us-west-2c",
"routeTableId": "rtb-nnnnnnnnnnnnnnnnn"
}
]
},
{
"name": "Public",
"type": "Public",
"subnets": [
{
"subnetId": "subnet-nnnnnnnnnnnnnnnnn",
"cidr": "10.3.0.0/26",
"availabilityZone": "us-west-2a",
"routeTableId": "rtb-nnnnnnnnnnnnnnnnn"
},
{
"subnetId": "subnet-nnnnnnnnnnnnnnnnn",
"cidr": "10.3.0.64/26",
"availabilityZone": "us-west-2b",
"routeTableId": "rtb-nnnnnnnnnnnnnnnnn"
},
{
"subnetId": "subnet-nnnnnnnnnnnnnnnnn",
"cidr": "10.3.0.128/26",
"availabilityZone": "us-west-2c",
"routeTableId": "rtb-nnnnnnnnnnnnnnnnn"
}
]
}
]
},
```
I create the Hosted Zone:
```
self.hosted_zone = route53.HostedZone(self, "PrivateDns",
vpcs = [self.vpc],
zone_name = self.config['Domain']
)
self.hosted_zone.add_vpc(remote_vpcs[region_dict['Region']])
```
### Possible Solution
I'm not familiar with the context provider, but it either it's not returning the correct region or the VPC constructor isn't using it.
https://github.com/aws/aws-cdk/blob/v1-main/packages/%40aws-cdk/aws-ec2/lib/vpc.ts#L1167-L1178
### Additional Information/Context
_No response_
### CDK CLI Version
2.42.1
### Framework Version
_No response_
### Node.js Version
16.15.0
### OS
AmazonLinux2
### Language
Python
### Language Version
Python 3.7.10
### Other information
_No response_
Contributor guide
Research direction
Start with packages/@aws-cdk/aws-ec2/lib/vpc.ts at lines 1167-1178, then trace how from_lookup populates the VPC environment from cdk.context.json. Reproduce the three-region Route53 HostedZone case and compare the synthesized VPCRegion values with the context entries; done means each VPC retains its lookup region in the generated template.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100