-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
vpc-endpoint-service.ts
186 lines (159 loc) · 5.59 KB
/
vpc-endpoint-service.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
import { Construct } from 'constructs';
import { CfnVPCEndpointService, CfnVPCEndpointServicePermissions } from './ec2.generated';
import { ArnPrincipal } from '../../aws-iam';
import { Aws, Fn, IResource, Resource, Stack, Token } from '../../core';
import { Default, RegionInfo } from '../../region-info';
/**
* A load balancer that can host a VPC Endpoint Service
*
*/
export interface IVpcEndpointServiceLoadBalancer {
/**
* The ARN of the load balancer that hosts the VPC Endpoint Service
*
* @attribute
*/
readonly loadBalancerArn: string;
}
/**
* A VPC endpoint service.
*
*/
export interface IVpcEndpointService extends IResource {
/**
* The service name of the VPC Endpoint Service that clients use to connect to,
* like com.amazonaws.vpce.<region>.vpce-svc-xxxxxxxxxxxxxxxx
*
* @attribute
*/
readonly vpcEndpointServiceName: string;
/**
* The id of the VPC Endpoint Service that clients use to connect to,
* like vpce-svc-xxxxxxxxxxxxxxxx
*
* @attribute
*/
readonly vpcEndpointServiceId: string;
}
/**
* A VPC endpoint service
* @resource AWS::EC2::VPCEndpointService
*
*/
export class VpcEndpointService extends Resource implements IVpcEndpointService {
/**
* One or more network load balancers to host the service.
* @attribute
*/
public readonly vpcEndpointServiceLoadBalancers: IVpcEndpointServiceLoadBalancer[];
/**
* Whether to require manual acceptance of new connections to the service.
*
*/
public readonly acceptanceRequired: boolean;
/**
* Whether to enable the built-in Contributor Insights rules provided by AWS PrivateLink.
*
*/
public readonly contributorInsightsEnabled?: boolean;
/**
* One or more Principal ARNs to allow inbound connections to.
* @deprecated use `allowedPrincipals`
*/
public readonly whitelistedPrincipals: ArnPrincipal[];
/**
* One or more Principal ARNs to allow inbound connections to.
*
*/
public readonly allowedPrincipals: ArnPrincipal[];
/**
* The id of the VPC Endpoint Service, like vpce-svc-xxxxxxxxxxxxxxxx.
* @attribute
*/
public readonly vpcEndpointServiceId: string;
/**
* The service name of the VPC Endpoint Service that clients use to connect to,
* like com.amazonaws.vpce.<region>.vpce-svc-xxxxxxxxxxxxxxxx
*
* @attribute
*/
public readonly vpcEndpointServiceName: string;
private readonly endpointService: CfnVPCEndpointService;
constructor(scope: Construct, id: string, props: VpcEndpointServiceProps) {
super(scope, id);
if (props.vpcEndpointServiceLoadBalancers === undefined || props.vpcEndpointServiceLoadBalancers.length === 0) {
throw new Error('VPC Endpoint Service must have at least one load balancer specified.');
}
this.vpcEndpointServiceLoadBalancers = props.vpcEndpointServiceLoadBalancers;
this.acceptanceRequired = props.acceptanceRequired ?? true;
this.contributorInsightsEnabled = props.contributorInsights;
if (props.allowedPrincipals && props.whitelistedPrincipals) {
throw new Error('`whitelistedPrincipals` is deprecated; please use `allowedPrincipals` instead');
}
this.allowedPrincipals = props.allowedPrincipals ?? props.whitelistedPrincipals ?? [];
this.whitelistedPrincipals = this.allowedPrincipals;
this.endpointService = new CfnVPCEndpointService(this, id, {
networkLoadBalancerArns: this.vpcEndpointServiceLoadBalancers.map(lb => lb.loadBalancerArn),
acceptanceRequired: this.acceptanceRequired,
contributorInsightsEnabled: this.contributorInsightsEnabled,
});
this.vpcEndpointServiceId = this.endpointService.ref;
const { region } = Stack.of(this);
const serviceNamePrefix = !Token.isUnresolved(region) ?
(RegionInfo.get(region).vpcEndpointServiceNamePrefix ?? Default.VPC_ENDPOINT_SERVICE_NAME_PREFIX) :
Default.VPC_ENDPOINT_SERVICE_NAME_PREFIX;
this.vpcEndpointServiceName = Fn.join('.', [serviceNamePrefix, Aws.REGION, this.vpcEndpointServiceId]);
if (this.allowedPrincipals.length > 0) {
new CfnVPCEndpointServicePermissions(this, 'Permissions', {
serviceId: this.endpointService.ref,
allowedPrincipals: this.allowedPrincipals.map(x => x.arn),
});
}
}
}
/**
* Construction properties for a VpcEndpointService.
*
*/
export interface VpcEndpointServiceProps {
/**
* Name of the Vpc Endpoint Service
* @deprecated This property is not used
* @default - CDK generated name
*/
readonly vpcEndpointServiceName?: string;
/**
* One or more load balancers to host the VPC Endpoint Service.
*
*/
readonly vpcEndpointServiceLoadBalancers: IVpcEndpointServiceLoadBalancer[];
/**
* Whether requests from service consumers to connect to the service through
* an endpoint must be accepted.
* @default true
*
*/
readonly acceptanceRequired?: boolean;
/**
* Indicates whether to enable the built-in Contributor Insights rules provided by AWS PrivateLink.
* @default false
*
*/
readonly contributorInsights?: boolean;
/**
* IAM users, IAM roles, or AWS accounts to allow inbound connections from.
* These principals can connect to your service using VPC endpoints. Takes a
* list of one or more ArnPrincipal.
* @default - no principals
* @deprecated use `allowedPrincipals`
*/
readonly whitelistedPrincipals?: ArnPrincipal[];
/**
* IAM users, IAM roles, or AWS accounts to allow inbound connections from.
* These principals can connect to your service using VPC endpoints. Takes a
* list of one or more ArnPrincipal.
* @default - no principals
*
*/
readonly allowedPrincipals?: ArnPrincipal[];
}