AWS in 1 Min · Episode 75

AWS Lambda triggers and event sources (Part 2)

Triggers and event sources. Watch the one-minute video, then read the details.

David Gomez1:17 video · 3 min read

"You have to call a Lambda function yourself." You can, but that's the unusual case. Most functions are invoked by events: another AWS service notices that something happened and runs your code for you. Part one of this mini-series covered what a function is. This part is about how it gets called, because the way an event arrives decides how errors and retries behave.

A trigger connects an event source to a function: "when an object is created in this bucket, run this function". The source service pushes the event to Lambda as soon as it occurs.

There are three delivery models worth knowing.

Synchronous: the caller waits

With a synchronous invocation, the caller waits while the function runs and gets the result back. Amazon API Gateway and Lambda function URLs work this way: an HTTP request comes in, the function runs, the response goes back to the user.

If the function fails, the error goes straight back to the caller. Lambda doesn't retry for you; the client decides whether to try again.

Asynchronous: hand over and move on

With an asynchronous invocation, the source hands the event to Lambda and doesn't wait. Lambda places it on an internal queue and runs your function shortly after. Amazon S3 notifications, Amazon SNS and Amazon EventBridge (including scheduled rules) work this way.

Here Lambda handles errors: if your code fails, by default it tries two more times. Events that still fail are discarded unless you configure somewhere to keep them: a dead-letter queue or an on-failure destination. Set one up; otherwise failures disappear silently.

Event source mappings: Lambda polls for you

Queues and streams don't push. For Amazon SQS, Amazon Kinesis, DynamoDB Streams and Kafka, you create an event source mapping: a Lambda resource that reads from the source and invokes your function with batches of records.

Two settings shape it: the batch size (the most records per invocation) and the batching window (how long to wait to fill a batch). If the function returns an error, the batch is processed again, so one bad record can block a stream shard or keep coming back from a queue until you handle it.

Plan for duplicates

AWS documents that asynchronous events can occasionally be delivered more than once, and that event source mappings process each event at least once. Retries add more repeats. So write functions that are idempotent: running twice with the same event gives the same result as running once. Typical techniques are using the event's ID as a key and checking whether it was already handled, or writing with "put if not exists".

A second classic mistake is the recursive loop: a function triggered by uploads to a bucket writes its output to the same bucket and triggers itself. Write results to a different bucket or prefix.

Where AI fits

Triggers turn a model into an automatic step. A document lands in Amazon S3; the S3 trigger invokes a function; the function sends the text to a model on Amazon Bedrock and asks for a summary; the summary is saved as a new object or a database record. Nobody opens a console.

For that pipeline, apply everything above: save the summary to a different prefix so it doesn't trigger the function again, make the function idempotent because the event may repeat, set a timeout long enough for the model, and send failures to a dead-letter queue so a throttled model call isn't lost.

Quick recap

  • A trigger connects an event source to a function.
  • Events arrive three ways: synchronous (caller waits), asynchronous (queued, retried) or by event source mapping (Lambda polls, in batches).
  • Expect duplicates, and keep failed events in a dead-letter queue.
AWS in 1 Min #75 · 1:17

Need an AWS expert on your team?

I'm David, an AWS expert and fractional cloud consultant. I work alongside your team to run your cloud and infrastructure: architecture, security, cost and day-to-day operations. You get senior AWS experience when you need it, without the cost of a full-time hire.

  • Architecture reviews and a clear plan for what to fix first
  • Security hardening: IAM, networking, audits
  • Lower AWS bills without slowing your team down
  • Hands-on help with AI workloads on AWS