0
0
AWScloud~5 mins

Event triggers for Lambda in AWS - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Event triggers for Lambda
O(n)
Understanding Time 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.

Scenario Under Consideration

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).

Identify Repeating Operations

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.
How Execution Grows With Input

As more objects are created, the total processing work grows linearly with the number of objects.

Input Size (n)Approx. Api Calls/Operations
1010 processObject calls
100100 processObject calls
10001000 processObject calls

Pattern observation: The number of operations grows directly with the number of events.

Final Time Complexity

Time Complexity: O(n)

This means the total work grows in a straight line as the number of events increases.

Common Mistake

[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.

Interview Connect

Understanding how event triggers scale helps you design systems that handle growing workloads smoothly and predict costs better.

Self-Check

"What if the Lambda function batches multiple events together before processing? How would the time complexity change?"