I’m trying to configure SQS with a dead letter queue using Terraform but it’s not working. I tried asking this question on stackoverflow but it’s yet to be answered.
Would here happen to know of a solution?
I’m trying to configure SQS with a dead letter queue using Terraform but it’s not working. I tried asking this question on stackoverflow but it’s yet to be answered.
Would here happen to know of a solution?
Are you setting the visibility timeout on the messages once you receive them? Otherwise they’ll continue to be received as you process the message until they hit the dead letter threshold.
I think you have to set redrive_policy on the SQS to point to the DLQ
Isn’t that done automatically when it’s mapped to a lambda?
That’s what I’m doing
locals {
redrive_policy = {
“deadLetterTargetArn” = aws_sqs_queue.cc-queue_deadletter.arn
“maxReceiveCount” = var.max_receive_count
}
}
resource “aws_sqs_queue” “cc-queue” {
name = var.queue_name
delay_seconds = var.delay_seconds
max_message_size = var.max_message_size
message_retention_seconds = var.message_retention_seconds
receive_wait_time_seconds = var.receive_wait_time_seconds
redrive_policy = jsonencode(local.redrive_policy)
tags = merge(local.common_tags, local.sqs_queue_tags, var.tags)
}
resource “aws_sqs_queue” “cc-queue_deadletter” {
name = “${var.queue_name}_deadletter”
delay_seconds = var.dlq_delay_seconds
max_message_size = var.dlq_max_message_size
message_retention_seconds = var.dlq_message_retention_seconds
receive_wait_time_seconds = var.dlq_receive_wait_time_seconds
tags = merge(local.common_tags, local.sqs_queue_deadletter_tags, var.tags)
}
module “default” {
source = “../../..”
queue_name = “test-${var.environment}”
env = var.environment
}
Last reply is for testing/creation of both queues if you use them within a module
How would it do that? It’s up to your application to know when it’s done processing and when it should release the visibility timeout.
That’s not entirely true. If a lambda is invoke by sqs. The process that triggers the Lambda will automatically delete it if the Lambda successfully executed. If it exits with a failure, it will go back on the queue.
I was just talking about the visibility timeout.
I haven’t used SQS with lambda before, but it’s clear from the docs that it won’t automatically update the visibility timeout. You need to update this yourself as you’re processing, or increase the default visibility timeout in the queue to ensure the message isn’t retried. This is what I was suggesting before. That’s why his messages keep going to dead letter. His processing time is greater than the default visibility timeout set on the queue.
There is really no need to ever have a visibility time out that is not equal to your lambda timeout.
If the Lambda exits successfully. It will be deleted. If the lambda times out, it will automatically go back on the queue.
related: is it a good idea to write to the DLQ through my application for some custom criteria i decide in my application in addition to letting SQS place messages on the DLQ per the criteria set in the redrive policy? or is this an egregious violation of a best practice somewhere?
I guess it’s ok to “forward” the message to the DLQ directly if you know already that retrying will behave the same. You really don’t need to let the message go through few rounds of retry before the redrive kicks in.
I’d say as good practice that you can always have a different queue for the messages that you reroute this way (or even one for each use case of dismissing a message) than the real DLQ.
Hey I found this post extremely helpful to understand the mechanics behind it.
https://lumigo.io/blog/sqs-and-lambda-the-missing-guide-on-failure-modes/
For your issue, does messages get sent to the DLQ right away or do they get processed the number of times you set your redrive threshold to? Are all the lambda executions successfull?
I may have missed something but I think you’re ok. One way I imagine this could go wrong is if your batch number is high and the timeout of your lambda as well.
Let’s say you have a visibility timeout for your messages of 10 seconds, batch size of 4, a lambda typically handling your message in 2 seconds and a lambda timeout of 6 seconds.
4 x 2 = 8 seconds
If one of your lambda execution, in the batch, time out, it will expire the visibility timeout of the whole batch of messages. (3 executions x 2 seconds = 6 seconds of execution + 6 sec of 1 execution timeout = 12 seconds > 10 sec visibility timeout)
This could lead to messages from this batch picked up by another jambe, duplicate handling of messages and may be hitting redrive threshold…
Watch out for settings of visibility timeout, batch size and redrive policy. To debug, try putting a batch of 1, redrive policy of 4 and visibility timeout to at least the same as the timeout value of your lambda. I would generally say set it to at least (lambda timeout x batch size).
I hope it’s clear. Don’t hesitate to post your answer if you find out what’s going on. ![]()
No, you set a visibility timeout when you configure your queue. This timeout is handled by your queue entirely and it can expire while the message is being handled if your lambda takes too long. This will most certainly cause your message to get picked up by another lambda. But I agree that might the issue. Read this one which I found extremely helpful.
https://lumigo.io/blog/sqs-and-lambda-the-missing-guide-on-failure-modes/
That’s what I’m saying.
You can update the visibility timeout as your function processes to ensure that it doesn’t get picked up.
I don’t think it’s healthy though if you look at the provided code (very basic example). If you don’t know how long it takes too process a message, then implementing a heartbeat to keep the message in flight is the way to do it, yes.
I’m not entirely sure but as others have noted here, by not throwing exceptions and returning 0 would be my guess.
Yup, that’s what I’m saying. ![]()