← Week 1: Messaging: SQS, SNS, EventBridge

Day 6: Lambda Integration with SQS

Phase 5 · Aug 17, 2026

← Week 1: Messaging: SQS, SNS, EventBridge

Agenda (2–3 hours)

  • Read (45 min): Lambda event source mapping documentation; Lambda SQS trigger documentation; Lambda Powertools for Rust README
  • Study (45 min): How does the Lambda service poll SQS on your behalf? What is "batch item failure reporting" and why does it matter?
  • Practice (45 min): Create a Lambda function in Rust (using lambda_runtime) triggered by an SQS queue; implement batch item failure reporting
  • Challenge (30 min): A Lambda batch of 10 messages processes 9 successfully. Without batch item failure reporting, what happens to all 10 messages?
← Week 1: Messaging: SQS, SNS, EventBridge

Lambda + SQS Event Source Mapping

Lambda's event source mapping (ESM) polls SQS on your behalf:

SQS Queue → Lambda Service Polling → Lambda Function (batch of 1-10 messages)

ESM manages:

  • Polling with long poll (saves API costs)
  • Scaling: adds Lambda instances as queue depth grows
  • DLQ routing: after maxReceiveCount failures
// Cargo.toml
[dependencies]
lambda_runtime = "0.11"
aws_lambda_events = { version = "0.15", features = ["sqs"] }
tokio = { version = "1", features = ["full"] }
← Week 1: Messaging: SQS, SNS, EventBridge

Lambda Handler with Batch Processing

use lambda_runtime::{service_fn, Error, LambdaEvent};
use aws_lambda_events::event::sqs::{SqsEvent, SqsBatchResponse, SqsBatchItemFailure};

async fn handler(event: LambdaEvent<SqsEvent>) -> Result<SqsBatchResponse, Error> {
    let mut failures = Vec::new();

    for record in &event.payload.records {
        let message_id = record.message_id.clone().unwrap_or_default();
        match process_record(record).await {
            Ok(()) => {}
            Err(e) => {
                error!(%e, %message_id, "record failed");
                failures.push(SqsBatchItemFailure { item_identifier: message_id });
            }
        }
    }

    Ok(SqsBatchResponse { batch_item_failures: failures })
}
← Week 1: Messaging: SQS, SNS, EventBridge

Batch Item Failure Reporting

Without ReportBatchItemFailures:

  • If any message fails, all 10 messages in the batch are retried
  • 9 messages processed successfully are re-processed → waste + potential duplicates

With ReportBatchItemFailures:

  • Only failed messages are retried
  • Successfully processed messages are deleted from the queue

Configuration:

# CloudFormation
EventSourceMapping:
  FunctionResponseTypes:
    - ReportBatchItemFailures
← Week 1: Messaging: SQS, SNS, EventBridge

Concurrency and Throttling

Lambda ESM scaling:

  • Starts with 5 concurrent instances
  • Adds up to 300 additional instances per minute
  • Max: 1,000 concurrent instances per queue (default account limit)

If Lambda is throttled:

  • Messages remain in queue (not lost)
  • ESM backs off and retries
  • Visibility timeout must be > Lambda function timeout (otherwise messages become visible during execution)
← Week 1: Messaging: SQS, SNS, EventBridge

Key Takeaways

  • Lambda ESM handles polling, scaling, and DLQ routing automatically
  • Always enable ReportBatchItemFailures — without it, any failure retries the entire batch
  • Set Lambda timeout < SQS visibility timeout to prevent message double-delivery
  • Lambda concurrency scales with queue depth; set concurrency limits to protect downstream services

Tomorrow: Phase 5 Week 1 Challenge — event-driven pipeline.