Skip to content

Commit

Permalink
feat(ec2): look up VPC from different regions (aws#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 aws#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 and TikiTDO committed Feb 21, 2022
1 parent 3f4c033 commit d5ed056
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
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
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
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 d5ed056

Please sign in to comment.