11import { Construct } from "constructs"
2- import { Bucket , EventType } from "aws-cdk-lib/aws-s3"
3- import { LambdaDestination } from "aws-cdk-lib/aws-s3-notifications"
2+ import { Bucket , EventType , CfnBucket } from "aws-cdk-lib/aws-s3"
43import { Function as LambdaFunction } from "aws-cdk-lib/aws-lambda"
4+ import { Aws } from "aws-cdk-lib"
5+ import { ServicePrincipal } from "aws-cdk-lib/aws-iam"
56
67export interface S3LambdaNotificationProps {
78 bucket : Bucket
@@ -12,11 +13,33 @@ export class S3LambdaNotification extends Construct {
1213 constructor ( scope : Construct , id : string , props : S3LambdaNotificationProps ) {
1314 super ( scope , id )
1415
15- const lambdaDestination = new LambdaDestination ( props . lambdaFunction )
16+ // Add source account to Lambda permission for NCSC compliance
17+ props . lambdaFunction . addPermission ( `S3Invoke-${ this . node . id } ` , {
18+ principal : new ServicePrincipal ( "s3.amazonaws.com" ) ,
19+ action : "lambda:InvokeFunction" ,
20+ sourceAccount : Aws . ACCOUNT_ID ,
21+ sourceArn : props . bucket . bucketArn
22+ } )
1623
17- // Listen for all object events to keep knowledge base in sync
18- props . bucket . addEventNotification ( EventType . OBJECT_CREATED , lambdaDestination )
19- props . bucket . addEventNotification ( EventType . OBJECT_REMOVED , lambdaDestination )
20- props . bucket . addEventNotification ( EventType . OBJECT_RESTORE_COMPLETED , lambdaDestination )
24+ // Get the underlying CfnBucket to configure notifications directly
25+ const cfnBucket = props . bucket . node . defaultChild as CfnBucket
26+
27+ // Configure notifications directly on the CfnBucket to avoid auto-permission creation
28+ cfnBucket . notificationConfiguration = {
29+ lambdaConfigurations : [
30+ {
31+ event : EventType . OBJECT_CREATED ,
32+ function : props . lambdaFunction . functionArn
33+ } ,
34+ {
35+ event : EventType . OBJECT_REMOVED ,
36+ function : props . lambdaFunction . functionArn
37+ } ,
38+ {
39+ event : EventType . OBJECT_RESTORE_COMPLETED ,
40+ function : props . lambdaFunction . functionArn
41+ }
42+ ]
43+ }
2144 }
2245}
0 commit comments