简体   繁体   English

如何解决为使用 for_each 创建的死信队列添加 SQS 重新驱动策略时的错误消息

[英]How to resolve the error message when adding SQS redrive policy for deadletter queue created using for_each

I want terraform to associate my SQS Management Event with my DLQ management event and i want the same thing done with SQS Data Event and DLQ Data Event.I am getting error messages when i run apply on my code below.please I need some help.我希望 terraform 将我的 SQS 管理事件与我的 DLQ 管理事件相关联,并且我希望对 SQS 数据事件和 DLQ 数据事件做同样的事情。当我在下面的代码上运行应用程序时,我收到错误消息。请我需要一些帮助。

.tfvars

sqs_queue_names = ["CloudTrail_SQS_Management_Event", "CloudTrail_SQS_Data_Event"]

dead_queue_names = ["CloudTrail_DLQ_Management_Event", "CloudTrail_DLQ_Data_Event"]
variable.tf 

variable "sqs_queue_names"{
  description = "The name of different SQS to be created"
  type        = set(string)
}

variable "dead_queue_names"{
  description = "The name of different Dead Queues to be created"
  type        = set(string)
}
main.tf

resource "aws_sqs_queue" "CloudTrail_SQS"{

    for_each                   = var.sqs_queue_names
    name                       = each.value
    redrive_policy = jsonencode({
        deadLetterTargetArn    = values(aws_sqs_queue.CloudTrail_SQS_DLQ)[*].arn
        maxReceiveCount        = var.max_receive_count
    })

    tags = var.default_tags
    
}

resource "aws_sqs_queue" "CloudTrail_SQS_DLQ"{

    for_each                   = var.dead_queue_names
    name                       = each.value
   
    tags = var.default_tags
}
ERROR MESSAGES:
Error: error creating SQS Queue (CloudTrail_SQS_Management_Event): InvalidParameterValue: Value {"deadLetterTargetArn":["arn:aws:sqs:us-east-1:123456789012:CloudTrail_DLQ_Data_Event","arn:aws:sqs:us-east-1:123456789012:CloudTrail_DLQ_Management_Event"],"maxReceiveCount":10} for parameter RedrivePolicy is invalid. Reason: Invalid value for deadLetterTargetArn.
│       status code: 400, request id: 9663b896-d86f-569e-92e2-e17152c2db26
│ 
│   with aws_sqs_queue.CloudTrail_SQS["CloudTrail_SQS_Management_Event"],
│   on main.tf line 5, in resource "aws_sqs_queue" "CloudTrail_SQS":
│    5: resource "aws_sqs_queue" "CloudTrail_SQS"{
│ 
╵
╷
│ Error: error creating SQS Queue (CloudTrail_SQS_Data_Event): InvalidParameterValue: Value {"deadLetterTargetArn":["arn:aws:sqs:us-east-1:123456789012:CloudTrail_DLQ_Data_Event","arn:aws:sqs:us-east-1:123456789012:CloudTrail_DLQ_Management_Event"],"maxReceiveCount":10} for parameter RedrivePolicy is invalid. Reason: Invalid value for deadLetterTargetArn.
│       status code: 400, request id: 88b8e4c5-1d50-5559-92f8-bd2297fd231f
│ 
│   with aws_sqs_queue.CloudTrail_SQS["CloudTrail_SQS_Data_Event"],
│   on main.tf line 5, in resource "aws_sqs_queue" "CloudTrail_SQS":
│    5: resource "aws_sqs_queue" "CloudTrail_SQS"{

The problem here is that you are not associating the dead letter queue with the corresponding SQS queue.这里的问题是您没有将死信队列与相应的 SQS 队列相关联。 values(aws_sqs_queue.CloudTrail_SQS_DLQ)[*].arn - this essentially passes every dead letter queue ARN for each SQS queue, it does not passes to correct ARN only. values(aws_sqs_queue.CloudTrail_SQS_DLQ)[*].arn - 这实质上传递了每个 SQS 队列的每个死信队列 ARN,它不会仅传递给正确的 ARN。

In order to overcome this, I suggest creating a module where we can tie together the SQS queue and its DLQ.为了克服这个问题,我建议创建一个模块,我们可以在其中将 SQS 队列及其 DLQ 联系在一起。 We can name for now my_sqs :我们现在可以命名为my_sqs

my_sqs/variables.tf : my_sqs/variables.tf

variable "sqs_queue_name"{
  description = "The name of different SQS to be created"
  type        = string
}

variable "dead_queue_name"{
  description = "The name of different Dead Queues to be created"
  type        = string
}

variable "max_receive_count" {
    type = number
}


my_sqs/main.tf : my_sqs/main.tf

resource "aws_sqs_queue" "sqs" {
  name  = var.sqs_queue_name

  redrive_policy = jsonencode({
    deadLetterTargetArn = aws_sqs_queue.dlq.arn
    maxReceiveCount     = var.max_receive_count
  })
}

resource "aws_sqs_queue" "dlq" {
  name  = var.dead_queue_name
}

Now we can use this module like this:现在我们可以像这样使用这个模块:

variables.tf : variables.tf

# Please not, we are tying the SQS and the DQL together here as well.
variable "queue_names" {
  default = [
    {
      sqs_name = "CloudTrail_SQS_Management_Event"
      dlq_name = "CloudTrail_DLQ_Management_Event"
    },
    {
      sqs_name = "CloudTrail_SQS_Data_Event"
      dlq_name = "CloudTrail_DLQ_Data_Event"
    }
  ]
}

From the main.tf we call the module we created above:main.tf我们调用我们上面创建的模块:

main.tf : main.tf

module "my_sqs" {
  source = "./my_sqs"
  for_each = {
    for sqs, dlq in var.queue_names : sqs => dlq
  }
  sqs_queue_name    = each.value.sqs_name
  dead_queue_name   = each.value.dlq_name
  max_receive_count = 4
}

Please note, this example may work with the latest Terraform versions.请注意,此示例可能适用于最新的 Terraform 版本。 It may not work with an older version which does not support having a for_each on a module .它可能不适用于不支持在module上使用for_each的旧版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为使用 for_each 创建的死信队列添加 SQS 重新驱动策略时如何修复错误消息 - How to fix error message when adding SQS redrive policy for deadletter queue created using for_each CloudFormation SQS队列Redrive策略依赖于创建的DLQ - CloudFormation SQS Queue Redrive policy dependency on a DLQ created 使用 AWS CLI 命令添加 SQS 重新驱动策略 - Adding SQS redrive policy using AWS CLI command 在死信队列中有消息时如何阻止 AWS SQS FIFO? - How to block AWS SQS FIFO while having a message in the deadletter queue? AWS SQS重新驱动策略,请在队列的末尾进行混乱 - AWS SQS redrive policy, which end of the queue do messges go to AWS SQS 死信队列重新驱动策略 - AWS SQS dead-letter queue redrive policy 如果没有设置重新驱动策略,AWS SQS 中的失败消息会发生什么情况? - What happends to a failed message in AWS SQS if there is no redrive policy set? 如何将使用 for_each 创建的每个 SQS 和 DLQ 以有序格式相互关联 - how to associate each SQS and DLQ created using for_each with one another in orderly format 如何确保在相应死信队列中有消息时阻塞 SQS FIFO - How to ensure SQS FIFO is blocked while having a message in the corresponding deadletter queue 无服务器:配置死信 SQS 队列 - serverless: configure deadletter SQS queue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM