CloudConsole

IAM policy: Lambda reading from an SQS queue

Updated 17 November 2026 · How we write and check guides

Short answer

The function's execution role needs sqs:ReceiveMessage, sqs:DeleteMessage and sqs:GetQueueAttributes on the queue ARN, plus CloudWatch Logs permissions. If the queue is encrypted with a customer managed KMS key, add kms:Decrypt on that key.

Policy diagram: 3 statement(s); first Allow sqs:ReceiveMessage, sqs:DeleteMessage… on sqs: orders-queue
The policy on this page drawn statement by statement: who it applies to, the actions, the resources and any condition.

The policy

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ConsumeQueue",
      "Effect": "Allow",
      "Action": [
        "sqs:ReceiveMessage",
        "sqs:DeleteMessage",
        "sqs:GetQueueAttributes"
      ],
      "Resource": "arn:aws:sqs:eu-west-2:123456789012:orders-queue"
    },
    {
      "Sid": "DecryptQueueMessages",
      "Effect": "Allow",
      "Action": "kms:Decrypt",
      "Resource": "arn:aws:kms:eu-west-2:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab"
    },
    {
      "Sid": "WriteLogs",
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:eu-west-2:123456789012:log-group:/aws/lambda/orders-worker:*"
    }
  ]
}

Why these three SQS actions

With an SQS trigger, your code never calls SQS itself. Lambda's event source mapping polls the queue using the function's execution role, invokes your function with a batch, and deletes the messages when the invocation succeeds.

  • sqs:ReceiveMessage: the long-poll that fetches batches.
  • sqs:DeleteMessage: removes processed messages. Without it, every message reappears after the visibility timeout and is processed again.
  • sqs:GetQueueAttributes: Lambda reads the queue's settings, such as the visibility timeout, when you create the mapping.

This is the same set as the managed policy AWSLambdaSQSQueueExecutionRole, but limited to one queue.

Create the trigger

aws iam put-role-policy --role-name orders-worker-lambda \
  --policy-name consume-orders-queue --policy-document file://sqs.json

aws lambda create-event-source-mapping --function-name orders-worker \
  --event-source-arn arn:aws:sqs:eu-west-2:123456789012:orders-queue \
  --batch-size 10 --function-response-types ReportBatchItemFailures

If the role is missing a permission, create-event-source-mapping fails straight away with an error saying the execution role does not have permission to call ReceiveMessage (or GetQueueAttributes) on SQS. Fix the policy and run it again.

Variations

  • SSE-SQS (SQS-managed encryption) needs no KMS permission; drop the second statement.
  • FIFO queues use the same three actions; the ARN ends in .fifo.
  • The function also sends messages (for example to the next queue): add sqs:SendMessage on that queue's ARN, and kms:GenerateDataKey if it uses a customer managed key.
  • Queue in another account: the queue's access policy must also allow the role's ARN to receive, delete and get attributes.

Watch it work

aws lambda list-event-source-mappings --function-name orders-worker \
  --query 'EventSourceMappings[].[UUID,State,LastProcessingResult]' --output table

aws sqs get-queue-attributes --queue-url https://sqs.eu-west-2.amazonaws.com/123456789012/orders-queue \
  --attribute-names ApproximateNumberOfMessages ApproximateNumberOfMessagesNotVisible

A mapping stuck in a failed state with a permissions message in LastProcessingResult means the role changed after the trigger was created.

Batch failures are best handled with partial batch responses (ReportBatchItemFailures), which needs no extra permission and avoids reprocessing the messages that already succeeded.

Common mistakes

  • Adding the permissions to the queue policy only. The poller uses the execution role, so the role needs them; the queue policy only matters across accounts.
  • Forgetting the dead-letter queue setup. The DLQ is configured on the source queue's redrive policy and needs no Lambda permission, but a queue without one retries a poison message until it expires. See redrive a DLQ.
  • Granting sqs:*: it includes sqs:PurgeQueue and sqs:DeleteQueue. Neither is needed by a consumer.

Questions

What permissions does Lambda need to read from SQS?

sqs:ReceiveMessage, sqs:DeleteMessage and sqs:GetQueueAttributes on the queue, in the function's execution role, plus kms:Decrypt if the queue uses a customer managed key.

Does the SQS queue policy need to allow Lambda?

Not in the same account. The event source mapping uses the execution role. A queue policy is only needed when the queue is in a different account.

When this happens away from your desk

A consumer that stopped deleting messages shows up as a queue that only ever grows. Watching queue depth and the age of the oldest message from a phone catches it before customers do.

About the app → · Download on the App Store

Sources