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

feat(ec2): look up VPC from different regions #16728

Merged
merged 2 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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