Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_ec2: vpc.fromLookup doesn't set env.region correctly #22178

Open
cartalla opened this issue Sep 21, 2022 · 3 comments
Open

aws_ec2: vpc.fromLookup doesn't set env.region correctly #22178

cartalla opened this issue Sep 21, 2022 · 3 comments
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. effort/small Small work item – less than a day of effort p2

Comments

@cartalla
Copy link

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

@cartalla cartalla added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 21, 2022
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Sep 21, 2022
@peterwoodworth
Copy link
Contributor

Thanks for reporting this @cartalla, I can confirm this behavior hasn't been fixed. Outputting the region of a Vpc looked up from region us-east-1 in a stack deploying to us-west-2 will give me us-west-2.

@daschaa @TheRealAmazonKendra The integration test in the PR seems to cover this exact case, however it's not working in practice. Could you take another look at this?

@peterwoodworth peterwoodworth added p1 effort/small Small work item – less than a day of effort and removed needs-triage This issue or PR still needs to be triaged. labels Sep 21, 2022
@m00ki3
Copy link

m00ki3 commented Oct 5, 2022

yeah, we got around it with the following, manually specifying via VPCProperty in CfnHostedZone.

CfnHostedZone(
    self,
    "id goes here",
    name = "domain name goes here",
    vpcs = [
        CfnHostedZone.VPCProperty(vpc_id="us-east-1 VPC ID goes here", vpc_region = "us-east-1"),
        CfnHostedZone.VPCProperty(vpc_id="us-east-2 VPC ID goes here", vpc_region = "us-east-2")
    ]
)

@sumupitchayan sumupitchayan added p1.5 and removed p1 labels May 17, 2023
@otaviomacedo otaviomacedo added p2 and removed p1.5 labels May 22, 2023
PbVrCt added a commit to PbVrCt/ecs_workshop_test that referenced this issue Jun 4, 2023
There is a bug in the CDK that was preventing Vpc.from_lookup() from working:
aws/aws-cdk#22178
To solve it i changed the root project structure
from 4 separate repos each with its cdk app
to one cdk app with all the stacks
In doing so, I replaced
CfnOutput and Fn.import_value for explicitly passing the dependencies
between stacks as arguments.
@tvb
Copy link

tvb commented Sep 18, 2024

Fyi; this bug still exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. effort/small Small work item – less than a day of effort p2
Projects
None yet
7 participants