Skip to content

Lesson 4 of 9 · 22m

Event Sources, Triggers, and Routing

How Lambda actually gets invoked — API Gateway, SQS, EventBridge, DynamoDB Streams, S3 — and how to choose the right edge for each workload.

A Lambda function is inert until something invokes it. Choosing that something is architecture, not plumbing. Every trigger changes delivery semantics, scaling behavior, retries, and the security boundary. Teams get hurt when they choose whichever trigger the console made easiest.

API Gateway is the synchronous edge

API Gateway invokes Lambda and waits for a response. It fits user-facing HTTP APIs, webhooks, and backend-for-frontend edges:

ApiFunction:
  Type: AWS::Serverless::Function
  Properties:
    Handler: api.handler
    Events:
      Api:
        Type: HttpApi
        Properties:
          Path: /orders
          Method: post

The key word is synchronous. Validate, authorize, write a command or enqueue work, and return. Do not pack report generation or third-party fanout into a request the user is waiting on. Also inspect a real event once — HTTP APIs and REST APIs differ in payload shape, authorizers, throttling, and cost.

EventBridge distributes facts

Use EventBridge when multiple systems care that something happened: OrderCreated, InvoicePaid, UserDeleted.

Rule:
  Type: AWS::Events::Rule
  Properties:
    EventBusName: orders-bus
    EventPattern:
      source: ["com.acme.orders"]
      detail-type: ["OrderCreated"]

A good event is a fact in the past tense, not a command. OrderCreated informs consumers. HandleOrder hides the recipient and turns the bus into RPC with worse debugging.

SQS buffers work and absorbs bursts

A queue explicitly decouples producer and consumer:

API Gateway -> ingest Lambda -> SQS -> worker Lambda -> downstream system

Use SQS when work must not be lost and downstream throughput must be controlled. Use EventBridge for distribution; use SQS for buffered processing. They overlap in convenience, not semantics.

Streams are ordered sequences

DynamoDB Streams and Kinesis are for projections, replication, analytics, and change-data-capture. They add shard ordering, batching, checkpointing, and iterator-age behavior:

Events:
  Stream:
    Type: DynamoDB
    Properties:
      Stream: !GetAtt OrdersTable.StreamSpecification.StreamArn
      BatchSize: 100
      BisectBatchOnError: true

Ordering is both the feature and the constraint. A bad record can block everything behind it unless failure handling is designed.

S3 notifications are convenient, not magic

S3 events work well for lightweight object processing such as thumbnails or metadata extraction. They also make loops easy: a function writing output into the same bucket and prefix can trigger itself forever. Use distinct destination buckets or prefix/suffix filters.

Field notes

  • Log a raw event once per source in development; schemas differ and guesses become parser bugs.
  • Include schemaVersion and stable detail-type names in event contracts.
  • Do not broadcast sensitive payloads widely merely because the bus is convenient.
  • Choose the semantic first: synchronous answer, distributed fact, buffered work, or ordered stream.

Was this lesson helpful?

Sign in to rate