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개 답변
2
수락된 답변

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
전문가
답변함 10달 전
profile pictureAWS
전문가
검토됨 9달 전
  • Helloo, Riku, That did resolve my issue. I appreciate it :) !

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠