Event-driven architecture essentials

Serverless Applications with AWS Lambda

Claudio Canales

Senior DevOps Engineer

Recap: one Lambda mental model

  • Event source: what triggers Lambda.
  • Event payload: JSON describing what happened.
  • Handler: code that processes it.
  • Outcome: response or side effect.
  • Logs: where you debug issues.

Lambda execution flow

Serverless Applications with AWS Lambda

Events are messages

  • An event describes something that happened.
  • It is usually a JSON message with metadata.
  • Your handler extracts fields and takes action.

Doorbell analogy for events

Serverless Applications with AWS Lambda

Common event sources in AWS

  • Amazon S3 triggers events on uploads.
  • Amazon SQS is a managed queue that delivers messages.
  • DynamoDB Streams produces a change log.
  • Each produces a different JSON payload.

Event sources: S3 vs SQS vs Streams

Serverless Applications with AWS Lambda

Synchronous vs asynchronous invocation

  • Synchronous: caller waits for a result.
  • Asynchronous: caller gets an acknowledgment; event is processed later.

Sync vs async analogy

Serverless Applications with AWS Lambda

Who sees the error?

  • Synchronous: the caller receives the error.
  • Asynchronous: Lambda retries and can route failures.
  • You debug through logs and failure targets.

Sync vs async error path

Serverless Applications with AWS Lambda

Payloads are contracts

  • Payloads are contracts: the sender decides the shape.
  • Your handler must know where to find the fields.
  • Extract what you need, validate it, process it, and log.

Payload contract and parsing workflow

Serverless Applications with AWS Lambda

Example payload: SQS message event

Annotated SQS event payload

  • Records is the batch.
  • body is a string.
  • messageId helps you trace and deduplicate.
Serverless Applications with AWS Lambda

Walkthrough: iterate through Records

def lambda_handler(event, context):
    records = event.get("Records", [])
    for record in records:
        body = record.get("body", "")
        print("BODY:", body)
    return {"statusCode": 200}
  • Read Records with a safe default.
  • Loop through each record and extract the body.
  • Log what matters, then return.

Loop through Records

Serverless Applications with AWS Lambda

Walkthrough: parse JSON body safely

import json
def lambda_handler(event, context):
    records = event.get("Records") or [{}]
    body = records[0].get("body", "{}")
    payload = json.loads(body)
    oid = payload.get("order_id")
    return json.dumps({"order_id": oid})
  • The body is a string; parse it with json.loads.
  • Read with a safe default like "{}".
  • Extract order_id and return JSON with json.dumps.

Body parsing flow

Serverless Applications with AWS Lambda

Defensive parsing checklist

Defensive parsing checklist

  • Log the event shape once when you are learning a new source.
  • Use .get() and defaults for optional fields.
  • Validate required fields and return clear errors.
  • Handle empty batches and missing keys.
Serverless Applications with AWS Lambda

Design for retries and duplicates

  • Many event sources use at-least-once delivery.
  • Duplicates can happen.
  • Design idempotent handlers so processing twice stays correct.

Retries and duplicates

Serverless Applications with AWS Lambda

Idempotency in practice

  • Choose a unique key, like messageId or order_id.
  • Store each processed key in DynamoDB.
  • Check if the key exists; if it does, skip the work.

Idempotency decision flow

Serverless Applications with AWS Lambda

Key takeaways

  • Events arrive as JSON from AWS services.
  • Invocation mode changes where errors appear.
  • Payloads vary, so parse and validate defensively.
  • Assume retries and duplicates.

Key takeaways pipeline

Serverless Applications with AWS Lambda

Let's practice!

Serverless Applications with AWS Lambda

Preparing Video For Download...