0
0
AwsHow-ToBeginner · 4 min read

How to Trigger AWS Lambda from SQS Queue

To trigger a AWS Lambda function from an SQS queue, configure the Lambda as an event source for the SQS queue. This setup makes Lambda automatically poll the queue and invoke your function when messages arrive.
📐

Syntax

To connect an SQS queue to a Lambda function, you create an event source mapping. This mapping tells Lambda which queue to watch and which function to run.

  • EventSourceArn: The Amazon Resource Name (ARN) of the SQS queue.
  • FunctionName: The name or ARN of the Lambda function to invoke.
  • BatchSize: Number of messages Lambda reads at once (default 10).
  • Enabled: Whether the event source mapping is active.
bash
aws lambda create-event-source-mapping \
  --event-source-arn arn:aws:sqs:region:account-id:queue-name \
  --function-name my-lambda-function \
  --batch-size 10 \
  --enabled
💻

Example

This example shows how to create a Lambda function triggered by an SQS queue using AWS CLI. It assumes you have an existing Lambda function and SQS queue.

bash
aws lambda create-event-source-mapping \
  --event-source-arn arn:aws:sqs:us-east-1:123456789012:my-queue \
  --function-name myLambdaFunction \
  --batch-size 5 \
  --enabled
Output
{ "UUID": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "StartingPosition": "LATEST", "BatchSize": 5, "EventSourceArn": "arn:aws:sqs:us-east-1:123456789012:my-queue", "FunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:myLambdaFunction", "State": "Enabled" }
⚠️

Common Pitfalls

  • Missing permissions: Lambda needs permission to read from the SQS queue. Attach an IAM role with sqs:ReceiveMessage, sqs:DeleteMessage, and sqs:GetQueueAttributes actions.
  • Incorrect ARN: Using the wrong ARN for the queue or Lambda function causes failure.
  • Batch size too large: Setting batch size too high can delay processing or cause timeouts.
  • Lambda timeout too short: Ensure Lambda timeout is enough to process the batch of messages.
json
### Wrong: No permissions attached
{
  "Version": "2012-10-17",
  "Statement": []
}

### Right: IAM policy for Lambda to access SQS
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "sqs:ReceiveMessage",
        "sqs:DeleteMessage",
        "sqs:GetQueueAttributes"
      ],
      "Resource": "arn:aws:sqs:us-east-1:123456789012:my-queue"
    }
  ]
}
📊

Quick Reference

Remember these key points when triggering Lambda from SQS:

  • Set up event source mapping with correct ARNs.
  • Ensure Lambda has an execution role with SQS permissions.
  • Choose batch size based on processing time and message volume.
  • Monitor Lambda errors and dead-letter queues for failed messages.

Key Takeaways

Create an event source mapping to connect SQS to Lambda.
Lambda needs IAM permissions to read and delete messages from SQS.
Use appropriate batch size to balance throughput and processing time.
Check ARNs carefully to avoid configuration errors.
Monitor Lambda and SQS for errors and message failures.