If you’re triggering a Lambda function from an SQS queue, you may hit this error during deployment:
Invalid request provided: Queue visibility timeout: 30 seconds is less than Function timeout: 300 seconds
This means your SQS queue’s visibility timeout is shorter than your Lambda function’s timeout. AWS blocks this configuration because it causes duplicate message processing. This guide explains why the error happens and how to fix the SQS visibility timeout in AWS Lambda correctly.
Why This Happens
When Lambda polls a message from SQS, that message becomes invisible to other consumers for the duration of the visibility timeout. If Lambda takes longer to process the message than the visibility timeout allows, SQS assumes processing failed and makes the message visible again. Another Lambda invocation picks it up, and now you have two invocations processing the same message.
This leads to:
- Duplicate processing (double charges, repeated emails, duplicate database writes)
- Data inconsistency across systems
- Wasted compute from redundant invocations (see How to Avoid Unexpected AWS Lambda Costs)
With the default SQS visibility timeout of 30 seconds and a Lambda timeout of 300 seconds, any invocation running longer than 30 seconds triggers a retry — even though the original is still running.
The Fix: Set Visibility Timeout to 6x Lambda Timeout
AWS recommends setting the queue’s visibility timeout to at least 6 times the Lambda function timeout, plus the MaximumBatchingWindowInSeconds value if you use batching. This accounts for the function’s execution time and allows room for Lambda to retry on throttling errors before the message becomes visible again.
For a Lambda function with a 300-second (5-minute) timeout:
- Minimum visibility timeout:
300 × 6 = 1800 seconds(30 minutes) - With a 5-second batching window:
(300 × 6) + 5 = 1805 seconds
The maximum visibility timeout for an SQS queue is 12 hours (43,200 seconds).
Configuring with AWS CLI
If your queue already exists, update the visibility timeout directly:
aws sqs set-queue-attributes \
--queue-url https://sqs.ap-southeast-1.amazonaws.com/123456789012/my-queue \
--attributes VisibilityTimeout=1800 \
--region ap-southeast-1
Verify the change:
aws sqs get-queue-attributes \
--queue-url https://sqs.ap-southeast-1.amazonaws.com/123456789012/my-queue \
--attribute-names VisibilityTimeout \
--region ap-southeast-1
You can also update this in the SQS console under Queue > Edit > Visibility timeout.
Configuring with CloudFormation
If you manage your infrastructure as code, define both the main queue and a dead letter queue (DLQ) in your CloudFormation template:
Resources:
MainQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: my-queue
VisibilityTimeout: 1800
RedrivePolicy:
deadLetterTargetArn: !GetAtt DeadLetterQueue.Arn
maxReceiveCount: 3
DeadLetterQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: my-queue-dlq
VisibilityTimeout: 60
MessageRetentionPeriod: 1209600
MyFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
# Lets Lambda read and delete SQS messages and write logs
- arn:aws:iam::aws:policy/service-role/AWSLambdaSQSQueueExecutionRole
MyFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: my-function
Handler: src/handler.lambda_handler
Runtime: python3.12
Timeout: 300
Role: !GetAtt MyFunctionRole.Arn
Code:
S3Bucket: your-artifact-bucket
S3Key: my-function.zip
SQSEventSource:
Type: AWS::Lambda::EventSourceMapping
Properties:
EventSourceArn: !GetAtt MainQueue.Arn
FunctionName: !Ref MyFunction
BatchSize: 10
FunctionResponseTypes:
- ReportBatchItemFailures
VisibilityTimeout: 1800— 6x the Lambda timeout of 300 secondsmaxReceiveCount: 3— after 3 failed attempts, the message moves to the DLQMessageRetentionPeriod: 1209600— keeps DLQ messages for 14 days before automatic deletionAWSLambdaSQSQueueExecutionRole— the AWS managed policy that lets the function receive and delete messages and write CloudWatch logsFunctionResponseTypes: ReportBatchItemFailures— lets the function retry only the messages that failed (see below)
If you’d rather not write the role and event source mapping by hand, AWS SAM generates both from a few lines. See How to Deploy AWS Lambda with SAM and GitLab CI/CD Using OIDC.
Configuring with Serverless Framework
The same configuration in serverless.yml. Note that Serverless Framework v4 is free only for individuals and organizations under $2M in annual revenue; larger organizations need a paid subscription, so consider AWS SAM if that applies to you.
functions:
myFunction:
handler: src/handler.lambda_handler
timeout: 300
events:
- sqs:
arn: !GetAtt MainQueue.Arn
batchSize: 10
functionResponseType: ReportBatchItemFailures
resources:
Resources:
MainQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${sls:stage}-my-queue
VisibilityTimeout: 1800
RedrivePolicy:
deadLetterTargetArn: !GetAtt DeadLetterQueue.Arn
maxReceiveCount: 3
DeadLetterQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${sls:stage}-my-queue-dlq
VisibilityTimeout: 60
MessageRetentionPeriod: 1209600
Retry Only the Messages That Failed
By default, if your function throws an error, Lambda treats the whole batch as failed. Every message, including the ones you already processed, becomes visible again and is retried. With ReportBatchItemFailures enabled (as in the templates above), your handler returns only the failed message IDs:
def lambda_handler(event, context):
failures = []
for record in event["Records"]:
try:
process(record)
except Exception:
failures.append({"itemIdentifier": record["messageId"]})
return {"batchItemFailures": failures}
For a full handler built this way, see How to Extract S3 Payload from SQS + SNS Event in AWS Lambda.
Make Processing Idempotent
A correct visibility timeout reduces duplicates but can’t remove them. SQS standard queues deliver at least once, so the same message can still arrive twice. Design your handler so processing a message twice has the same effect as processing it once: use upserts instead of inserts, or record processed message IDs. The Powertools for AWS Lambda idempotency utility does this for you using a DynamoDB table.
Handling Failed Messages with a Dead Letter Queue
A dead letter queue (DLQ) captures messages that fail processing after maxReceiveCount attempts. Without a DLQ, failed messages keep cycling between the queue and Lambda until the message retention period expires.
Lambda does not automatically retry messages from the DLQ. To reprocess them, you can:
- Use SQS dead-letter queue redrive in the console — select the DLQ, choose “Start DLQ redrive”, and move messages back to the main queue
- Write a separate Lambda function that polls the DLQ and reprocesses or routes the messages
- Replay messages using the AWS CLI (see below)
To move messages from the DLQ back to the queue they came from with the CLI, start a message move task. Leaving out --destination-arn sends each message back to its original source queue:
aws sqs start-message-move-task \
--source-arn arn:aws:sqs:your-region:123456789012:my-queue-dlq
Fix the bug that sent the messages to the DLQ first, or they’ll end up back there. To test fixes against a local queue before redriving, see How to Run AWS Serverless Locally with DynamoDB Local and LocalStack.
If your processing involves long-running or multi-step workflows, consider using Step Functions instead of a single Lambda. For an example of this pattern, see How to Avoid AWS Lambda Timeout When Processing HubSpot Records.
Monitoring SQS and Lambda
Watch these CloudWatch metrics to catch visibility timeout issues early:
| Metric | What It Tells You |
|---|---|
ApproximateNumberOfMessagesVisible |
Messages waiting to be processed — a growing number means Lambda can’t keep up |
ApproximateNumberOfMessagesNotVisible |
Messages currently being processed (in-flight) |
NumberOfMessagesReceived |
Total messages received — compare with NumberOfMessagesDeleted to spot retries |
ApproximateAgeOfOldestMessage |
How long the oldest message has been in the queue — high values indicate a processing backlog |
Set a CloudWatch alarm on the DLQ’s ApproximateNumberOfMessagesVisible metric. Any message landing in the DLQ means something failed, and you want to know about it immediately. For cleaning up old Lambda logs generated during debugging, see How to Delete AWS Lambda Logs from CloudWatch using Python Scripts.
Quick Reference
| Setting | Value |
|---|---|
| Main queue visibility timeout | 6x Lambda timeout (e.g., 1800s for a 300s function) |
| DLQ visibility timeout | 60 seconds |
| DLQ max receive count | 3–5 attempts |
| DLQ message retention | 14 days (1209600 seconds) |
| Maximum visibility timeout | 12 hours (43,200 seconds) |
Conclusion
The fix is straightforward: set your SQS queue’s visibility timeout to at least 6x your Lambda function’s timeout. Pair it with partial batch responses, idempotent processing, a dead letter queue, and a CloudWatch alarm on the DLQ to catch failures early.
For more on how Lambda receives and processes messages, see Understanding Lambda’s Event and Context Parameters. If you’re building cross-account messaging with SQS, check out Setting Up Cross-Account SNS to SQS Subscription in AWS.


