You already know that a Lambda function executes when an event occurs (subchapter 14.1). But which events? Who "calls" the function? That's defined by triggers: the sources that invoke your Lambda. In this subchapter, we'll look at the most important ones and how they open the door to a ton of architectures.

What is a trigger

A trigger is what makes your function execute. You connect an event source to your Lambda, and every time that source generates an event, AWS automatically invokes your function, passing it the event data.

   Event source   ──(event)──►   Lambda executes
   (API, S3, queue...)

A single function can have one or several triggers. Let's look at the four most common ones.

  1. API Gateway: turn your Lambda into a web API

API Gateway allows your Lambda to respond to HTTP requests, just like a normal web API. It's the way to create serverless backends and APIs.

User / mobile app
      │ HTTP (GET /products)
      ▼
  API Gateway  ──invokes──►  Lambda  ──► queries data ──► responds JSON

Real-world example: a mobile app needs a backend that returns the list of products. Instead of setting up an always-on server (EC2), you use API Gateway + Lambda: when the app makes a GET /products, API Gateway invokes your Lambda, which returns the data. If no one uses the app at night, you pay nothing. If thousands use it during the day, it scales by itself.

This is one of the most popular serverless patterns, and the basis of the serverless blog project we'll see in Chapter 33.

  1. S3: react to uploaded files

Remember S3, the object storage (Chapter 5). You can configure a bucket so that every time a file is uploaded (or deleted), it triggers a Lambda.

User uploads a photo to S3
      │
      ▼
  S3 triggers ──► Lambda ──► generates a thumbnail / analyzes it / processes it

Classic example: a website where users upload photos. When an image arrives in the S3 bucket, a Lambda is triggered that automatically creates a thumbnail (small version) and saves it. The user doesn't wait: the photo is processed "by itself" in the background. Other uses: converting formats, extracting text, scanning for viruses, etc.

  1. DynamoDB Streams: react to changes in the database

Remember DynamoDB (subchapter 8.3). A DynamoDB Stream is a "change log" of the table: every time an item is created, modified, or deleted, it can trigger a Lambda with the details of the change.

An order is inserted into the DynamoDB table
      │
      ▼
  DynamoDB Stream triggers ──► Lambda ──► sends confirmation email,
                                          updates statistics...

Example: in a store, when a new order is inserted into the Orders table, the stream triggers a Lambda that sends a confirmation email to the customer and updates a sales dashboard. The database "notifies" of its own changes, and the logic reacts automatically.

  1. SQS: process messages from a queue

Remember queues (we'll see them in depth in Chapter 15). An SQS queue accumulates messages (pending tasks), and a Lambda can process them one by one or in batches.

Tasks accumulate in the SQS queue
   [task] [task] [task] [task]
      │
      ▼
  SQS triggers ──► Lambda ──► processes each task (at its own pace)

Example: a store receives thousands of orders in a rush (Black Friday). Instead of processing them all at once and getting overwhelmed, it puts them in an SQS queue. A Lambda takes them out and processes them at a sustainable pace. The queue acts as a "buffer" (we'll see this as decoupling in Chapter 15).

Trigger summary table

Trigger Fires when... Typical use case
API Gateway An HTTP request arrives Serverless APIs and backends
S3 A file is uploaded/deleted Process images, files
DynamoDB Streams A table item changes React to changes (emails, stats)
SQS There are messages in the queue Process background tasks

And there are many more: CloudWatch (scheduled tasks like "every night at 2:00"), SNS (notifications), EventBridge (system events, Chapter 15), Kinesis (data streaming, Chapter 29), etc.

The powerful idea: event-driven architectures

The important thing about triggers is the pattern they enable: event-driven architectures. Instead of one big monolithic program that does everything, you have small functions that react to events:

Upload photo → Lambda creates thumbnail
New order → Lambda sends email
Message in queue → Lambda processes the task
HTTP request arrives → Lambda responds

Each piece is small, independent, and runs only when needed. This makes systems more flexible, decoupled, and cheaper. We'll dive deeper into these patterns in Chapters 15 and 28.

What you should remember

  • A trigger is the source that invokes your Lambda when an event occurs; you connect the source and AWS calls your function automatically.
  • API Gateway: HTTP requests → your Lambda acts as a serverless API/backend.
  • S3: a file is uploaded → process images, convert formats, scan, etc.
  • DynamoDB Streams: a change in the database → react (emails, statistics).
  • SQS: messages in a queue → process background tasks at a sustainable pace.
  • Triggers enable event-driven architectures: small functions that react to events, more flexible and cheaper than a monolith.

In the next subchapter, we'll look at a key practical aspect: how to manage your function's dependencies (libraries) and reuse code with Layers.

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved