@@ -46,7 +46,7 @@ import { QueueEncryption } from "@aws-cdk/aws-sqs";
4646import { LogGroup } from "@aws-cdk/aws-logs" ;
4747import { LogGroupLogDestination } from "@aws-cdk/aws-apigateway" ;
4848
49- const API_CONCURRENT_REQUESTS = 20 ; //approximate number of 1-2 page documents to be processed parallelly
49+ const API_CONCURRENT_REQUESTS = 20 ; //approximate number of 1-2 page documents to be processed in parallell
5050
5151export interface TextractStackProps {
5252 email : string ;
@@ -171,12 +171,14 @@ export class CdkTextractStack extends cdk.Stack {
171171 behaviors : [ { isDefaultBehavior : true } ] ,
172172 } ,
173173 ] ,
174- errorConfigurations : [ {
175- errorCode : 404 ,
176- responseCode : 200 ,
177- errorCachingMinTtl : 5 ,
178- responsePagePath : '/index.html'
179- } ] ,
174+ errorConfigurations : [
175+ {
176+ errorCode : 404 ,
177+ responseCode : 200 ,
178+ errorCachingMinTtl : 5 ,
179+ responsePagePath : "/index.html" ,
180+ } ,
181+ ] ,
180182 priceClass : PriceClass . PRICE_CLASS_100 ,
181183 httpVersion : HttpVersion . HTTP2 ,
182184 enableIpV6 : true ,
@@ -324,12 +326,39 @@ export class CdkTextractStack extends cdk.Stack {
324326 elasticSearch . node . addDependency ( serviceLinkedRole ) ;
325327 }
326328
329+ const jobResultsKey = new kms . Key (
330+ this ,
331+ this . resourceName ( "JobResultsKey" ) ,
332+ {
333+ enableKeyRotation : true ,
334+ enabled : true ,
335+ trustAccountIdentities : true ,
336+ policy : new iam . PolicyDocument ( {
337+ assignSids : true ,
338+ statements : [
339+ new iam . PolicyStatement ( {
340+ actions : [ "kms:GenerateDataKey*" , "kms:Decrypt" ] ,
341+ resources : [ "*" ] , // Resource level permissions are not necessary in this policy statement, as it is automatically restricted to this key
342+ effect : iam . Effect . ALLOW ,
343+ principals : [
344+ new iam . ServicePrincipal ( "sns.amazonaws.com" ) ,
345+ new iam . ServicePrincipal ( "lambda.amazonaws.com" ) ,
346+ new iam . ServicePrincipal ( "textract.amazonaws.com" ) ,
347+ new iam . ServicePrincipal ( "sqs.amazonaws.com" ) ,
348+ ] ,
349+ } ) ,
350+ ] ,
351+ } ) ,
352+ }
353+ ) ;
354+
327355 // SNS Topic
328356 const jobCompletionTopic = new sns . Topic (
329357 this ,
330- this . resourceName ( "JobCompletion " ) ,
358+ this . resourceName ( "JobCompletionTopic " ) ,
331359 {
332360 displayName : "Job completion topic" ,
361+ masterKey : jobResultsKey ,
333362 }
334363 ) ;
335364
@@ -349,6 +378,13 @@ export class CdkTextractStack extends cdk.Stack {
349378 resources : [ jobCompletionTopic . topicArn ] ,
350379 } )
351380 ) ;
381+ textractServiceRole . addToPolicy (
382+ new iam . PolicyStatement ( {
383+ effect : iam . Effect . ALLOW ,
384+ actions : [ "kms:Decrypt" , "kms:GenerateDataKey*" ] ,
385+ resources : [ jobResultsKey . keyArn ] ,
386+ } )
387+ ) ;
352388
353389 // DynamoDB tables
354390 const outputTable = new ddb . Table ( this , this . resourceName ( "OutputTable" ) , {
@@ -440,6 +476,7 @@ export class CdkTextractStack extends cdk.Stack {
440476 {
441477 visibilityTimeout : cdk . Duration . seconds ( 900 ) ,
442478 retentionPeriod : cdk . Duration . seconds ( 1209600 ) ,
479+ encryption : QueueEncryption . KMS_MANAGED ,
443480 }
444481 ) ;
445482
@@ -449,12 +486,16 @@ export class CdkTextractStack extends cdk.Stack {
449486 {
450487 visibilityTimeout : cdk . Duration . seconds ( 900 ) ,
451488 retentionPeriod : cdk . Duration . seconds ( 1209600 ) ,
489+ encryption : QueueEncryption . KMS ,
490+ encryptionMasterKey : jobResultsKey ,
491+ dataKeyReuse : cdk . Duration . seconds ( 86400 ) ,
452492 deadLetterQueue : {
453493 maxReceiveCount : 3 ,
454494 queue : jobResultsDLQueue ,
455495 } ,
456496 }
457497 ) ;
498+
458499 // trigger
459500 jobCompletionTopic . addSubscription (
460501 new snsSubscriptions . SqsSubscription ( jobResultsQueue )
@@ -874,6 +915,7 @@ export class CdkTextractStack extends cdk.Stack {
874915 jobResultProcessor . addLayers ( textractorLayer ) ;
875916 jobResultProcessor . addLayers ( boto3Layer ) ;
876917 jobResultProcessor . addLayers ( elasticSearchLayer ) ;
918+ jobResultsKey . grantEncryptDecrypt ( jobResultProcessor ) ;
877919
878920 // Triggers
879921 jobResultProcessor . addEventSource (
0 commit comments