-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
vpc-endpoint.ts
490 lines (421 loc) · 15.2 KB
/
vpc-endpoint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import * as iam from '@aws-cdk/aws-iam';
import { Aws, Construct, IResource, Lazy, Resource } from '@aws-cdk/core';
import { Connections, IConnectable } from './connections';
import { CfnVPCEndpoint } from './ec2.generated';
import { Peer } from './peer';
import { Port } from './port';
import { ISecurityGroup, SecurityGroup } from './security-group';
import { allRouteTableIds } from './util';
import { IVpc, SubnetSelection, SubnetType } from './vpc';
/**
* A VPC endpoint.
*/
export interface IVpcEndpoint extends IResource {
/**
* The VPC endpoint identifier.
* @attribute
*/
readonly vpcEndpointId: string;
}
export abstract class VpcEndpoint extends Resource implements IVpcEndpoint {
public abstract readonly vpcEndpointId: string;
protected policyDocument?: iam.PolicyDocument;
/**
* Adds a statement to the policy document of the VPC endpoint. The statement
* must have a Principal.
*
* Not all interface VPC endpoints support policy. For more information
* see https://docs.aws.amazon.com/vpc/latest/userguide/vpce-interface.html
*
* @param statement the IAM statement to add
*/
public addToPolicy(statement: iam.PolicyStatement) {
if (!statement.hasPrincipal) {
throw new Error('Statement must have a `Principal`.');
}
if (!this.policyDocument) {
this.policyDocument = new iam.PolicyDocument();
}
this.policyDocument.addStatements(statement);
}
}
/**
* A gateway VPC endpoint.
*/
export interface IGatewayVpcEndpoint extends IVpcEndpoint {
}
/**
* The type of VPC endpoint.
*/
export enum VpcEndpointType {
/**
* Interface
*
* An interface endpoint is an elastic network interface with a private IP
* address that serves as an entry point for traffic destined to a supported
* service.
*/
INTERFACE = 'Interface',
/**
* Gateway
*
* A gateway endpoint is a gateway that is a target for a specified route in
* your route table, used for traffic destined to a supported AWS service.
*/
GATEWAY = 'Gateway'
}
/**
* A service for a gateway VPC endpoint.
*/
export interface IGatewayVpcEndpointService {
/**
* The name of the service.
*/
readonly name: string;
}
/**
* An AWS service for a gateway VPC endpoint.
*/
export class GatewayVpcEndpointAwsService implements IGatewayVpcEndpointService {
public static readonly DYNAMODB = new GatewayVpcEndpointAwsService('dynamodb');
public static readonly S3 = new GatewayVpcEndpointAwsService('s3');
/**
* The name of the service.
*/
public readonly name: string;
constructor(name: string, prefix?: string) {
this.name = `${prefix || 'com.amazonaws'}.${Aws.REGION}.${name}`;
}
}
/**
* Options to add a gateway endpoint to a VPC.
*/
export interface GatewayVpcEndpointOptions {
/**
* The service to use for this gateway VPC endpoint.
*/
readonly service: IGatewayVpcEndpointService;
/**
* Where to add endpoint routing.
*
* @default private subnets
*/
readonly subnets?: SubnetSelection[]
}
/**
* Construction properties for a GatewayVpcEndpoint.
*/
export interface GatewayVpcEndpointProps extends GatewayVpcEndpointOptions {
/**
* The VPC network in which the gateway endpoint will be used.
*/
readonly vpc: IVpc
}
/**
* A gateway VPC endpoint.
* @resource AWS::EC2::VPCEndpoint
*/
export class GatewayVpcEndpoint extends VpcEndpoint implements IGatewayVpcEndpoint {
public static fromGatewayVpcEndpointId(scope: Construct, id: string, gatewayVpcEndpointId: string): IGatewayVpcEndpoint {
class Import extends VpcEndpoint {
public vpcEndpointId = gatewayVpcEndpointId;
}
return new Import(scope, id);
}
/**
* The gateway VPC endpoint identifier.
*/
public readonly vpcEndpointId: string;
/**
* The date and time the gateway VPC endpoint was created.
* @attribute
*/
public readonly vpcEndpointCreationTimestamp: string;
/**
* @attribute
*/
public readonly vpcEndpointNetworkInterfaceIds: string[];
/**
* @attribute
*/
public readonly vpcEndpointDnsEntries: string[];
constructor(scope: Construct, id: string, props: GatewayVpcEndpointProps) {
super(scope, id);
const subnets = props.subnets || [{ subnetType: SubnetType.PRIVATE }];
const routeTableIds = allRouteTableIds(...subnets.map(s => props.vpc.selectSubnets(s)));
if (routeTableIds.length === 0) {
throw new Error(`Can't add a gateway endpoint to VPC; route table IDs are not available`);
}
const endpoint = new CfnVPCEndpoint(this, 'Resource', {
policyDocument: Lazy.anyValue({ produce: () => this.policyDocument }),
routeTableIds,
serviceName: props.service.name,
vpcEndpointType: VpcEndpointType.GATEWAY,
vpcId: props.vpc.vpcId
});
this.vpcEndpointId = endpoint.ref;
this.vpcEndpointCreationTimestamp = endpoint.attrCreationTimestamp;
this.vpcEndpointDnsEntries = endpoint.attrDnsEntries;
this.vpcEndpointNetworkInterfaceIds = endpoint.attrNetworkInterfaceIds;
}
}
/**
* A service for an interface VPC endpoint.
*/
export interface IInterfaceVpcEndpointService {
/**
* The name of the service.
*/
readonly name: string;
/**
* The port of the service.
*/
readonly port: number;
/**
* Whether Private DNS is supported by default.
*/
readonly privateDnsDefault?: boolean;
}
/**
* A custom-hosted service for an interface VPC endpoint.
*/
export class InterfaceVpcEndpointService implements IInterfaceVpcEndpointService {
/**
* The name of the service.
*/
public readonly name: string;
/**
* The port of the service.
*/
public readonly port: number;
/**
* Whether Private DNS is supported by default.
*/
public readonly privateDnsDefault?: boolean = false;
constructor(name: string, port?: number) {
this.name = name;
this.port = port || 443;
}
}
/**
* An AWS service for an interface VPC endpoint.
*/
export class InterfaceVpcEndpointAwsService implements IInterfaceVpcEndpointService {
public static readonly SAGEMAKER_NOTEBOOK = new InterfaceVpcEndpointAwsService('notebook', 'aws.sagemaker');
public static readonly CLOUDFORMATION = new InterfaceVpcEndpointAwsService('cloudformation');
public static readonly CLOUDTRAIL = new InterfaceVpcEndpointAwsService('cloudtrail');
public static readonly CODEBUILD = new InterfaceVpcEndpointAwsService('codebuild');
public static readonly CODEBUILD_FIPS = new InterfaceVpcEndpointAwsService('codebuild-fips');
public static readonly CODECOMMIT = new InterfaceVpcEndpointAwsService('codecommit');
public static readonly CODECOMMIT_FIPS = new InterfaceVpcEndpointAwsService('codecommit-fips');
public static readonly CODEPIPELINE = new InterfaceVpcEndpointAwsService('codepipeline');
public static readonly CONFIG = new InterfaceVpcEndpointAwsService('config');
public static readonly EC2 = new InterfaceVpcEndpointAwsService('ec2');
public static readonly EC2_MESSAGES = new InterfaceVpcEndpointAwsService('ec2messages');
public static readonly ECR = new InterfaceVpcEndpointAwsService('ecr.api');
public static readonly ECR_DOCKER = new InterfaceVpcEndpointAwsService('ecr.dkr');
public static readonly ECS = new InterfaceVpcEndpointAwsService('ecs');
public static readonly ECS_AGENT = new InterfaceVpcEndpointAwsService('ecs-agent');
public static readonly ECS_TELEMETRY = new InterfaceVpcEndpointAwsService('ecs-telemetry');
public static readonly ELASTIC_INFERENCE_RUNTIME = new InterfaceVpcEndpointAwsService('elastic-inference.runtime');
public static readonly ELASTIC_LOAD_BALANCING = new InterfaceVpcEndpointAwsService('elasticloadbalancing');
public static readonly CLOUDWATCH_EVENTS = new InterfaceVpcEndpointAwsService('events');
public static readonly APIGATEWAY = new InterfaceVpcEndpointAwsService('execute-api');
public static readonly CODECOMMIT_GIT = new InterfaceVpcEndpointAwsService('git-codecommit');
public static readonly CODECOMMIT_GIT_FIPS = new InterfaceVpcEndpointAwsService('git-codecommit-fips');
public static readonly KINESIS_STREAMS = new InterfaceVpcEndpointAwsService('kinesis-streams');
public static readonly KMS = new InterfaceVpcEndpointAwsService('kms');
public static readonly CLOUDWATCH_LOGS = new InterfaceVpcEndpointAwsService('logs');
public static readonly CLOUDWATCH = new InterfaceVpcEndpointAwsService('monitoring');
public static readonly SAGEMAKER_API = new InterfaceVpcEndpointAwsService('sagemaker.api');
public static readonly SAGEMAKER_RUNTIME = new InterfaceVpcEndpointAwsService('sagemaker.runtime');
public static readonly SAGEMAKER_RUNTIME_FIPS = new InterfaceVpcEndpointAwsService('sagemaker.runtime-fips');
public static readonly SECRETS_MANAGER = new InterfaceVpcEndpointAwsService('secretsmanager');
public static readonly SERVICE_CATALOG = new InterfaceVpcEndpointAwsService('servicecatalog');
public static readonly SNS = new InterfaceVpcEndpointAwsService('sns');
public static readonly SQS = new InterfaceVpcEndpointAwsService('sqs');
public static readonly SSM = new InterfaceVpcEndpointAwsService('ssm');
public static readonly SSM_MESSAGES = new InterfaceVpcEndpointAwsService('ssmmessages');
public static readonly STS = new InterfaceVpcEndpointAwsService('sts');
public static readonly TRANSFER = new InterfaceVpcEndpointAwsService('transfer.server');
public static readonly STORAGE_GATEWAY = new InterfaceVpcEndpointAwsService('storagegateway');
public static readonly REKOGNITION = new InterfaceVpcEndpointAwsService('rekognition');
public static readonly REKOGNITION_FIPS = new InterfaceVpcEndpointAwsService('rekognition-fips');
/**
* The name of the service.
*/
public readonly name: string;
/**
* The port of the service.
*/
public readonly port: number;
/**
* Whether Private DNS is supported by default.
*/
public readonly privateDnsDefault?: boolean = true;
constructor(name: string, prefix?: string, port?: number) {
this.name = `${prefix || 'com.amazonaws'}.${Aws.REGION}.${name}`;
this.port = port || 443;
}
}
/**
* Options to add an interface endpoint to a VPC.
*/
export interface InterfaceVpcEndpointOptions {
/**
* The service to use for this interface VPC endpoint.
*/
readonly service: IInterfaceVpcEndpointService;
/**
* Whether to associate a private hosted zone with the specified VPC. This
* allows you to make requests to the service using its default DNS hostname.
*
* @default set by the instance of IInterfaceVpcEndpointService, or true if
* not defined by the instance of IInterfaceVpcEndpointService
*/
readonly privateDnsEnabled?: boolean;
/**
* The subnets in which to create an endpoint network interface. At most one
* per availability zone.
*
* @default - private subnets
*/
readonly subnets?: SubnetSelection;
/**
* The security groups to associate with this interface VPC endpoint.
*
* @default - a new security group is created
*/
readonly securityGroups?: ISecurityGroup[];
/**
* Whether to automatically allow VPC traffic to the endpoint
*
* If enabled, all traffic to the endpoint from within the VPC will be
* automatically allowed. This is done based on the VPC's CIDR range.
*
* @default true
*/
readonly open?: boolean;
}
/**
* Construction properties for an InterfaceVpcEndpoint.
*/
export interface InterfaceVpcEndpointProps extends InterfaceVpcEndpointOptions {
/**
* The VPC network in which the interface endpoint will be used.
*/
readonly vpc: IVpc
}
/**
* An interface VPC endpoint.
*/
export interface IInterfaceVpcEndpoint extends IVpcEndpoint, IConnectable {
}
/**
* A interface VPC endpoint.
* @resource AWS::EC2::VPCEndpoint
*/
export class InterfaceVpcEndpoint extends VpcEndpoint implements IInterfaceVpcEndpoint {
/**
* Imports an existing interface VPC endpoint.
*/
public static fromInterfaceVpcEndpointAttributes(scope: Construct, id: string, attrs: InterfaceVpcEndpointAttributes): IInterfaceVpcEndpoint {
if (!attrs.securityGroups && !attrs.securityGroupId) {
throw new Error('Either `securityGroups` or `securityGroupId` must be specified.');
}
const securityGroups = attrs.securityGroupId
? [SecurityGroup.fromSecurityGroupId(scope, 'SecurityGroup', attrs.securityGroupId)]
: attrs.securityGroups;
class Import extends Resource implements IInterfaceVpcEndpoint {
public readonly vpcEndpointId = attrs.vpcEndpointId;
public readonly connections = new Connections({
defaultPort: Port.tcp(attrs.port),
securityGroups,
});
}
return new Import(scope, id);
}
/**
* The interface VPC endpoint identifier.
*/
public readonly vpcEndpointId: string;
/**
* The date and time the interface VPC endpoint was created.
* @attribute
*/
public readonly vpcEndpointCreationTimestamp: string;
/**
* The DNS entries for the interface VPC endpoint.
* @attribute
*/
public readonly vpcEndpointDnsEntries: string[];
/**
* One or more network interfaces for the interface VPC endpoint.
* @attribute
*/
public readonly vpcEndpointNetworkInterfaceIds: string[];
/**
* The identifier of the first security group associated with this interface
* VPC endpoint.
*
* @deprecated use the `connections` object
*/
public readonly securityGroupId: string;
/**
* Access to network connections.
*/
public readonly connections: Connections;
constructor(scope: Construct, id: string, props: InterfaceVpcEndpointProps) {
super(scope, id);
const securityGroups = props.securityGroups || [new SecurityGroup(this, 'SecurityGroup', {
vpc: props.vpc
})];
this.securityGroupId = securityGroups[0].securityGroupId;
this.connections = new Connections({
defaultPort: Port.tcp(props.service.port),
securityGroups
});
if (props.open !== false) {
this.connections.allowDefaultPortFrom(Peer.ipv4(props.vpc.vpcCidrBlock));
}
const subnets = props.vpc.selectSubnets({ ...props.subnets, onePerAz: true });
const subnetIds = subnets.subnetIds;
const endpoint = new CfnVPCEndpoint(this, 'Resource', {
privateDnsEnabled: props.privateDnsEnabled ?? props.service.privateDnsDefault ?? true,
policyDocument: Lazy.anyValue({ produce: () => this.policyDocument }),
securityGroupIds: securityGroups.map(s => s.securityGroupId),
serviceName: props.service.name,
vpcEndpointType: VpcEndpointType.INTERFACE,
subnetIds,
vpcId: props.vpc.vpcId
});
this.vpcEndpointId = endpoint.ref;
this.vpcEndpointCreationTimestamp = endpoint.attrCreationTimestamp;
this.vpcEndpointDnsEntries = endpoint.attrDnsEntries;
this.vpcEndpointNetworkInterfaceIds = endpoint.attrNetworkInterfaceIds;
}
}
/**
* Construction properties for an ImportedInterfaceVpcEndpoint.
*/
export interface InterfaceVpcEndpointAttributes {
/**
* The interface VPC endpoint identifier.
*/
readonly vpcEndpointId: string;
/**
* The identifier of the security group associated with the interface VPC endpoint.
*
* @deprecated use `securityGroups` instead
*/
readonly securityGroupId?: string;
/**
* The security groups associated with the interface VPC endpoint.
*
*/
readonly securityGroups?: ISecurityGroup[];
/**
* The port of the service of the interface VPC endpoint.
*/
readonly port: number;
}