Event Sources and Triggers
Serverless functions don't run continuously — they respond to events. Understanding the different event sources helps you design systems where the right function runs at the right time, triggered by the right thing.
Common Event Sources
HTTP triggers are the most familiar. An API Gateway receives an HTTP request and invokes your function. The event contains the request method, headers, body, and path parameters.
Queue triggers fire when messages arrive in a queue like AWS SQS or Google Pub/Sub. Great for decoupling systems and handling work asynchronously.
Storage triggers respond to file operations. Upload an image to S3, and a function automatically processes it. Delete a file, and another function cleans up related data.
Database triggers react to data changes. A new row in DynamoDB can trigger a function to update a search index or send a notification.
Schedule triggers run functions on a schedule, like cron jobs. Generate daily reports, clean up old data, or send reminder emails at specific times.
Stream triggers process real-time data from services like Kinesis or Kafka. Each batch of records triggers a function invocation.
Matching Triggers to Use Cases
The power of serverless comes from connecting the right trigger to the right function:
User uploads image → S3 event → Lambda resizes image
Order placed → SQS message → Lambda sends confirmation email
Every hour → CloudWatch schedule → Lambda generates report
Database change → DynamoDB stream → Lambda updates search index
API request → API Gateway → Lambda returns data
Each trigger type has different characteristics. HTTP triggers need fast responses. Queue triggers can take longer since users aren't waiting. Schedule triggers run regardless of user activity.
Event Payload Structure
Each event source provides different data. An HTTP event includes request details:
{
"httpMethod": "POST",
"path": "/users",
"headers": { "Content-Type": "application/json" },
"body": "{\"name\": \"Alice\"}"
}
An S3 event includes file information:
{
"Records": [{
"s3": {
"bucket": { "name": "my-bucket" },
"object": { "key": "uploads/photo.jpg" }
}
}]
}
Understanding these structures helps you write functions that correctly extract the data they need.