Add lambda function for image resizing#946
Conversation
|
@NiallJoeMaher is attempting to deploy a commit to the Codú Team on Vercel. A member of the Team first needs to authorize it. |
|
Warning Rate limit exceeded@NiallJoeMaher has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 17 minutes and 3 seconds before requesting another review. How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. WalkthroughThese changes include removing an intermediary function from an existing Lambda to simplify its logic, adding a new Lambda function for resizing images uploaded to an S3 bucket, and updating the AWS CDK stack to reflect these changes. The stack now manages two distinct Lambda functions: one for avatar resizing and another for general image uploads. Changes
Sequence Diagram(s)sequenceDiagram
participant S3
participant Lambda (Avatar Resize)
participant Lambda (Upload Resize)
S3-->>Lambda (Avatar Resize): Object Created (avatar/)
Lambda (Avatar Resize)-->>S3: Upload resized versions
S3-->>Lambda (Upload Resize): Object Created (public/)
Lambda (Upload Resize)-->>S3: Upload resized versions
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Actionable comments posted: 3
Outside diff range and nitpick comments (1)
cdk/lambdas/avatarResize/index.js (1)
Line range hint
8-33:
Consider optimizing the stream handling for large images.Using
Buffer.concat(await stream.toArray())might not be optimal for large images. Consider using streams to handle large images more efficiently.- const resizedImage = await sharp(Buffer.concat(await stream.toArray())) + const resizedImage = await sharp(stream)
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files ignored due to path filters (4)
cdk/lambdas/avatarResize/package-lock.jsonis excluded by!**/package-lock.json,!**/*.jsoncdk/lambdas/avatarResize/package.jsonis excluded by!**/*.jsoncdk/lambdas/uploadResize/package-lock.jsonis excluded by!**/package-lock.json,!**/*.jsoncdk/lambdas/uploadResize/package.jsonis excluded by!**/*.json
Files selected for processing (3)
- cdk/lambdas/avatarResize/index.js (2 hunks)
- cdk/lambdas/uploadResize/index.js (1 hunks)
- cdk/lib/storage-stack.ts (1 hunks)
Additional comments not posted (5)
cdk/lambdas/avatarResize/index.js (2)
Line range hint
1-6:
LGTM!The imports and S3 client initialization are appropriate and necessary for the functionality.
Line range hint
34-43:
LGTM!The upload of the resized image and the success response handling are correctly implemented.
cdk/lambdas/uploadResize/index.js (2)
1-18: LGTM!The imports, S3 client initialization, and size definitions are appropriate and necessary for the functionality.
36-75: LGTM!The resizing, uploading, and success response handling are correctly implemented.
cdk/lib/storage-stack.ts (1)
Line range hint
1-61:
LGTM!The imports and class definition are appropriate and necessary for the functionality.
| exports.handler = async (event) => { | ||
| const bucket = event.Records[0].s3.bucket.name; | ||
| const key = event.Records[0].s3.object.key; | ||
|
|
||
| const params = { | ||
| Bucket: bucket, | ||
| Key: key, | ||
| }; | ||
|
|
||
| try { | ||
| const response = await s3.send(new GetObjectCommand(params)); | ||
| const stream = response.Body; | ||
|
|
||
| if (!stream) throw new Error("BodyStream is empty"); | ||
|
|
||
| const imageBuffer = Buffer.concat(await stream.toArray()); |
There was a problem hiding this comment.
Consider optimizing the stream handling for large images.
Using Buffer.concat(await stream.toArray()) might not be optimal for large images. Consider using streams to handle large images more efficiently.
- const imageBuffer = Buffer.concat(await stream.toArray());
+ const imageBuffer = await stream;Committable suggestion was skipped due to low confidence.
| // Lambda for resizing avatar uploads | ||
| const s3AvatarEventHandler = new NodejsFunction(this, "ResizeAvatar", { | ||
| runtime: lambda.Runtime.NODEJS_20_X, | ||
| entry: path.join(__dirname, "/../lambdas/imageResize/index.js"), | ||
| timeout: cdk.Duration.seconds(120), | ||
| depsLockFilePath: path.join( | ||
| __dirname, | ||
| "/../lambdas/avatarResize/package-lock.json", | ||
| ), | ||
| bundling: { | ||
| nodeModules: ["sharp", "@aws-sdk/client-s3"], | ||
| }, | ||
| }); |
There was a problem hiding this comment.
Fix the entry point path for the Lambda function.
The entry point path for the Lambda function should be ../lambdas/avatarResize/index.js instead of ../lambdas/imageResize/index.js.
- entry: path.join(__dirname, "/../lambdas/imageResize/index.js"),
+ entry: path.join(__dirname, "/../lambdas/avatarResize/index.js"),Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // Lambda for resizing avatar uploads | |
| const s3AvatarEventHandler = new NodejsFunction(this, "ResizeAvatar", { | |
| runtime: lambda.Runtime.NODEJS_20_X, | |
| entry: path.join(__dirname, "/../lambdas/imageResize/index.js"), | |
| timeout: cdk.Duration.seconds(120), | |
| depsLockFilePath: path.join( | |
| __dirname, | |
| "/../lambdas/avatarResize/package-lock.json", | |
| ), | |
| bundling: { | |
| nodeModules: ["sharp", "@aws-sdk/client-s3"], | |
| }, | |
| }); | |
| // Lambda for resizing avatar uploads | |
| const s3AvatarEventHandler = new NodejsFunction(this, "ResizeAvatar", { | |
| runtime: lambda.Runtime.NODEJS_20_X, | |
| entry: path.join(__dirname, "/../lambdas/avatarResize/index.js"), | |
| timeout: cdk.Duration.seconds(120), | |
| depsLockFilePath: path.join( | |
| __dirname, | |
| "/../lambdas/avatarResize/package-lock.json", | |
| ), | |
| bundling: { | |
| nodeModules: ["sharp", "@aws-sdk/client-s3"], | |
| }, | |
| }); |
| const s3UploadEventHandler = new NodejsFunction(this, "ResizeAvatar", { | ||
| runtime: lambda.Runtime.NODEJS_20_X, | ||
| entry: path.join(__dirname, "/../lambdas/imageResize/index.js"), | ||
| timeout: cdk.Duration.seconds(120), | ||
| depsLockFilePath: path.join( | ||
| __dirname, | ||
| "/../lambdas/imageResize/package-lock.json", | ||
| "/../lambdas/avatarResize/package-lock.json", | ||
| ), | ||
| bundling: { | ||
| nodeModules: ["sharp", "@aws-sdk/client-s3"], | ||
| }, | ||
| }); | ||
|
|
||
| this.bucket.grantReadWrite(s3EventHandler); | ||
| this.bucket.grantReadWrite(s3AvatarEventHandler); | ||
| this.bucket.grantReadWrite(s3UploadEventHandler); | ||
|
|
||
| s3EventHandler.addEventSource( | ||
| s3AvatarEventHandler.addEventSource( | ||
| new S3EventSource(this.bucket, { | ||
| events: [s3.EventType.OBJECT_CREATED], | ||
| filters: [{ prefix: "u/" }], | ||
| }), | ||
| ); | ||
|
|
||
| s3UploadEventHandler.addEventSource( | ||
| new S3EventSource(this.bucket, { | ||
| events: [s3.EventType.OBJECT_CREATED], | ||
| filters: [{ prefix: "public/" }], | ||
| }), | ||
| ); |
There was a problem hiding this comment.
Fix the entry point path for the Lambda function.
The entry point path for the Lambda function should be ../lambdas/uploadResize/index.js instead of ../lambdas/imageResize/index.js.
- entry: path.join(__dirname, "/../lambdas/imageResize/index.js"),
+ entry: path.join(__dirname, "/../lambdas/uploadResize/index.js"),Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const s3UploadEventHandler = new NodejsFunction(this, "ResizeAvatar", { | |
| runtime: lambda.Runtime.NODEJS_20_X, | |
| entry: path.join(__dirname, "/../lambdas/imageResize/index.js"), | |
| timeout: cdk.Duration.seconds(120), | |
| depsLockFilePath: path.join( | |
| __dirname, | |
| "/../lambdas/imageResize/package-lock.json", | |
| "/../lambdas/avatarResize/package-lock.json", | |
| ), | |
| bundling: { | |
| nodeModules: ["sharp", "@aws-sdk/client-s3"], | |
| }, | |
| }); | |
| this.bucket.grantReadWrite(s3EventHandler); | |
| this.bucket.grantReadWrite(s3AvatarEventHandler); | |
| this.bucket.grantReadWrite(s3UploadEventHandler); | |
| s3EventHandler.addEventSource( | |
| s3AvatarEventHandler.addEventSource( | |
| new S3EventSource(this.bucket, { | |
| events: [s3.EventType.OBJECT_CREATED], | |
| filters: [{ prefix: "u/" }], | |
| }), | |
| ); | |
| s3UploadEventHandler.addEventSource( | |
| new S3EventSource(this.bucket, { | |
| events: [s3.EventType.OBJECT_CREATED], | |
| filters: [{ prefix: "public/" }], | |
| }), | |
| ); | |
| const s3UploadEventHandler = new NodejsFunction(this, "ResizeAvatar", { | |
| runtime: lambda.Runtime.NODEJS_20_X, | |
| entry: path.join(__dirname, "/../lambdas/uploadResize/index.js"), | |
| timeout: cdk.Duration.seconds(120), | |
| depsLockFilePath: path.join( | |
| __dirname, | |
| "/../lambdas/avatarResize/package-lock.json", | |
| ), | |
| bundling: { | |
| nodeModules: ["sharp", "@aws-sdk/client-s3"], | |
| }, | |
| }); | |
| this.bucket.grantReadWrite(s3AvatarEventHandler); | |
| this.bucket.grantReadWrite(s3UploadEventHandler); | |
| s3AvatarEventHandler.addEventSource( | |
| new S3EventSource(this.bucket, { | |
| events: [s3.EventType.OBJECT_CREATED], | |
| filters: [{ prefix: "u/" }], | |
| }), | |
| ); | |
| s3UploadEventHandler.addEventSource( | |
| new S3EventSource(this.bucket, { | |
| events: [s3.EventType.OBJECT_CREATED], | |
| filters: [{ prefix: "public/" }], | |
| }), | |
| ); |
✨ Codu Pull Request 💻
Pull Request details
Any Breaking changes