Event triggers for Lambda in AWS - Time & Space Complexity
When using event triggers for AWS Lambda, it's important to understand how the number of events affects the work Lambda does.
We want to know how the number of triggered events changes the total processing time.
Analyze the time complexity of this Lambda event trigger setup.
// Example: S3 bucket triggers Lambda on object creation
const aws = require('aws-sdk');
const s3 = new aws.S3();
exports.handler = async (event) => {
for (const record of event.Records) {
const bucket = record.s3.bucket.name;
const key = record.s3.object.key;
await processObject(bucket, key);
}
};
async function processObject(bucket, key) {
// Process the S3 object
}
This code, triggered by S3 object creation events, processes each object in the received event records (S3 batches multiple events into invocations).
Look at what repeats when the Lambda runs:
- Primary operation: Processing each S3 object triggered by the event.
- How many times: Once for every object creation event received.
As more objects are created, the total processing work grows linearly with the number of objects.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 processObject calls |
| 100 | 100 processObject calls |
| 1000 | 1000 processObject calls |
Pattern observation: The number of operations grows directly with the number of events.
Time Complexity: O(n)
This means the total work grows in a straight line as the number of events increases.
[X] Wrong: "The Lambda runs once no matter how many events happen at the same time."
[OK] Correct: While S3 may batch multiple events into a single invocation, more events still mean more total work (more processObject calls) and typically more Lambda executions.
Understanding how event triggers scale helps you design systems that handle growing workloads smoothly and predict costs better.
"What if the Lambda function batches multiple events together before processing? How would the time complexity change?"
Practice
Solution
Step 1: Understand Lambda triggers
Lambda functions can be triggered by events from various AWS services.Step 2: Identify common event sources
Amazon S3 triggers Lambda when a new object is uploaded to a bucket.Final Answer:
Amazon S3 object upload event -> Option CQuick Check:
S3 upload triggers Lambda = A [OK]
- Confusing EC2 events as Lambda triggers
- Thinking CloudTrail logs directly trigger Lambda
- Assuming RDS backups trigger Lambda
Solution
Step 1: Know how Lambda connects to SQS
Lambda uses event source mappings to poll messages from SQS queues.Step 2: Identify correct configuration
Creating an event source mapping links the SQS queue to Lambda for automatic invocation.Final Answer:
Create an event source mapping between the SQS queue and Lambda -> Option DQuick Check:
SQS triggers Lambda via event source mapping = D [OK]
- Confusing S3 triggers with SQS triggers
- Attaching IAM roles to SQS instead of Lambda
- Using CloudWatch Events incorrectly for SQS
{
"EventSourceArn": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
"FunctionName": "ProcessQueueMessages",
"Enabled": true
}What happens when a new message arrives in the SQS queue?
Solution
Step 1: Understand event source mapping
The snippet shows an event source mapping linking an SQS queue to a Lambda function.Step 2: Effect of Enabled=true
With Enabled set to true, Lambda polls the queue and invokes the function on new messages.Final Answer:
The Lambda function ProcessQueueMessages is invoked automatically -> Option AQuick Check:
Enabled event source mapping triggers Lambda = C [OK]
- Assuming manual invocation is needed
- Thinking messages get deleted without processing
- Believing configuration is incomplete without batch size
Solution
Step 1: Check DynamoDB stream status
For Lambda to trigger, the DynamoDB stream must be enabled on the table.Step 2: Confirm stream is active
If the stream is disabled or not enabled, no events are generated to invoke Lambda.Final Answer:
The DynamoDB stream is disabled or not enabled -> Option AQuick Check:
DynamoDB stream must be enabled for Lambda trigger = A [OK]
- Ignoring stream enablement status
- Assuming empty table stops triggers
- Blaming Lambda code without checking stream
Solution
Step 1: Identify scheduled triggers for Lambda
To run Lambda on a schedule without external events, use CloudWatch Events (EventBridge) rules.Step 2: Use cron expression in EventBridge
Create a rule with a cron expression for 6 AM UTC daily to trigger the Lambda function automatically.Final Answer:
Create a CloudWatch Events (EventBridge) rule with a cron expression to trigger Lambda -> Option BQuick Check:
Scheduled Lambda uses EventBridge cron rule = B [OK]
- Trying to trigger Lambda via S3 or SQS for schedule
- Assuming DynamoDB Streams can schedule events
- Not using cron expressions for timing
Ready to prove it?
Pass the test and this pattern moves unknown
