Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
How do I include an image (jpg) with a lambda function?

I'm trying to write a lambda that puts a watermark on an image, then saves the result to S3. I'm using the Sharp library to do so. I'm deploying with Serverless Framework.

I'm unsure how to 'include' the image with the lambda. I'll be watermarking millions of images, so I want to avoid having to 'read' the image from S3 every time and I'd like to just have it be included in the lambda package.

let overlayResp;
    try {
        const overlayImg = await Sharp(sourceImage.Body)
        .composite([{input: './logos/white.png', gravity: 'southeast' }])
        .toFormat('jpeg').toBuffer();
        overlayResp = await s3.putObject({
            Bucket: IMAGE_BUCKET,
            Body: overlayImg,
            Key: `${filename.replace('original', 'overlay')}`
        }).promise();

    } catch (err) {
        console.error('Overlay err :>> ', err);  //TODO: DLQ
    }

I've tried including the image in the 'logos' folder alongside my processNewImage.js folder like so: filestructure

Using the above code. It errors out saying 'Input file is missing'.

I've tried an to import the image const WHITE = require('/logos/white.png'); and changing the composite line to .composite([{input: WHITE, gravity: 'southeast' }]) and the result is that the code doesn't even run (Cannot find module '/logos/white.png).

It feels like the lambda package doesn't have the image included. How can I include it and/or make a reference to it?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The path you are using (/logos/white.png) is an absolute path and that means that your code is "looking" in the wrong place.

I am not 100% sure but one of the following two options should solve your issue:

  1. Use a relative path (./logos/white.png)
  2. Use the LAMBDA_TASK_ROOT environment variable:
let path = `${process.env.LAMBDA_TASK_ROOT}/logos/white.png`

Documentation: https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime

Another option is split the application code and the assets like the image up. The image would go into a Lambda layer which you would be able find in another directory on the Lambdas disk:

https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!