import * as cdk from 'aws-cdk-lib'; import * as s3 from 'aws-cdk-lib/aws-s3'; import * as s3n from 'aws-cdk-lib/aws-s3-notifications'; import * as lambda from 'aws-cdk-lib/aws-lambda'; import {Bucket, EventType, IBucket} from 'aws-cdk-lib/aws-s3'; export class S3EventNotificationsStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { super(scope, id, props); // Create an S3 bucket const bucketa = new s3.Bucket(this, 'MyBucket', { bucketName: 'test-event-notifications1', autoDeleteObjects: true, // This will automatically delete the objects when the bucket is deleted removalPolicy: cdk.RemovalPolicy.DESTROY, // This will delete the bucket when the stack is deleted }); const bucket = Bucket.fromBucketName( this, 'notifications-ravsaran', bucketa.bucketName, ); console.log("print value : " + (bucket instanceof Bucket)); // Create a Lambda function to handle the S3 event notifications const lambdaFunction = new lambda.Function(this, 'MyLambdaFunction', { runtime: lambda.Runtime.NODEJS_LATEST, handler: 'index.handler', code: lambda.Code.fromInline(` exports.handler = async (event) => { console.log('S3 event:', event); // Add your custom logic to handle the S3 event return { statusCode: 200 }; }; `), environment: { BUCKET_NAME: bucket.bucketName, }, }); // Grant the Lambda function permissions to access the S3 bucket bucket.grantReadWrite(lambdaFunction); /* * Step 1: Deploy the stack by adding two event notifications. * Step 2: Remove one of the event notifications, then redeploy the stack to reproduce the issue. */ bucket.addEventNotification(s3.EventType.OBJECT_CREATED, new s3n.LambdaDestination(lambdaFunction), { prefix: 'TEST/' }); bucket.addEventNotification(s3.EventType.OBJECT_CREATED, new s3n.LambdaDestination(lambdaFunction), { prefix: 'TEST_CDD/' }); } } const app = new cdk.App(); new S3EventNotificationsStack(app, 'S3EventNotificationsStack'); app.synth();