如何使用模块指定死信依赖? [英] How to specify dead letter dependency using modules?

查看:177
本文介绍了如何使用模块指定死信依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此官方模块,我有以下核心模块:

I have the following core module based off this official module:

module "sqs" {
  source = "github.com/terraform-aws-modules/terraform-aws-sqs?ref=0d48cbdb6bf924a278d3f7fa326a2a1c864447e2"
  name = "${var.site_env}-sqs-${var.service_name}"
}

我想创建两个队列:xyz和xyz_dead. xyz将其死信消息发送到xyz_dead.

I'd like to create two queues: xyz and xyz_dead. xyz sends its dead letter messages to xyz_dead.

module "xyz_queue" {
  source       = "../helpers/sqs"
  service_name = "xyz"

  redrive_policy = <<POLICY { 
      "deadLetterTargetArn" : "${data.TODO.TODO.arn}",   
      "maxReceiveCount" : 5
  }
  POLICY
  site_env = "${var.site_env}"
}

module "xyz_dead_queue" {
  source       = "../helpers/sqs"
  service_name = "xyz_dead"
  site_env     = "${var.site_env}"
}

如何指定deadLetterTargetArn依赖项?

如果我这样做:

data "aws_sqs_queue" "dead_queue" {
  filter {
    name   = "tag:Name"
    values = ["${var.site_env}-sqs-xyz_dead"]
  }
}

并将deadLetterTargetArn设置为"${data.aws_sqs_queue.dead_queue.arn}",然后出现此错误:

and set deadLetterTargetArn to "${data.aws_sqs_queue.dead_queue.arn}", then I get this error:

错误:data.aws_sqs_queue.thumbnail_requests_queue_dead:名称": 未设置必填字段错误: data.aws_sqs_queue.thumbnail_requests_queue_dead::无效或未知 键:过滤器

Error: data.aws_sqs_queue.thumbnail_requests_queue_dead: "name": required field is not set Error: data.aws_sqs_queue.thumbnail_requests_queue_dead: : invalid or unknown key: filter

推荐答案

做到这一点的最佳方法是使用

The best way to do this is to use the outputted ARN from the module:

module "xyz_queue" {
  source       = "../helpers/sqs"
  service_name = "xyz"
  site_env     = "${var.site_env}"

  redrive_policy = <<POLICY
{ 
  "deadLetterTargetArn" : "${module.xyz_dead_queue.this_sqs_queue_arn}",   
  "maxReceiveCount" : 5
}
POLICY
}

module "xyz_dead_queue" {
  source       = "../helpers/sqs"
  service_name = "xyz_dead"
  site_env     = "${var.site_env}"
}

注意:我还更改了您的HEREDOC的缩进,因为您通常需要使用这些缩进来删除

NB: I've also changed the indentation of your HEREDOC here because you normally need to remove the indentation with these.

这会将SQS队列的ARN直接从xyz_dead_queue模块传递到xyz_queue.

This will pass the ARN of the SQS queue directly from the xyz_dead_queue module to the xyz_queue.

对于您遇到的错误, aws_sqs_queue数据源仅采用name参数,而不采用filter像其他一些数据源一样进行阻塞.

As for the errors you were getting, the aws_sqs_queue data source takes only a name argument, not a filter block like some of the other data sources do.

如果您想使用aws_sqs_queue数据源,那么您只想使用:

If you wanted to use the aws_sqs_queue data source then you'd just want to use:

data "aws_sqs_queue" "dead_queue" {
  name = "${var.site_env}-sqs-${var.service_name}"
}

也就是说,如果您同时创建两个对象,那么除非首先创建第一个资源,否则使用数据源引用其中一个对象会遇到问题.这是因为数据源在资源之前运行,因此如果两个队列都不存在,则您的数据源将运行并且找不到死信队列,从而失败.如果死信队列确实存在,那就可以了.通常,尽管最好避免使用这样的数据源,而仅使用它们来引用在单独的terraform apply中创建的内容(甚至可能是在Terraform之外创建的).

That said, if you are creating two things at the same time then you are going to have issues using a data source to refer to one of those things unless you create the first resource first. This is because data sources run before resources so if neither queue yet exists your data source would run and not find the dead letter queue and thus fail. If the dead letter queue did exist then it would be okay. In general though you're best off avoiding using data sources like this and only use them to refer to things being created in a separate terraform apply (or perhaps even created outside of Terraform).

将资源或模块的输出传递到其他资源/模块,并允许Terraform也为它们正确构建依赖关系树,您会变得更好.

You are also much better off simply passing the outputs of resources or modules to other resources/modules and allowing Terraform to correctly build a dependency tree for them as well.

这篇关于如何使用模块指定死信依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆