AWS Lambda
SNS
serverless architecture
event-driven computing
cloud computing

When Lambda is invoked by SNS, will there always be just 1 record?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Lambda Invocations by SNS: Examining the Record Count

When working with AWS services, especially AWS Lambda and Amazon SNS (Simple Notification Service), it is crucial to understand how these components interact, specifically regarding the number of records processed during an invocation. This article delves into whether Lambda functions invoked by SNS always receive just one record and provides detailed insights into the mechanisms at play.

Understanding SNS and Lambda Interactions

Amazon SNS is a fully managed pub/sub messaging service that enables you to decouple microservices, distributed systems, and serverless applications. When SNS publishes a message, it can be received by multiple subscribers, one of which could be an AWS Lambda function.

AWS Lambda is a compute service that lets you run code without provisioning or managing servers. Lambda executes your code only when needed, scaling automatically with the size of the workload.

When you set up an SNS topic and subscribe a Lambda function to it, the dynamics of invocation depend on how the SNS message payload is structured and delivered.

Single SNS Message = Single Lambda Record

In a typical configuration, when SNS sends a message to Lambda, each message results in a separate invocation of the Lambda function. Thus, for every SNS message pushed to the Lambda function, there is usually one record in the event payload. This behavior is mostly considered standard in SNS-Lambda integrations:

  1. SNS Topic Message: Each published message to an SNS topic is uniquely delivered to the Lambda function.
  2. Lambda Event Structure: The event passed to the Lambda function contains details about the message attributes and payload.
  3. Single Record Per Invocation: Each Lambda invocation triggered by SNS contains exactly one record from the SNS message payload.

Here is an example structure of the event received by a Lambda function when invoked via SNS:

  • Batch Size Settings: Unlike SQS (Simple Queue Service) setup with Lambda, where you can adjust batch sizes, SNS directly triggers the function with each message.
  • Concurrency Issues: High throughput SNS topics may result in concurrent Lambda invocations, one for each message, which should be managed accordingly.
  • Error Handling: SNS has limited built-in retries for Lambda if the function fails; beyond that, manual intervention or additional architecture like DLQs (Dead Letter Queues) might be needed.

Course illustration
Course illustration

All Rights Reserved.