This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
/
elasticsearch.ts
355 lines (333 loc) · 13.6 KB
/
elasticsearch.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
import { CfnMapping } from 'aws-cdk-lib';
import { Domain, EngineVersion } from 'aws-cdk-lib/aws-opensearchservice';
import {
CfnUserPool,
CfnUserPoolDomain,
CfnUserPoolClient,
CfnIdentityPool,
CfnIdentityPoolRoleAttachment,
} from 'aws-cdk-lib/aws-cognito';
import { EbsDeviceVolumeType } from 'aws-cdk-lib/aws-ec2';
import {
Role,
ManagedPolicy,
ServicePrincipal,
PolicyStatement,
Effect,
FederatedPrincipal,
ArnPrincipal,
} from 'aws-cdk-lib/aws-iam';
import { LogGroup, ResourcePolicy } from 'aws-cdk-lib/aws-logs';
import { Construct } from 'constructs';
import { Key } from 'aws-cdk-lib/aws-kms';
import { NagSuppressions } from 'cdk-nag';
export default class ElasticSearchResources {
kibanaUserPool: CfnUserPool | undefined = undefined;
kibanaUserPoolDomain: CfnUserPoolDomain | undefined = undefined;
kibanaUserPoolClient: CfnUserPoolClient | undefined = undefined;
kibanaIdentityPool: CfnIdentityPool | undefined = undefined;
kibanaCognitoRole: Role | undefined = undefined;
adminKibanaAccessRole: Role | undefined = undefined;
identityPoolRoleAttachment: CfnIdentityPoolRoleAttachment | undefined = undefined;
searchLogs: LogGroup;
searchLogsResourcePolicy: ResourcePolicy;
elasticSearchDomain: Domain;
constructor(
scope: Construct,
stackName: string,
stage: string,
account: string,
partition: string,
region: string,
isDev: boolean,
elasticSearchKMSKey: Key,
) {
const regionMappings: CfnMapping = new CfnMapping(scope, 'regionMap', {
mapping: {
'us-east-2': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'us-east-1': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'us-west-1': {
smallEc2: 'c6g.large.search',
largeEc2: 'r6g.large.search',
},
'us-west-2': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'af-south-1': {
smallEc2: 'c5.large.search',
largeEc2: 'm5.large.search',
},
'ap-east-1': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'ap-south-1': {
smallEc2: 'c6g.large.search',
largeEc2: 'r6g.large.search',
},
'ap-southeast-2': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'ap-southeast-1': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'ap-northeast-3': {
smallEc2: 'c5.large.search',
largeEc2: 'm5.large.search',
},
'ap-northeast-2': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'ap-northeast-1': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'ca-central-1': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'eu-central-1': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'eu-west-1': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'eu-west-2': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'eu-west-3': {
smallEc2: 'c5.large.search',
largeEc2: 'm5.large.search',
},
'eu-south-1': {
smallEc2: 'c5.large.search',
largeEc2: 'm5.large.search',
},
'eu-north-1': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'me-south-1': {
smallEc2: 'c5.large.search',
largeEc2: 'm5.large.search',
},
'sa-east-1': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'us-gov-east-1': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
'us-gov-west-1': {
smallEc2: 'c6g.large.search',
largeEc2: 'm6g.large.search',
},
},
});
if (isDev) {
this.kibanaUserPool = new CfnUserPool(scope, 'kibanaUserPool', {
userPoolName: `${stackName}-Kibana`,
adminCreateUserConfig: {
allowAdminCreateUserOnly: true,
},
autoVerifiedAttributes: ['email'],
schema: [
{
attributeDataType: 'String',
name: 'email',
required: true,
},
{
attributeDataType: 'String',
name: 'cc_confirmed',
},
],
policies: {
passwordPolicy: {
minimumLength: 8,
requireLowercase: true,
requireNumbers: true,
requireSymbols: true,
requireUppercase: true,
},
},
userPoolAddOns: {
advancedSecurityMode: 'ENFORCED',
},
});
NagSuppressions.addResourceSuppressions(this.kibanaUserPool, [
{
id: 'AwsSolutions-COG2',
reason: 'Only admins can create users in this user pool',
},
]);
this.kibanaUserPoolDomain = new CfnUserPoolDomain(scope, 'kibanaUserPoolDomain', {
userPoolId: this.kibanaUserPool.ref,
domain: `kibana-${stage}-${account}`,
});
this.kibanaUserPoolClient = new CfnUserPoolClient(scope, 'kibanaUserPoolClient', {
clientName: `${stackName}-KibanaClient`,
generateSecret: false,
userPoolId: this.kibanaUserPool.ref,
explicitAuthFlows: ['ADMIN_NO_SRP_AUTH', 'USER_PASSWORD_AUTH'],
preventUserExistenceErrors: 'ENABLED',
});
this.kibanaIdentityPool = new CfnIdentityPool(scope, 'kibanaIdentityPool', {
identityPoolName: `${stackName}-KibanaIDPool`,
allowUnauthenticatedIdentities: false,
cognitoIdentityProviders: [
{
clientId: this.kibanaUserPoolClient.ref,
providerName: this.kibanaUserPool.attrProviderName,
},
],
});
this.kibanaCognitoRole = new Role(scope, 'kibanaCognitoRole', {
managedPolicies: [ManagedPolicy.fromAwsManagedPolicyName('AmazonESCognitoAccess')],
assumedBy: new ServicePrincipal('es.amazonaws.com'),
});
NagSuppressions.addResourceSuppressions(this.kibanaCognitoRole, [
{
id: 'AwsSolutions-IAM4',
reason: 'This role uses AWS Managed policies for only dev deployments, not in production',
},
]);
this.adminKibanaAccessRole = new Role(scope, 'adminKibanaAccessRole', {
assumedBy: new FederatedPrincipal(
'cognito-identity.amazonaws.com',
{
StringEquals: {
'cognito-identity.amazonaws.com:aud': this.kibanaIdentityPool.ref,
},
'ForAnyValue:StringLike': {
'cognito-identity.amazonaws.com:amr': 'authenticated',
},
},
'sts:AssumeRoleWithWebIdentity',
),
});
this.identityPoolRoleAttachment = new CfnIdentityPoolRoleAttachment(scope, 'identityPoolRoleAttachment', {
identityPoolId: this.kibanaIdentityPool.ref,
roles: {
authenticated: this.adminKibanaAccessRole.roleArn,
},
});
}
this.searchLogs = new LogGroup(scope, 'searchLogs', {
logGroupName: `${stackName}-search-logs`,
});
this.searchLogsResourcePolicy = new ResourcePolicy(scope, 'searchLogsResourcePolicy', {
policyStatements: [
new PolicyStatement({
effect: Effect.ALLOW,
principals: [new ServicePrincipal('es.amazonaws.com')],
actions: ['logs:PutLogEvents', 'logs:CreateLogStream'],
resources: [`arn:${partition}:logs:${region}:${account}:log-group:${stackName}-search-logs:*`],
}),
],
resourcePolicyName: `${stackName}-search-logs-resource-policy`,
});
this.searchLogsResourcePolicy.node.addDependency(this.searchLogs);
let elasticSearchDomainAccessPolicy: PolicyStatement[] | undefined;
if (isDev) {
elasticSearchDomainAccessPolicy = [
new PolicyStatement({
effect: Effect.ALLOW,
principals: [
new ArnPrincipal(this.adminKibanaAccessRole!.roleArn),
new ArnPrincipal(
`arn:${partition}:sts::${account}:assumed-role/${
this.kibanaCognitoRole!.roleName
}/CognitoIdentityCredentials`,
),
],
actions: ['es:*'],
resources: [`arn:${partition}:es:${region}:${account}:domain/*`],
}),
];
}
this.elasticSearchDomain = new Domain(scope, 'elasticSearchDomain', {
// Assuming ~100GB storage requirement for PROD; min storage requirement is ~290GB https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/sizing-domains.html
// If you change the size of the Elasticsearch Domain, consider also updating the NUMBER_OF_SHARDS on the updateSearchMappings resource
ebs: {
enabled: true,
volumeType: EbsDeviceVolumeType.GP2,
volumeSize: isDev ? 10 : 73,
},
version: EngineVersion.ELASTICSEARCH_7_10,
zoneAwareness: {
enabled: !isDev,
},
enableVersionUpgrade: true,
capacity: {
masterNodes: isDev ? undefined : 3,
masterNodeInstanceType: isDev ? undefined : regionMappings.findInMap(region, 'smallEc2'),
dataNodes: isDev ? 1 : 4,
dataNodeInstanceType: regionMappings.findInMap(region, isDev ? 'smallEc2' : 'largeEc2'),
},
encryptionAtRest: {
enabled: true,
kmsKey: elasticSearchKMSKey,
},
nodeToNodeEncryption: true,
automatedSnapshotStartHour: isDev ? undefined : 0,
cognitoDashboardsAuth: isDev
? {
identityPoolId: this.kibanaIdentityPool!.ref,
userPoolId: this.kibanaUserPool!.ref,
role: this.kibanaCognitoRole!,
}
: undefined,
accessPolicies: elasticSearchDomainAccessPolicy,
logging: {
appLogEnabled: true,
appLogGroup: this.searchLogs,
slowSearchLogEnabled: true,
slowSearchLogGroup: this.searchLogs,
slowIndexLogEnabled: true,
slowIndexLogGroup: this.searchLogs,
},
});
this.elasticSearchDomain.node.addDependency(this.searchLogsResourcePolicy);
NagSuppressions.addResourceSuppressions(this.elasticSearchDomain, [
{
id: 'AwsSolutions-OS1',
reason: 'We do not want a VPC for OpenSearch. We are controlling access to OS using IAM roles',
},
{
id: 'AwsSolutions-OS3',
reason: 'We only access the OpenSearch domain via Lambda functions',
},
{
id: 'AwsSolutions-OS4',
reason: 'We dont use dedicated master nodes in the dev stage only.',
},
{
id: 'AwsSolutions-OS5',
reason: 'All requests are processed through APIGW/Lambda before reaching the OS Domain',
},
{
id: 'AwsSolutions-OS7',
reason: 'We dont enable Zone Awareness in the dev stage only',
},
{
id: 'AwsSolutions-IAM5',
reason: 'This wildcard policy only applies in the dev stage',
},
]);
}
}