Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the URL for a Dead Letter Queue for an SQS queue generated using Terraform?

I can get the URL of an SQS queue by creating a Terraform Data Source like below,

data "aws_sqs_queue" "my-sqs-queue-name" {
  name = "my-sqs-queue-name"
}

and then by referring to it as,

QUEUE_URL = data.aws_sqs_queue.my-sqs-queue-name.url

How can I get the URL for the Dead Letter Queue created automatically with the above Queue?
I want something like the following. Is it even possible?

DEAD_LETTER_QUEUE_URL = data.aws_sqs_queue.my-sqs-queue-name.dead_letter_queue.url
like image 301
Ishan Madhusanka Avatar asked Jan 23 '26 15:01

Ishan Madhusanka


1 Answers

Below is code to create a Dead letter queue and how to get SQS url using Terraform resource attributes and data source.

https://www.terraform.io/docs/providers/aws/r/sqs_queue.html https://www.terraform.io/docs/providers/aws/d/sqs_queue.html

resource "aws_sqs_queue" "example" {
  name   = "example"
  policy = data.aws_iam_policy_document.sqs-queue-policy.json
  redrive_policy            = jsonencode({
    deadLetterTargetArn = aws_sqs_queue.example_dlq.arn
    maxReceiveCount     = 5
  })
}

resource "aws_sqs_queue" "example_dlq" {
  name = "example-dlq"
}

data "aws_sqs_queue" "example_dlq {
  name = "example-dlq"
}

output "sqs_url_data_source" {
  value = data.aws_sqs_queue.example_dlq.url
}

output "sqs_url_resource_attribute" {
  value = aws_sqs_queue.example.example_dlq.id
}

Or

DEAD_LETTER_QUEUE_URL = aws_sqs_queue.example.example_dlq.id

DEAD_LETTER_QUEUE_URL = data.aws_sqs_queue.example_dlq.url
like image 111
Piyush Sonigra Avatar answered Jan 26 '26 11:01

Piyush Sonigra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!