Today I Learned

Fetching Images with Lambda

October 06, 2019

Online Images

When creating dynamic digital ads you often need to account for frequently changing content. Depending on the complexity of the client this could come from an API or URL. More often than not it’s just a URL. Unfortunately linking to a third party URL in a digital ads creates a whole new set of challenges.

  • Will the image be available?
  • Do they use HTTPS?
  • What size is the file?

To mitigate these issues I prefer to retrieve client images before referencing them in an ad. We used to do this with a simple PHP script that retrieved the image from URL and saved it to the local server. Moving to AWS Lambda and static site hosting changes this process considerably.

A New Hope

Migrating this process to AWS Lambda entails fetching the file from a specified URL and then saving it to S3. The following Nodejs script takes care of fetching the file:

"use strict";

const AWS = require("aws-sdk");
const Jimp = require("jimp");
const s3 = new AWS.S3();
const imageType = "image/jpeg";
const bucket = 'samplebucket';

exports.handler = async (event, context) => {
    let objectKey = 'sampleimage.jpeg';
    const image = await Jimp.read('https://www.sample.com/images/sampleimage.jpg/');
    const buffer = await image.getBufferAsync(imageType);

    return s3.putObject({
        Bucket: bucket,
        Key: objectKey,
        Body: buffer,
        ContentType: imageType
    }).promise();

Since this script requires the Jimp library you need to build it locally with NPM then zip all files and import to the Lambda console. Once this was done I used a CloudWatch rule to trigger the Lambda function at set intervals. If you need to have an HTML page or script link to the image in S3 just be sure to set your S3 permissions correctly.

Final Thoughts

Serverless functions with AWS Lambda are quickly becoming one of my favorite tools. Its quick, easy, scalable and almost free :-)


Ryan Kovalchick

Written by Ryan Kovalchick who lives and works in Allentown Pennsylvania creating, fixing and building.