Skip to content
Quest 08Advanced14 min

Thumbnails on Upload

When a new sprite lands in the bucket, a function should create a thumbnail. S3 event notifications do exactly that, and they hide one of the most expensive bugs in serverless: a function that triggers itself forever.

What you'll learn

  • bucket.addEventNotification subscribes a function to events such as OBJECT_CREATED. CDK wires the invoke permission for you.
  • If a function writes into the same bucket it listens to, filter by prefix and suffix so its own output can't trigger it.
  • Behind the scenes CDK uses a custom resource to set the bucket's notification configuration.

Section 01

Subscribe a function

An event notification has an event type, a destination and optional filters. The destination classes live in aws-s3-notifications: LambdaDestination, SqsDestination and SnsDestination.

Thumbnail on upload
import * as s3n from "aws-cdk-lib/aws-s3-notifications";

bucket.addEventNotification(
  s3.EventType.OBJECT_CREATED,
  new s3n.LambdaDestination(processorFn),
  { prefix: "sprites/originals/", suffix: ".png" },
);

Section 02

The infinite loop

The processor reads sprites/originals/0252-treecko.png and writes sprites/thumbs/0252-treecko.png into the same bucket. If the notification has no filter, that new object is also an OBJECT_CREATED event, which invokes the function, which writes another object. Each invocation triggers the next, one after another, and Lambda's recursive-loop detection may eventually cut it off, but you have already paid for every invocation and every object written.

Two defences, both cheap: use filters so the function only sees inputs, or write outputs to a different bucket. The starter in the exercise has the loop; the checker tests which keys your filter would match, the way S3 would.

Section 03

A hidden custom resource

CloudFormation can only declare a bucket's notification configuration on the bucket itself, which would make the bucket depend on the function and the function's permission depend on the bucket. CDK avoids that cycle with a custom resource, Custom::S3BucketNotifications, backed by a small Lambda function. You will see it, and its role, in the template.

If you already manage the bucket's notifications in the console or another stack, importing the bucket and adding a notification can overwrite them. Know who owns the configuration.

Practice

Your turn

ProcessorFn makes a thumbnail for every new sprite and writes it back to sprites/thumbs/ in the same bucket. As written, each thumbnail triggers the function again. Trigger it only for PNG files under sprites/originals/.

OBJECT_CREATED notification with prefix: "sprites/originals/" and suffix: ".png".
Tab inserts two spaces. Press Escape, then Tab, to leave the editor. Control or Command plus Enter runs the code.
Simulated — no AWS calls

The simulator runs the constructs this course teaches, plus simple loops and helper methods. Other CDK code may not be recognised.

Check

Check your understanding

Quick Quiz · one question, as many tries as you need.

A function writes thumbnails to sprites/thumbs/ in the same bucket that triggers it. Which setup avoids a loop and still processes new PNGs?

Choose one answer.