11import { Construct } from "constructs"
2- import { Bucket , EventType , CfnBucket } from "aws-cdk-lib/aws-s3"
2+ import { Bucket , EventType } from "aws-cdk-lib/aws-s3"
3+ import { LambdaDestination } from "aws-cdk-lib/aws-s3-notifications"
34import { 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"
65
76export interface S3LambdaNotificationProps {
87 bucket : Bucket
@@ -13,33 +12,11 @@ export class S3LambdaNotification extends Construct {
1312 constructor ( scope : Construct , id : string , props : S3LambdaNotificationProps ) {
1413 super ( scope , id )
1514
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- } )
15+ const lambdaDestination = new LambdaDestination ( props . lambdaFunction )
2316
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- }
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 )
4421 }
4522}
0 commit comments