This workshop content is over a year old and AWS Amplify has made a great deal of improvements since this content was originally written.

Instead of this content, please check out some of the more up-to-date Amplify content. One great example is the AWS Serverless Airline Booking project. Besides that, you can find lots more excellent AWS Amplify content on the AWS Amplify Community site.

If you would still like to review the content of this workshop, these instructions should still work, provided you use the specific version of AWS Amplify noted in the Prerequisites section.

Integrating Amazon Rekognition

It would be great if we could find images without having to manually tag them with descriptions of their contents. Luckily, adding this feature is pretty easy thanks to Amazon Rekognition. We can use the DetectLabels API – if we give it a photo, it will respond with a list of appropriate labels for the image. Perfect!

Amazon Rekognition’s DetectLabels Quick Summary

You pass the input image as base64-encoded image bytes or as a reference to an image in an Amazon S3 bucket. If you use the AWS CLI to call Amazon Rekognition operations, passing image bytes is not supported. The image must be either a PNG or JPEG formatted file.

For each object, scene, and concept the API returns one or more labels. Each label provides the object name, and the level of confidence that the image contains the object. For example, suppose the input image has a lighthouse, the sea, and a rock. The response includes all three labels, one for each object.

{Name: lighthouse, Confidence: 98.4629}
{Name: rock,Confidence: 79.2097}
{Name: sea,Confidence: 75.061}

Integrating Rekognition with the Photo Processor Lambda

Let’s add Amazon Rekognition integration in to our photo_processor lambda function.

Replace amplify/backend/function/S3Triggerxxxxxxx/src/index.js with the following version:

// photoalbums/amplify/backend/function/S3Triggerxxxxxxx/src/index.js

const AWS = require('aws-sdk');
const S3 = new AWS.S3({ signatureVersion: 'v4' });
const Rekognition = new AWS.Rekognition();
const DynamoDBDocClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
const uuidv4 = require('uuid/v4');

Note: Sharp requires native extensions to be installed in a way that is compatible
with Amazon Linux (in order to run successfully in a Lambda execution environment).

If you're not working in Cloud9, you can follow the instructions on how to install the module and native dependencies.
const Sharp = require('sharp');

// We'll expect these environment variables to be defined when the Lambda function is deployed
const THUMBNAIL_WIDTH = parseInt(process.env.THUMBNAIL_WIDTH, 10);
const THUMBNAIL_HEIGHT = parseInt(process.env.THUMBNAIL_HEIGHT, 10);

async function getLabelNames(bucketName, key) {
  let params = {
    Image: {
      S3Object: {
        Bucket: bucketName, 
        Name: key
    MaxLabels: 50, 
    MinConfidence: 70
  const detectionResult = await Rekognition.detectLabels(params).promise();
  const labelNames = => l.Name.toLowerCase()); 
  return labelNames;

function storePhotoInfo(item) {
    const params = {
        Item: item,
    return DynamoDBDocClient.put(params).promise();

async function getMetadata(bucketName, key) {
    const headResult = await S3.headObject({Bucket: bucketName, Key: key }).promise();
    return headResult.Metadata;

function thumbnailKey(filename) {
    return `public/resized/${filename}`;

function fullsizeKey(filename) {
    return `public/${filename}`;

function makeThumbnail(photo) {
    return Sharp(photo).resize(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT).toBuffer();

async function resize(bucketName, key) {
    const originalPhoto = (await S3.getObject({ Bucket: bucketName, Key: key }).promise()).Body;
    const originalPhotoName = key.replace('uploads/', '');
    const originalPhotoDimensions = await Sharp(originalPhoto).metadata();

    const thumbnail = await makeThumbnail(originalPhoto);

    await Promise.all([
            Body: thumbnail,
            Bucket: bucketName,
            Key: thumbnailKey(originalPhotoName),

            Bucket: bucketName,
            CopySource: bucketName + '/' + key,
            Key: fullsizeKey(originalPhotoName),

    await S3.deleteObject({
        Bucket: bucketName,
        Key: key

    return {
        photoId: originalPhotoName,
        thumbnail: {
            key: thumbnailKey(originalPhotoName),
            width: THUMBNAIL_WIDTH,
            height: THUMBNAIL_HEIGHT

        fullsize: {
            key: fullsizeKey(originalPhotoName),
            width: originalPhotoDimensions.width,
            height: originalPhotoDimensions.height

async function processRecord(record) {
    const bucketName =;
    const key = record.s3.object.key;
    if (key.indexOf('uploads') != 0) return;
    const metadata = await getMetadata(bucketName, key);
    const sizes = await resize(bucketName, key);   
    const labelNames = await getLabelNames(bucketName, sizes.fullsize.key);
    const id = uuidv4();
    const item = {
        id: id,
        owner: metadata.owner,
        labels: labelNames,
        photoAlbumId: metadata.albumid,
        bucket: bucketName,
        thumbnail: sizes.thumbnail,
        fullsize: sizes.fullsize,
        createdAt: new Date().getTime()
    await storePhotoInfo(item);

exports.handler = async (event, context, callback) => {
    try {
        callback(null, { status: 'Photo Processed' });
    catch (err) {

What we changed

  • Created an instance of AWS.Rekognition to interact with the Amazon Rekognition API

  • Added the getLabelNames function to use Rekognition.detectLabels to return a list of appropriate labels for a given photo on S3

  • Updated the processPrcord function to use the getLabelNames function to get labels for the photo and include them in the item record it persists to DynamoDB

Our Photo Processor code now uses Amazon Rekognition’s detectLabels API. But because we already added permissions for this action in the previous section, we won’t need to update the CloudFormation template again.

Re-deploying the Photo Processor Lambda

From the photoalbums directory, run: amplify push to deploy an updated version of the Photo Processor function.

After the deploy finishes, try adding a new photo to an album. Then, go look at its row in the PhotoTable in DynamoDB and see if you see a labels property for the new upload. Hopefully you see some relevant labels for the photo!