-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreview-analysis-stack.ts
More file actions
490 lines (446 loc) · 20.9 KB
/
review-analysis-stack.ts
File metadata and controls
490 lines (446 loc) · 20.9 KB
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 cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as lambdaEvent from 'aws-cdk-lib/aws-lambda-event-sources';
import * as ddb from 'aws-cdk-lib/aws-dynamodb';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as kinesis from 'aws-cdk-lib/aws-kinesis';
import * as athena from 'aws-cdk-lib/aws-athena';
import * as glue from '@aws-cdk/aws-glue-alpha';
import { NagSuppressions } from 'cdk-nag'
import { DynamoDBStreamsToLambda } from '@aws-solutions-constructs/aws-dynamodbstreams-lambda';
import { KinesisStreamsToKinesisFirehoseToS3 } from '@aws-solutions-constructs/aws-kinesisstreams-kinesisfirehose-s3';
import * as base from '../../lib/template/stack/base/base-stack';
import { AppContext } from '../../lib/template/app-context';
import { StackConfig } from '../../lib/template/app-config';
interface LambdaConfig {
LambdaFuncName: string;
LambdaFuncMemory: number;
LambdaFuncCode: string;
LambdaFuncHandler: string;
LambdaFuncBatch: number;
LambdaFuncWindow: number;
StreamBatchSize: string;
}
interface KinesisConfig {
KinesisStreamName: string;
KinesisFireHoseName: string;
KinesisBucketName: string;
}
interface GlueConfig {
GlueDatabaseName: string;
GlueTableName: string;
}
interface AthenaConfig {
AtheanGroupName: string;
AtheanBucketName: string;
AtheanQuerySentiment: string;
AtheanQueryEntities: string;
AtheanQuerySyntax: string;
}
export interface ReviewAnalysisStackConfig extends StackConfig {
LambdaConfig: LambdaConfig;
KinesisConfig: KinesisConfig;
GlueConfig: GlueConfig;
AthenaConfig: AthenaConfig;
}
export class ReviewAnalysisStack extends base.BaseStack {
constructor(appContext: AppContext, stackConfig: ReviewAnalysisStackConfig) {
super(appContext, stackConfig);
const ddbConfig = this.commonProps.appConfig.Stack.ReviewBackend.DdbConfig;
const backendTableName: string = ddbConfig.DdbTableName;
const backendTablePartitionKey: string = ddbConfig.DdbTablePartitionKey;
const backendTableSortKey: string = ddbConfig.DdbTableSortKey;
const kinesisBucket = this.createSecureS3Bucket({
bucketId: 'kinesis-bucket',
serverAccessLogsBucket: this.createSecureS3Bucket({bucketId: 'kinesis-access'})
})
const glueDatabase = new glue.Database(this, 'review-database', {
databaseName: this.withStackName(stackConfig.GlueConfig.GlueDatabaseName).toLowerCase(),
});
const glueTable = new glue.Table(this, 'comprehend-table', {
database: glueDatabase,
tableName: this.withStackName(stackConfig.GlueConfig.GlueTableName).toLowerCase(),
dataFormat: glue.DataFormat.PARQUET,
bucket: kinesisBucket,
columns: this.loadTableSchemaColumns(backendTablePartitionKey, backendTableSortKey)
});
const kinesisPipeline = this.createKinesisPipeline(stackConfig.KinesisConfig, kinesisBucket, glueDatabase, glueTable);
this.createDdbToLambda(stackConfig.LambdaConfig, backendTableName, kinesisPipeline);
const athenaBucket = this.createAthenaResources(stackConfig.AthenaConfig, glueDatabase, glueTable, backendTablePartitionKey, backendTableSortKey);
this.createQuickSightRole([
kinesisBucket, athenaBucket
]);
this.nagSuppress();
}
private createKinesisPipeline(config: KinesisConfig, kinesisBucket: s3.Bucket, glueDatabase: glue.Database, glueTable: glue.Table) {
const pipeline = new KinesisStreamsToKinesisFirehoseToS3(this, 'stream-firehose-s3', {
kinesisStreamProps: {
streamName: this.withStackName(config.KinesisStreamName),
encryption: kinesis.StreamEncryption.MANAGED
},
kinesisFirehoseProps: {
deliveryStreamType: 'KinesisStreamAsSource',
deliveryStreamName: this.withStackName(config.KinesisFireHoseName),
extendedS3DestinationConfiguration: {
bucketArn: kinesisBucket.bucketArn,
compressionFormat: 'UNCOMPRESSED',
bufferingHints: {
sizeInMBs: 64,
intervalInSeconds: 60
},
dataFormatConversionConfiguration: {
enabled: true,
inputFormatConfiguration: {
deserializer: { openXJsonSerDe: {} }
},
outputFormatConfiguration: {
serializer: { parquetSerDe: {} }
},
schemaConfiguration: {
region: this.commonProps.appConfig.Project.Region,
roleArn: this.createFireHoseRole().roleArn,
versionId: 'LATEST',
databaseName: glueDatabase.databaseName,
tableName: glueTable.tableName
},
}
}
},
existingBucketObj: kinesisBucket,
});
this.putParameter(`${config.KinesisStreamName}StreamName`, pipeline.kinesisStream.streamName);
this.putParameter(`${config.KinesisFireHoseName}HoseName`, pipeline.kinesisFirehose.deliveryStreamName!);
return pipeline;
}
private createDdbToLambda(config: LambdaConfig, backendTableName: string, kinesisPipeline: KinesisStreamsToKinesisFirehoseToS3) {
const backendTableArn = this.getParameter(`${backendTableName}TableArn`);
const backendTableStreamArn = this.getParameter(`${backendTableName}TableStreamArn`);
const backendTable = ddb.Table.fromTableAttributes(this, 'backend-table', {
tableArn: backendTableArn,
tableStreamArn: backendTableStreamArn
});
const streamLambda = new DynamoDBStreamsToLambda(this, 'dynamodbstreams-lambda', {
existingTableInterface: backendTable,
lambdaFunctionProps: {
functionName: this.withStackName(config.LambdaFuncName),
runtime: lambda.Runtime.PYTHON_3_9,
code: lambda.Code.fromAsset(config.LambdaFuncCode),
handler: config.LambdaFuncHandler,
memorySize: config.LambdaFuncMemory,
environment: {
STREAM_NAME: kinesisPipeline.kinesisStream.streamName,
STREAM_BATCH_SIZE: config.StreamBatchSize
},
deadLetterQueue: new sqs.Queue(this, 'stream-lambda-dlq', {encryption: sqs.QueueEncryption.KMS_MANAGED}),
},
dynamoEventSourceProps: {
startingPosition: lambda.StartingPosition.LATEST,
batchSize: config.LambdaFuncBatch,
maxBatchingWindow: cdk.Duration.minutes(config.LambdaFuncWindow),
onFailure: new lambdaEvent.SqsDlq(new sqs.Queue(this, 'sqs-dlq', {encryption: sqs.QueueEncryption.KMS_MANAGED})),
}
});
kinesisPipeline.kinesisStream.grantWrite(streamLambda.lambdaFunction);
streamLambda.lambdaFunction.role?.addToPrincipalPolicy(new iam.PolicyStatement({
resources: ['*'],
actions: ['comprehend:BatchDetectEntities', 'comprehend:BatchDetectSyntax'],
}));
this.putParameter(`${config.LambdaFuncName}FunctionArn`, streamLambda.lambdaFunction.functionArn);
}
private createAthenaResources(cofig: AthenaConfig, glueDatabase: glue.Database, glueTable: glue.Table, backendTablePartitionKey: string, backendTableSortKey: string): s3.Bucket {
const athenaBucket = this.createSecureS3Bucket({
bucketId: 'athena-bucket',
serverAccessLogsBucket: this.createSecureS3Bucket({bucketId: 'athena-access'})
});
const athenaWorkGroup = new athena.CfnWorkGroup(this, 'athena-wg', {
name: this.withStackName(cofig.AtheanGroupName).toLowerCase(),
workGroupConfiguration: {
resultConfiguration: {
outputLocation: `s3://${athenaBucket.bucketName}/query/`
}
}
});
this.createAthenaQueriesSentiment(cofig, athenaWorkGroup, glueDatabase, glueTable, backendTablePartitionKey, backendTableSortKey);
this.createAthenaQueriesEntities(cofig, athenaWorkGroup, glueDatabase, glueTable, backendTablePartitionKey, backendTableSortKey);
this.createAthenaQueriesSyntax(cofig, athenaWorkGroup, glueDatabase, glueTable, backendTablePartitionKey, backendTableSortKey);
return athenaBucket;
}
private createQuickSightRole(bucketList: s3.Bucket[]) {
const quicksightRole = new iam.Role(this, 'quicksight-role', {
assumedBy: new iam.ServicePrincipal('quicksight.amazonaws.com'),
});
this.exportOutput('QuickSightRole', quicksightRole.roleName);
quicksightRole.addToPrincipalPolicy(new iam.PolicyStatement({
actions: [
'iam:List*',
],
resources: [
'*'
],
}));
quicksightRole.addToPrincipalPolicy(new iam.PolicyStatement({
actions: [
's3:ListAllMyBuckets',
],
resources: [
'arn:aws:s3:::*'
],
}));
quicksightRole.addToPrincipalPolicy(new iam.PolicyStatement({
actions: [
's3:ListBucket',
's3:ListBucketMultipartUploads',
's3:GetBucketLocation',
],
resources: bucketList.map(bucket => bucket.bucketArn)
}));
quicksightRole.addToPrincipalPolicy(new iam.PolicyStatement({
actions: [
's3:GetObject',
's3:GetObjectVersion',
's3:PutObject',
's3:AbortMultipartUpload',
's3:ListMultipartUploadParts',
],
resources: bucketList.map(bucket => bucket.bucketArn + '/*')
}));
quicksightRole.addManagedPolicy(iam.ManagedPolicy.fromManagedPolicyArn(this, 'AWSQuicksightAthenaAccess', 'arn:aws:iam::aws:policy/service-role/AWSQuicksightAthenaAccess'));
}
private createFireHoseRole(): iam.Role {
const hoseRole = new iam.Role(this, 'host-role', {
assumedBy: new iam.ServicePrincipal('firehose.amazonaws.com'),
});
hoseRole.addManagedPolicy({ managedPolicyArn: 'arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole' });
hoseRole.addManagedPolicy({ managedPolicyArn: 'arn:aws:iam::aws:policy/AWSLambda_FullAccess' });
hoseRole.addManagedPolicy({ managedPolicyArn: 'arn:aws:iam::aws:policy/AmazonKinesisFullAccess' });
hoseRole.addToPolicy(new iam.PolicyStatement({
resources: ['*'],
actions: ['kms:*'],
}));
hoseRole.addToPolicy(new iam.PolicyStatement({
resources: ['*'],
actions: ['logs:*'],
}));
hoseRole.addToPolicy(new iam.PolicyStatement({
resources: ['*'],
actions: ['s3:*'],
}));
return hoseRole;
}
private createAthenaQueriesSentiment(config: AthenaConfig, athenaWorkGroup: athena.CfnWorkGroup, glueDatabase: glue.Database, glueTable: glue.Table, backendTablePartitionKey: string, backendTableSortKey: string) {
const databaseTableName = `"${glueDatabase.databaseName}"."${glueTable.tableName}"`;
const sentimentTableName = config.AtheanQuerySentiment;
const athenaQuerySentiment = new athena.CfnNamedQuery(this, `create-${sentimentTableName}`, {
name: `create-${sentimentTableName}`,
workGroup: athenaWorkGroup.name,
database: glueDatabase.databaseName,
queryString: `CREATE TABLE "${sentimentTableName}" AS
SELECT ${backendTablePartitionKey}, ${backendTableSortKey}, timestamp,
sentiment.sentiment as sentiment,
sentiment.sentimentscore.mixed AS mixed,
sentiment.sentimentscore.negative AS negative,
sentiment.sentimentscore.neutral AS neutral,
sentiment.sentimentscore.positive AS positive
FROM ${databaseTableName};`
});
athenaQuerySentiment.addDependsOn(athenaWorkGroup);
const athenaQuerySentimentGroupby = new athena.CfnNamedQuery(this, `group-by-${sentimentTableName}`, {
name: `group-by-${sentimentTableName}`,
workGroup: athenaWorkGroup.name,
database: glueDatabase.databaseName,
queryString: `SELECT sentiment, count(${backendTableSortKey}) as number
FROM ${sentimentTableName};`
});
athenaQuerySentimentGroupby.addDependsOn(athenaWorkGroup);
}
private createAthenaQueriesEntities(config: AthenaConfig, athenaWorkGroup: athena.CfnWorkGroup, glueDatabase: glue.Database, glueTable: glue.Table, backendTablePartitionKey: string, backendTableSortKey: string) {
const databaseTableName = `"${glueDatabase.databaseName}"."${glueTable.tableName}"`;
const entitiesTableName = config.AtheanQueryEntities;
const entitiesTempTableName = `temp-${entitiesTableName}`
const athenaQueryEntitiesTemp = new athena.CfnNamedQuery(this, `create-${entitiesTempTableName}`, {
name: `create-${entitiesTempTableName}`,
workGroup: athenaWorkGroup.name,
database: glueDatabase.databaseName,
queryString: `CREATE TABLE "${entitiesTempTableName}" AS
SELECT ${backendTablePartitionKey}, ${backendTableSortKey}, sentiment.sentiment as sentiment, nested FROM ${databaseTableName}
CROSS JOIN UNNEST(entities) as t(nested);`
});
athenaQueryEntitiesTemp.addDependsOn(athenaWorkGroup);
const athenaQueryEntities = new athena.CfnNamedQuery(this, `create-${entitiesTableName}`, {
name: `create-${entitiesTableName}`,
workGroup: athenaWorkGroup.name,
database: glueDatabase.databaseName,
queryString: `CREATE TABLE "${entitiesTableName}" AS
SELECT ${backendTablePartitionKey}, ${backendTableSortKey}, sentiment,
nested.beginoffset AS beginoffset,
nested.endoffset AS endoffset,
nested.text AS text,
nested.score AS score,
nested.type AS category
FROM "${entitiesTempTableName}";`
});
athenaQueryEntities.addDependsOn(athenaWorkGroup);
athenaQueryEntities.addDependsOn(athenaQueryEntitiesTemp);
}
private createAthenaQueriesSyntax(config: AthenaConfig, athenaWorkGroup: athena.CfnWorkGroup, glueDatabase: glue.Database, glueTable: glue.Table, backendTablePartitionKey: string, backendTableSortKey: string) {
const databaseTableName = `"${glueDatabase.databaseName}"."${glueTable.tableName}"`;
const syntaxTableName = config.AtheanQuerySyntax
const syntaxTempTableName = `temp-${syntaxTableName}`
const athenaQuerySyntaxTemp = new athena.CfnNamedQuery(this, `create-${syntaxTempTableName}`, {
name: `create-${syntaxTempTableName}`,
workGroup: athenaWorkGroup.name,
database: glueDatabase.databaseName,
queryString: `CREATE TABLE "${syntaxTempTableName}" AS
SELECT ${backendTablePartitionKey}, ${backendTableSortKey}, sentiment.sentiment as sentiment, nested FROM ${databaseTableName}
CROSS JOIN UNNEST(syntax) as t(nested);`
});
athenaQuerySyntaxTemp.addDependsOn(athenaWorkGroup);
const athenaQuerySyntax = new athena.CfnNamedQuery(this, `create-${syntaxTableName}`, {
name: `create-${syntaxTableName}`,
workGroup: athenaWorkGroup.name,
database: glueDatabase.databaseName,
queryString: `CREATE TABLE "${syntaxTableName}" AS
SELECT ${backendTablePartitionKey}, ${backendTableSortKey}, sentiment,
nested.beginoffset AS beginoffset,
nested.endoffset AS endoffset,
nested.text AS text,
nested.partofspeech.score AS score,
nested.partofspeech.tag AS tag
FROM "${syntaxTempTableName}"
WHERE nested.partofspeech.tag = 'ADJ' OR nested.partofspeech.tag = 'ADV';`
});
athenaQuerySyntax.addDependsOn(athenaWorkGroup);
athenaQuerySyntax.addDependsOn(athenaQuerySyntaxTemp);
}
private loadTableSchemaColumns(partitionKey: string, sortKey: string): glue.Column[] {
return [
{
name: partitionKey,
type: glue.Schema.STRING,
}, {
name: sortKey,
type: glue.Schema.STRING,
}, {
name: 'Timestamp',
type: glue.Schema.TIMESTAMP,
}, {
name: 'Review',
type: glue.Schema.STRING,
}, {
name: 'Sentiment',
type: glue.Schema.struct([
{
name: 'Sentiment',
type: glue.Schema.STRING,
}, {
name: 'SentimentScore',
type: glue.Schema.struct([
{
name: 'Mixed',
type: glue.Schema.DOUBLE,
},
{
name: 'Negative',
type: glue.Schema.DOUBLE,
},
{
name: 'Neutral',
type: glue.Schema.DOUBLE,
},
{
name: 'Positive',
type: glue.Schema.DOUBLE,
}
])
}
]),
}, {
name: 'Entities',
type: glue.Schema.array(glue.Schema.struct([
{
name: 'Score',
type: glue.Schema.DOUBLE,
}, {
name: 'Type',
type: glue.Schema.STRING
}, {
name: 'Text',
type: glue.Schema.STRING
}, {
name: 'BeginOffset',
type: glue.Schema.INTEGER
}, {
name: 'EndOffset',
type: glue.Schema.INTEGER
},
]))
}, {
name: 'Syntax',
type: glue.Schema.array(glue.Schema.struct([
{
name: 'TokenId',
type: glue.Schema.INTEGER
}, {
name: 'Text',
type: glue.Schema.STRING
}, {
name: 'BeginOffset',
type: glue.Schema.INTEGER
}, {
name: 'EndOffset',
type: glue.Schema.INTEGER
}, {
name: 'PartOfSpeech',
type: glue.Schema.struct([
{
name: 'Score',
type: glue.Schema.DOUBLE,
}, {
name: 'Tag',
type: glue.Schema.STRING
}
])
}
]))
}
];
}
private nagSuppress() {
NagSuppressions.addStackSuppressions(this, [
{
id: 'AwsSolutions-S1',
reason: 'Demonstrate a stack level suppression.'
},
{
id: 'AwsSolutions-S2',
reason: 'Demonstrate a stack level suppression.'
},
{
id: 'AwsSolutions-IAM4',
reason: 'Demonstrate a stack level suppression.'
},
{
id: 'AwsSolutions-IAM5',
reason: 'Demonstrate a stack level suppression.'
},
{
id: 'AwsSolutions-SQS3',
reason: 'Demonstrate a stack level suppression.'
},
{
id: 'AwsSolutions-SQS4',
reason: 'Demonstrate a stack level suppression.'
},
{
id: 'AwsSolutions-ATH1',
reason: 'Demonstrate a stack level suppression.'
},
{
id: 'AwsSolutions-KDF1',
reason: 'Demonstrate a stack level suppression.'
},
]);
}
}