How to specify dead letter dependency using modules?

随声附和 提交于 2020-03-04 05:40:18

问题


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}"
}

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}"
}

How do I specify the deadLetterTargetArn dependency?

If I do:

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

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

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


回答1:


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}"
}

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

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

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.

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}"
}

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).

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.



来源:https://stackoverflow.com/questions/52359343/how-to-specify-dead-letter-dependency-using-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!