Event-driven workers
When a request shouldn’t block on slow work, you publish an event and let workers drain it. This is the fan-out pattern: an API Lambda publishes to SNS, which fans into SQS queues, each drained by a worker Lambda.
Build it
Section titled “Build it”- Wire Internet → API Gateway → Lambda (the producer) — see the serverless API guide for that base.
- Add an SNS topic and wire the producer Lambda’s downstream socket to it.
- Add an SQS queue and subscribe it to the SNS topic (the subscription is generated for you).
- Add a worker Lambda and wire the SQS queue to its event-source socket — this emits an
aws_lambda_event_source_mapping, so the worker is invoked per batch. - Optionally add an EventBridge rule for scheduled (cron) invocations.
Why the sockets matter
Section titled “Why the sockets matter”The SQS → worker connection generates the event source mapping automatically — you never hand-write the polling wiring. And because the queue is wired to a specific worker, adding a second queue and a second worker stays unambiguous.
resource "aws_sns_topic" "events" { name = "events" }
resource "aws_sqs_queue" "work" { name = "work" }
resource "aws_sns_topic_subscription" "fanout" { topic_arn = aws_sns_topic.events.arn protocol = "sqs" endpoint = aws_sqs_queue.work.arn}
resource "aws_lambda_event_source_mapping" "drain" { event_source_arn = aws_sqs_queue.work.arn function_name = aws_lambda_function.worker.arn batch_size = 10}- Simulate the fan-out under load to size the worker concurrency.
- See the messaging nodes in the node reference.