Skip to content

Commit 0c77cb6

Browse files
authored
feat(ec2): support the new SupportedRegions property for AWS::EC2::VPCEndpointService (#33959)
### Issue # (if applicable) N/A ### Reason for this change Supporting the new L1 property in the L2 construct ### Description of changes Added a new L2 prop - `allowedRegions` - which is of type `string[]`. It gets passed to the L1 `SupportedRegions` property. ### Describe any new or updated permissions being added none ### Description of how you validated changes Unit tests and integ test ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 22dc717 commit 0c77cb6

File tree

11 files changed

+144
-12
lines changed

11 files changed

+144
-12
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.vpc-endpoint-service.js.snapshot/TestStackLoadBalancer.assets.json

+5-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.vpc-endpoint-service.js.snapshot/TestStackLoadBalancer.template.json

+3
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@
512512
],
513513
"SupportedIpAddressTypes": [
514514
"ipv4"
515+
],
516+
"SupportedRegions": [
517+
"us-east-2"
515518
]
516519
}
517520
}

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.vpc-endpoint-service.js.snapshot/VpcEndpointserviceDefaultTestDeployAssert1FF764BE.assets.json

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.vpc-endpoint-service.js.snapshot/cdk.out

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.vpc-endpoint-service.js.snapshot/integ.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.vpc-endpoint-service.js.snapshot/manifest.json

+8-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.vpc-endpoint-service.js.snapshot/tree.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.vpc-endpoint-service.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class TestStack extends cdk.Stack {
1717
vpcEndpointServiceLoadBalancers: [loadBalancer],
1818
acceptanceRequired: true,
1919
contributorInsights: true,
20-
supportedIpAddressTypes: [ec2.IpAddressType.IPV4], // change to ipv4 and re-run
20+
supportedIpAddressTypes: [ec2.IpAddressType.IPV4],
21+
allowedRegions: ['us-east-2'],
2122
});
2223
}
2324
}

packages/aws-cdk-lib/aws-ec2/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,18 @@ new ec2.VpcEndpointService(this, 'EndpointService', {
11341134
});
11351135
```
11361136

1137+
You can restrict access to your endpoint service to specific AWS regions:
1138+
1139+
```ts
1140+
declare const networkLoadBalancer: elbv2.NetworkLoadBalancer;
1141+
1142+
new ec2.VpcEndpointService(this, 'EndpointService', {
1143+
vpcEndpointServiceLoadBalancers: [networkLoadBalancer],
1144+
// Allow service consumers from these regions only
1145+
allowedRegions: ['us-east-1', 'eu-west-1'],
1146+
});
1147+
```
1148+
11371149
Endpoint services support private DNS, which makes it easier for clients to connect to your service by automatically setting up DNS in their VPC.
11381150
You can enable private DNS on an endpoint service like so:
11391151

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

+13
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ export class VpcEndpointService extends Resource implements IVpcEndpointService
103103
*/
104104
private readonly supportedIpAddressTypes?: IpAddressType[];
105105

106+
/**
107+
* The Regions from which service consumers can access the service.
108+
*/
109+
private readonly allowedRegions?: string[];
110+
106111
/**
107112
* The id of the VPC Endpoint Service, like vpce-svc-xxxxxxxxxxxxxxxx.
108113
* @attribute
@@ -132,6 +137,7 @@ export class VpcEndpointService extends Resource implements IVpcEndpointService
132137
this.acceptanceRequired = props.acceptanceRequired ?? true;
133138
this.contributorInsightsEnabled = props.contributorInsights;
134139
this.supportedIpAddressTypes = props.supportedIpAddressTypes;
140+
this.allowedRegions = props.allowedRegions;
135141

136142
if (props.allowedPrincipals && props.whitelistedPrincipals) {
137143
throw new Error('`whitelistedPrincipals` is deprecated; please use `allowedPrincipals` instead');
@@ -144,6 +150,7 @@ export class VpcEndpointService extends Resource implements IVpcEndpointService
144150
acceptanceRequired: this.acceptanceRequired,
145151
contributorInsightsEnabled: this.contributorInsightsEnabled,
146152
supportedIpAddressTypes: this.supportedIpAddressTypes?.map(type => type.toString()),
153+
supportedRegions: this.allowedRegions,
147154
});
148155

149156
this.vpcEndpointServiceId = this.endpointService.ref;
@@ -220,4 +227,10 @@ export interface VpcEndpointServiceProps {
220227
* @default - No specific IP address types configured
221228
*/
222229
readonly supportedIpAddressTypes?: IpAddressType[];
230+
231+
/**
232+
* The Regions from which service consumers can access the service.
233+
* @default - No Region restrictions
234+
*/
235+
readonly allowedRegions?: string[];
223236
}

packages/aws-cdk-lib/aws-ec2/test/vpc-endpoint-service.test.ts

+96
Original file line numberDiff line numberDiff line change
@@ -229,5 +229,101 @@ describe('vpc endpoint service', () => {
229229
SupportedIpAddressTypes: ['ipv4', 'ipv6'],
230230
});
231231
});
232+
233+
test('without specifying allowed regions', () => {
234+
// GIVEN
235+
const stack = new Stack();
236+
237+
// WHEN
238+
const lb = new DummyEndpointLoadBalacer('arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/Test/9bn6qkf4e9jrw77a');
239+
new VpcEndpointService(stack, 'EndpointService', {
240+
vpcEndpointServiceLoadBalancers: [lb],
241+
acceptanceRequired: false,
242+
});
243+
244+
// THEN
245+
Template.fromStack(stack).hasResourceProperties('AWS::EC2::VPCEndpointService', {
246+
NetworkLoadBalancerArns: ['arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/Test/9bn6qkf4e9jrw77a'],
247+
AcceptanceRequired: false,
248+
});
249+
250+
// Verify SupportedRegions is not present when not specified
251+
const template = Template.fromStack(stack);
252+
const resources = template.findResources('AWS::EC2::VPCEndpointService');
253+
const resourceKey = Object.keys(resources)[0];
254+
expect(resources[resourceKey].Properties.SupportedRegions).toBeUndefined();
255+
});
256+
257+
test('with a single allowed region', () => {
258+
// GIVEN
259+
const stack = new Stack();
260+
261+
// WHEN
262+
const lb = new DummyEndpointLoadBalacer('arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/Test/9bn6qkf4e9jrw77a');
263+
new VpcEndpointService(stack, 'EndpointService', {
264+
vpcEndpointServiceLoadBalancers: [lb],
265+
acceptanceRequired: false,
266+
allowedRegions: ['us-east-1'],
267+
});
268+
269+
// THEN
270+
Template.fromStack(stack).hasResourceProperties('AWS::EC2::VPCEndpointService', {
271+
NetworkLoadBalancerArns: ['arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/Test/9bn6qkf4e9jrw77a'],
272+
AcceptanceRequired: false,
273+
SupportedRegions: ['us-east-1'],
274+
});
275+
});
276+
277+
test('with multiple allowed regions', () => {
278+
// GIVEN
279+
const stack = new Stack();
280+
281+
// WHEN
282+
const lb = new DummyEndpointLoadBalacer('arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/Test/9bn6qkf4e9jrw77a');
283+
new VpcEndpointService(stack, 'EndpointService', {
284+
vpcEndpointServiceLoadBalancers: [lb],
285+
acceptanceRequired: false,
286+
allowedRegions: ['us-east-1', 'us-west-1', 'eu-west-1'],
287+
});
288+
289+
// THEN
290+
Template.fromStack(stack).hasResourceProperties('AWS::EC2::VPCEndpointService', {
291+
NetworkLoadBalancerArns: ['arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/Test/9bn6qkf4e9jrw77a'],
292+
AcceptanceRequired: false,
293+
SupportedRegions: ['us-east-1', 'us-west-1', 'eu-west-1'],
294+
});
295+
});
296+
297+
test('with combined options including allowed regions', () => {
298+
// GIVEN
299+
const stack = new Stack();
300+
301+
// WHEN
302+
const lb = new DummyEndpointLoadBalacer('arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/Test/9bn6qkf4e9jrw77a');
303+
new VpcEndpointService(stack, 'EndpointService', {
304+
vpcEndpointServiceLoadBalancers: [lb],
305+
acceptanceRequired: true,
306+
allowedRegions: ['us-east-1', 'us-west-2'],
307+
supportedIpAddressTypes: [IpAddressType.IPV4],
308+
contributorInsights: true,
309+
allowedPrincipals: [new ArnPrincipal('arn:aws:iam::123456789012:root')],
310+
});
311+
312+
// THEN
313+
Template.fromStack(stack).hasResourceProperties('AWS::EC2::VPCEndpointService', {
314+
NetworkLoadBalancerArns: ['arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/Test/9bn6qkf4e9jrw77a'],
315+
AcceptanceRequired: true,
316+
SupportedRegions: ['us-east-1', 'us-west-2'],
317+
SupportedIpAddressTypes: ['ipv4'],
318+
ContributorInsightsEnabled: true,
319+
});
320+
321+
Template.fromStack(stack).hasResourceProperties('AWS::EC2::VPCEndpointServicePermissions', {
322+
ServiceId: {
323+
Ref: 'EndpointServiceED36BE1F',
324+
},
325+
AllowedPrincipals: ['arn:aws:iam::123456789012:root'],
326+
});
327+
});
232328
});
233329
});

0 commit comments

Comments
 (0)