Skip to content

Commit

Permalink
fix(route53): vpc region in template overridden by stack region (#20530)
Browse files Browse the repository at this point in the history
Fixes #20496 

This PR implements the proposed change in #20496 - When a region is set in the vpc it is used in the CloudFormation template. Otherwise the region from the respective stack is used.

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
daschaa authored Sep 7, 2022
1 parent b0ba52e commit aedc888
Show file tree
Hide file tree
Showing 17 changed files with 1,064 additions and 6 deletions.
17 changes: 14 additions & 3 deletions packages/@aws-cdk/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,13 @@ export interface VpcAttributes {
* VPN gateway's identifier
*/
readonly vpnGatewayId?: string;

/**
* The region the VPC is in
*
* @default - The region of the stack where the VPC belongs to
*/
readonly region?: string;
}

export interface SubnetAttributes {
Expand Down Expand Up @@ -1203,7 +1210,7 @@ export class Vpc extends VpcBase {
dummyValue: undefined,
}).value;

return new LookedUpVpc(scope, id, attributes || DUMMY_VPC_PROPS, attributes === undefined);
return new LookedUpVpc(scope, id, attributes ?? DUMMY_VPC_PROPS, attributes === undefined);

/**
* Prefixes all keys in the argument with `tag:`.`
Expand Down Expand Up @@ -2008,7 +2015,9 @@ class ImportedVpc extends VpcBase {
private readonly cidr?: string | undefined;

constructor(scope: Construct, id: string, props: VpcAttributes, isIncomplete: boolean) {
super(scope, id);
super(scope, id, {
region: props.region,
});

this.vpcId = props.vpcId;
this.vpcArn = Arn.format({
Expand Down Expand Up @@ -2058,7 +2067,9 @@ class LookedUpVpc extends VpcBase {
private readonly cidr?: string | undefined;

constructor(scope: Construct, id: string, props: cxapi.VpcContextResponse, isIncomplete: boolean) {
super(scope, id);
super(scope, id, {
region: props.region,
});

this.vpcId = props.vpcId;
this.vpcArn = Arn.format({
Expand Down
48 changes: 48 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/integ.vpc-lookup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as cdk from '@aws-cdk/core';
import { IntegTest } from '@aws-cdk/integ-tests';
import * as ec2 from '../lib';

const appWithVpc = new cdk.App();
const stack = new cdk.Stack(appWithVpc, 'StackWithVpc', {
env: {
region: 'eu-west-1',
account: '123456',
},
});

const testVpc = new ec2.Vpc(stack, 'MyVpc', {
vpcName: 'my-vpc-name',
});

const appUnderTest = new cdk.App();
const stackLookup = new cdk.Stack(appUnderTest, 'StackUnderTest', {
env: {
region: 'us-east-2',
account: '123456',
},
});

const vpcFromVpcAttributes = ec2.Vpc.fromVpcAttributes(stackLookup, 'VpcFromVpcAttributes', {
region: 'eu-west-1',
availabilityZones: ['eu-west-1a'],
vpcId: testVpc.vpcId,
});

const vpcFromLookup = ec2.Vpc.fromLookup(stack, 'VpcFromLookup', {
region: 'eu-west-1',
vpcName: 'my-vpc-name',
});

new cdk.CfnOutput(stackLookup, 'OutputFromVpcAttributes', {
value: `Region fromVpcAttributes: ${vpcFromVpcAttributes.env.region}`,
});

new cdk.CfnOutput(stackLookup, 'OutputFromLookup', {
value: `Region fromLookup: ${vpcFromLookup.env.region}`,
});

new IntegTest(appUnderTest, 'ArchiveTest', {
testCases: [stackLookup],
});
appWithVpc.synth();
appUnderTest.synth();
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "21.0.0",
"files": {
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
"source": {
"path": "ArchiveTestDefaultTestDeployAssert3405726A.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
}
},
"dockerImages": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"Parameters": {
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
}
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5"
],
{
"Ref": "BootstrapVersion"
}
]
}
]
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "21.0.0",
"files": {
"cfac1423efb7c99cadc3f40367c753fe5b1527d9a6950c096ba326fabac4c89f": {
"source": {
"path": "StackUnderTest.template.json",
"packaging": "file"
},
"destinations": {
"123456-us-east-2": {
"bucketName": "cdk-hnb659fds-assets-123456-us-east-2",
"objectKey": "cfac1423efb7c99cadc3f40367c753fe5b1527d9a6950c096ba326fabac4c89f.json",
"region": "us-east-2",
"assumeRoleArn": "arn:${AWS::Partition}:iam::123456:role/cdk-hnb659fds-file-publishing-role-123456-us-east-2"
}
}
}
},
"dockerImages": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"Outputs": {
"OutputFromVpcAttributes": {
"Value": "Region fromVpcAttributes: eu-west-1"
},
"OutputFromLookup": {
"Value": "Region fromLookup: eu-west-1"
}
},
"Parameters": {
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
}
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5"
],
{
"Ref": "BootstrapVersion"
}
]
}
]
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "21.0.0",
"files": {
"628a32283150d3af24efb3a11967996306884a3835ac731ab35822da2ce7e9ff": {
"source": {
"path": "StackWithVpc.template.json",
"packaging": "file"
},
"destinations": {
"123456-eu-west-1": {
"bucketName": "cdk-hnb659fds-assets-123456-eu-west-1",
"objectKey": "628a32283150d3af24efb3a11967996306884a3835ac731ab35822da2ce7e9ff.json",
"region": "eu-west-1",
"assumeRoleArn": "arn:${AWS::Partition}:iam::123456:role/cdk-hnb659fds-file-publishing-role-123456-eu-west-1"
}
}
}
},
"dockerImages": {}
}
Loading

0 comments on commit aedc888

Please sign in to comment.