Skip to content

Commit

Permalink
feat(ec2): look up VPC from different regions (#16728)
Browse files Browse the repository at this point in the history
- Currently Vpc.fromLookup will default to the Stack's region, when needing to lookup Stack's from other regions (e.g. for VPC peering) this doesn't work and the only other work around is a custom resource
- The lookup provider already supports passing a region and works fine if you pass one that's not the Stack's inferred region, so just propagate that option to the top level
- Fixes #10208


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
mina-asham authored Oct 25, 2021
1 parent e063fbd commit f1e244b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-ec2/lib/vpc-lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ export interface VpcLookupOptions {
* @default aws-cdk:subnet-name
*/
readonly subnetGroupNameTag?: string;

/**
* Optional to override inferred region
*
* @default Current stack's environment region
*/
readonly region?: string;
}
6 changes: 6 additions & 0 deletions packages/@aws-cdk/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,9 +1118,15 @@ export class Vpc extends VpcBase {
filter.isDefault = options.isDefault ? 'true' : 'false';
}

const overrides: {[key: string]: string} = {};
if (options.region) {
overrides.region = options.region;
}

const attributes: cxapi.VpcContextResponse = ContextProvider.getValue(scope, {
provider: cxschema.ContextProvider.VPC_PROVIDER,
props: {
...overrides,
filter,
returnAsymmetricSubnets: true,
subnetGroupNameTag: options.subnetGroupNameTag,
Expand Down
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/vpc.from-lookup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,24 @@ describe('vpc from lookup', () => {
restoreContextProvider(previous);

});
test('passes account and region', () => {
const previous = mockVpcContextProviderWith({
vpcId: 'vpc-1234',
subnetGroups: [],
}, options => {
expect(options.region).toEqual('region-1234');
});

const stack = new Stack();
const vpc = Vpc.fromLookup(stack, 'Vpc', {
vpcId: 'vpc-1234',
region: 'region-1234',
});

expect(vpc.vpcId).toEqual('vpc-1234');

restoreContextProvider(previous);
});
});
});

Expand Down

0 comments on commit f1e244b

Please sign in to comment.