Next, we will create the lambda/sqs.tf file. This file provisions the Amazon Simple Queue Service (SQS), including a primary queue and a Dead Letter Queue (DLQ).
Instead of allowing the Lambda Collector function to invoke the Lambda Analyzer function directly, we place Amazon SQS between them to act as a message buffer:
# Create the Dead Letter Queue.
resource "aws_sqs_queue" "dlq" {
name = "${var.project_name}-dlq"
message_retention_seconds = 1209600 # Maximum message retention period is 14 days.
}
# Create the primary queue.
# If a message fails to be processed more than three times, it is automatically moved to the DLQ to prevent processing from being blocked.
resource "aws_sqs_queue" "events" {
name = "${var.project_name}-events"
visibility_timeout_seconds = 300 # The message visibility timeout must be greater than or equal to the Analyzer Lambda timeout.
message_retention_seconds = 345600 # Messages are retained for 4 days.
# Move failed events to the Dead Letter Queue after three failed processing attempts.
redrive_policy = jsonencode({
deadLetterTargetArn = aws_sqs_queue.dlq.arn
maxReceiveCount = 3
})
}