Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda