Skip to content

Commit f55cd2b

Browse files
authored
fix(ec2): interface endpoints do not work with Vpc.fromLookup() (#18554)
The validation was too eager, causing a validation error before the actual lookup happened. Add a property to `SelectedSubnets` to make it clear in consuming code that validation shouldn't happen yet. Fixes #17600. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent b49b002 commit f55cd2b

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

packages/@aws-cdk/aws-ec2/lib/vpc-endpoint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ export class InterfaceVpcEndpoint extends VpcEndpoint implements IInterfaceVpcEn
574574
const subnets = subnetSelection.subnets;
575575

576576
// Sanity check the subnet count
577-
if (subnetSelection.subnets.length == 0) {
577+
if (!subnetSelection.isPendingLookup && subnetSelection.subnets.length == 0) {
578578
throw new Error('Cannot create a VPC Endpoint with no subnets');
579579
}
580580

packages/@aws-cdk/aws-ec2/lib/vpc.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,17 @@ export interface SelectedSubnets {
351351
* Whether any of the given subnets are from the VPC's public subnets.
352352
*/
353353
readonly hasPublic: boolean;
354+
355+
/**
356+
* The subnet selection is not actually real yet
357+
*
358+
* If this value is true, don't validate anything about the subnets. The count
359+
* or identities are not known yet, and the validation will most likely fail
360+
* which will prevent a successful lookup.
361+
*
362+
* @default false
363+
*/
364+
readonly isPendingLookup?: boolean;
354365
}
355366

356367
/**
@@ -430,6 +441,7 @@ abstract class VpcBase extends Resource implements IVpc {
430441
internetConnectivityEstablished: tap(new CompositeDependable(), d => subnets.forEach(s => d.add(s.internetConnectivityEstablished))),
431442
subnets,
432443
hasPublic: subnets.some(s => pubs.has(s)),
444+
isPendingLookup: this.incompleteSubnetDefinition,
433445
};
434446
}
435447

packages/@aws-cdk/aws-ec2/test/vpc-endpoint.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AnyPrincipal, PolicyStatement } from '@aws-cdk/aws-iam';
33
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
44
import { ContextProvider, Fn, Stack } from '@aws-cdk/core';
55
// eslint-disable-next-line max-len
6-
import { GatewayVpcEndpoint, GatewayVpcEndpointAwsService, InterfaceVpcEndpoint, InterfaceVpcEndpointAwsService, InterfaceVpcEndpointService, SecurityGroup, SubnetType, Vpc } from '../lib';
6+
import { GatewayVpcEndpoint, GatewayVpcEndpointAwsService, InterfaceVpcEndpoint, InterfaceVpcEndpointAwsService, InterfaceVpcEndpointService, SecurityGroup, SubnetFilter, SubnetType, Vpc } from '../lib';
77

88
describe('vpc endpoint', () => {
99
describe('gateway endpoint', () => {
@@ -268,6 +268,25 @@ describe('vpc endpoint', () => {
268268

269269
});
270270

271+
describe('add interface endpoint to looked-up VPC', () => {
272+
test('initial run', () => {
273+
// GIVEN
274+
const stack = new Stack(undefined, undefined, { env: { account: '1234', region: 'us-east-1' } });
275+
const vpc = Vpc.fromLookup(stack, 'Vpc', {
276+
vpcId: 'doesnt-matter',
277+
});
278+
279+
// THEN: doesn't throw
280+
vpc.addInterfaceEndpoint('SecretsManagerEndpoint', {
281+
service: InterfaceVpcEndpointAwsService.SECRETS_MANAGER,
282+
subnets: {
283+
subnetFilters: [SubnetFilter.byIds(['1234'])],
284+
},
285+
});
286+
});
287+
});
288+
289+
271290
test('import/export', () => {
272291
// GIVEN
273292
const stack2 = new Stack();

0 commit comments

Comments
 (0)