Wrong S3 bucket (destination bucket) configured as trigger for Lambda in typescript CDK

0

Hi all. I'm provisioning a CloudFormation stack using CDK in typescript.

Synopsis

  • The CF stack consists of two buckets and one lambdas function.
  • One bucket is for uploading original images, the other bucket is for storing processed images.
  • The lambda function should be triggered when I upload images to the originals folder (input), then resizes the images and puts them into the processed folder (output).

Problem

When I provision the stack with cdk deploy and check the lambda function on the console, The output folder for processed images is set as the trigger. I tried changing the bucket configurations in functions related to SNS notifications and lambda, but it doesn't seem to work.

Below is part of my stack to give you a general idea about my code structure. Can someone kindly indicate where I should look? Thank you!

export class ServerlessCdkStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {

    ...
    const uploadsBucket = this.createBucket(uploadsBucketName);
    const processedBucket = this.importBucket(processedBucketName);
    const lambda = this.createLambda(functionPath, uploadsBucketName, processedBucketName, folderInput, folderOutput);

    const snsTopic = this.createSnsTopic(topicName);
    this.createSnsSubscription(snsTopic, webhookUrl);
    this.createS3NotifyToSns(folderInput, snsTopic, uploadsBucket);
    this.createS3NotifyToLambda(folderInput, lambda, processedBucket);
  } 

  createBucket(bucketName: string): s3.IBucket {
    const bucket = new s3.Bucket(this, 'UploadsBucket', {
      bucketName: bucketName,
      removalPolicy: cdk.RemovalPolicy.DESTROY
    });
    return bucket;

  createLambda(functionPath: string, uploadsBucketName: string, processedBucketName: string, folderInput: string, folderOutput: string): lambda.IFunction {
    const lambdaFunction = new lambda.Function(this, 'ImageProcessingLambda', {
      runtime: lambda.Runtime.NODEJS_18_X,
      handler: 'index.handler',
      code: lambda.Code.fromAsset(functionPath),
      environment: {
        DEST_BUCKET_NAME: processedBucketName,
        FOLDER_INPUT: uploadsBucketName,
        FOLDER_OUTPUT: processedBucketName,
        PROCESS_WIDTH: '512',
        PROCESS_HEIGHT: '512'
      }
    });
    return lambdaFunction;
  }

  createS3NotifyToLambda(prefix: string, lambda: lambda.IFunction, bucket: s3.IBucket): void {
    const destination = new s3n.LambdaDestination(lambda);
    bucket.addEventNotification(s3.EventType.OBJECT_CREATED_PUT,
      destination,
      {prefix: prefix} // folder to contain the original images
    )
  }
 }
1 Answer
2
Accepted Answer

In the following section, we need to make sure that the parameter passed to "bucket" is the correct bucket.

  createS3NotifyToLambda(prefix: string, lambda: lambda.IFunction, bucket: s3.IBucket): void {
    const destination = new s3n.LambdaDestination(lambda);
    bucket.addEventNotification(s3.EventType.OBJECT_CREATED_PUT,
      destination,
      {prefix: prefix} // folder to contain the original images
    )
  }

In other words, if I am correct, you need to change "processedBucket" to "uploadsBucket" in the next section.

this.createS3NotifyToLambda(folderInput, lambda, processedBucket);
profile picture
EXPERT
answered 10 months ago
profile pictureAWS
EXPERT
reviewed 10 months ago
  • Helloo, Riku, That did resolve my issue. I appreciate it :) !

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions