SNS Error Handling: Fixes for Failed Message Delivery
In this article
- How to Handle SNS Delivery Failures, Step by Step
- Why SNS Reports an Error at All
- Publish Errors: When the API Call Itself Fails
- HTTP or HTTPS Endpoint Delivery Failures
- Lambda and SQS Subscription Failures
- Setting Up the Dead-Letter Queue Properly
- Which Error Do You Actually Have?
- What to Put in Your Application Code
Most SNS errors come from failed deliveries to a subscription endpoint, not from publishing. Turn on a dead-letter queue (DLQ) for each subscription, check CloudWatch metrics for failed deliveries, and fix the endpoint or its policy so messages stop failing.
Time 20 min · Steps 7 · Level Medium
How to Handle SNS Delivery Failures, Step by Step
- Confirm where the error sits. A publish error (the API call to SNS itself) is different from a delivery error (SNS could not reach the subscriber). Publish errors show up in your application logs; delivery errors show up in CloudWatch metrics like NumberOfNotificationsFailed.
- Attach a dead-letter queue to the subscription. In the SNS console, open the subscription, edit its redrive policy, and point it at an SQS queue you control. Failed messages then land there instead of vanishing after retries are exhausted.
- Check CloudWatch metrics and logs. Look at NumberOfNotificationsFailed for the topic and, for Lambda subscribers, the function's own error logs in CloudWatch Logs. The error text tells you which fix applies.
- Fix the endpoint or its access policy. For HTTP/S endpoints, make sure your server returns 200 OK quickly and allows SNS to call it. For SQS and Lambda subscriptions, check the resource access policy so SNS has permission to deliver.
- Verify the subscription is confirmed. Pending subscriptions receive nothing. Confirm the endpoint via the confirmation message, and treat a subscription that reverts to pending as a sign the endpoint stopped responding.
- Redrive the failed messages. Once the endpoint is healthy, move the messages from the DLQ back onto the topic or directly to the subscriber, then verify each one was processed.
- Handle publish errors in code. Catch exceptions from the publish call, log the message ID, and retry with backoff for transient errors rather than dropping the message.
Why SNS Reports an Error at All
SNS is a push service: your application publishes once, and SNS then tries to deliver to every subscriber. If a subscriber's endpoint is down, returns an error, or lacks permission, SNS retries the delivery for a while, and if the retries fail, the message is discarded unless a DLQ catches it. That is why one broken subscriber can look like "SNS is broken" while the publish call itself succeeded. The message ID from the publish response is the key to tracing what happened next.
Publish Errors: When the API Call Itself Fails
A publish error means SNS never accepted the message, so nothing was delivered to anyone. You will see an exception in your code, not a CloudWatch delivery failure.
How to confirm it
Check whether the publish call returned a message ID at all. No message ID means the publish failed, and your logs should hold the exception text. Compare that against the common causes below.
Common causes and fixes
Invalid parameter values, such as a malformed phone number for SMS delivery, are rejected immediately; correct the value and republish. A topic that does not exist in that region, usually because the topic ARN has a typo or the wrong region, returns a not-found error; copy the ARN from the console rather than typing it. Missing publish permission on the caller's IAM policy shows up as an access-denied error; grant sns:Publish on the topic ARN to the role or user making the call. Message size over the service limit is rejected outright; split the payload or send a pointer to data stored elsewhere, such as an S3 object key. Throttling on a very high publish rate returns retryable errors; retry with exponential backoff so a burst does not turn into lost messages.
HTTP or HTTPS Endpoint Delivery Failures
For HTTP/S subscribers, your server must return a 200-level response, and it must handle SNS's subscription confirmation, notification, and unsubscribe confirmation message types. Retrying takes place over roughly six hours at increasing intervals before SNS gives up.
To confirm this is your problem, watch your own server logs for SNS POST requests and the status codes you returned. A 4xx or 5xx response, or a timeout, counts as a failure. The fix has three parts: respond 200 quickly even if you process the message asynchronously, verify the message signature if you need to trust the contents, and set a DLQ so anything that still fails is preserved rather than dropped. If your endpoint is behind a firewall, allow SNS's IP ranges, which AWS publishes in its documentation, and note that the exact ranges change over time so check the current list rather than a cached one.
Lambda and SQS Subscription Failures
Lambda subscribers fail when the function itself errors, and SNS retries a failed asynchronous invocation a small number of times before giving up. Check the function's CloudWatch Logs for the actual exception; the SNS side only tells you delivery failed, not why. Fix the code error, then redrive from the DLQ.
SQS subscriptions fail when the queue cannot accept the message. The usual cause is the queue's access policy not granting SNS permission to send to it, which shows up as an access-denied error in CloudWatch. Either attach the policy on the queue side allowing the topic's service principal, or rely on the subscription created through the console, which sets the permission for you. Also confirm the queue is not full to the point of rejecting messages and that it is not a FIFO queue, which needs a different setup than a standard queue.
Setting Up the Dead-Letter Queue Properly
The DLQ is your safety net, and it is set per subscription, not per topic. Create a standard SQS queue, then in the SNS console open the subscription, edit the redrive policy, and choose that queue. When delivery retries are exhausted, SNS sends the original message plus a JSON envelope describing the failure to the DLQ.
Two habits keep the DLQ useful. First, monitor it: an empty DLQ is the goal, so set a CloudWatch alarm on the queue's approximate number of visible messages so you learn about failures within minutes rather than days. Second, redrive deliberately: read the envelope to see why the message failed, fix the endpoint, and only then replay the message, otherwise it will fail again and you will loop. Note that a DLQ captures delivery failures; it does nothing for publish errors, which your own code must catch.
Which Error Do You Actually Have?
Work through three questions in order. Did the publish call return a message ID? If no, it is a publish error and the fix is in your code, your IAM policy, or your topic ARN. If yes, is the subscription confirmed and healthy? A pending subscription or a deleted endpoint delivers nothing. If the subscription looks fine, check CloudWatch for delivery failures and your subscriber's logs for the underlying error. This order matters because fixing the subscriber does nothing if the message never reached SNS in the first place.
What to Put in Your Application Code
Wrap every publish call in error handling that separates retryable from non-retryable failures. Throttling and transient service errors deserve a retry with exponential backoff, starting around one second and doubling up to a sensible cap. Invalid parameter and access-denied errors will not fix themselves, so log them loudly and surface them rather than retrying. Always log the message ID when a publish succeeds, because that ID is what lets you trace a specific message through CloudWatch later. For anything business-critical, consider writing the message to SQS first and publishing from a worker, so a publish failure never loses the event.
Official source: AWS Documentation