Ric*_*mes 3 amazon-sqs terraform terraform-provider-aws
我正在使用 Terraform 创建 AWS SQS 队列。对于每项服务,我需要创建两个队列,一个正常队列和一个错误队列。每个的设置大多相同,但我需要首先创建错误队列,以便我可以将其 ARN 传递到正常队列,作为其重新驱动策略的一部分。必须有一种更好的方法来循环替换仅名称,而不是创建 10 个模块。所以编程逻辑...对于queue_prefixes中的每个队列,创建错误模块,然后创建常规模块。我确信我只是没有正确搜索或提出正确的问题。
沙箱/main.tf
provider "aws" {
region = "us-west-2"
}
module "hfd_sqs_error_sandbox" {
source = "../"
for_each = var.queue_prefixes
name= each.key+"_Error"
}
module "hfd_sqs_sandbox" {
source = "../"
name=hfd_sqs_error_sandbox.name
redrive_policy = jsonencode({
deadLetterTargetArn = hfd_sqs_error_sandbox_this_sqs_queue_arn,
maxReceiveCount = 3
})
}
Run Code Online (Sandbox Code Playgroud)
变量.tf
variable "queue_prefixes" {
description = "Create these queues with the enviroment prefixed"
type = list(string)
default = [
"Clops",
"Document",
"Ledger",
"Log",
"Underwriting",
"Wallet",
]
}
Run Code Online (Sandbox Code Playgroud)
您可能需要考虑添加一个包装器模块来创建普通队列和死信队列。这将使按顺序创建资源变得更加容易。
考虑这个示例(使用空资源以便于测试):
创建所有队列的根模块:
# ./main.tf
locals {
queue_prefixes = [
"Queue_Prefix_1",
"Queue_Prefix_2",
]
}
module queue_set {
source = "./modules/queue_set"
for_each = toset(local.queue_prefixes)
name = each.key
}
Run Code Online (Sandbox Code Playgroud)
包装器模块创建一组 2 个队列:正常 + dlq:
# ./modules/queue_set/main.tf
variable "name" {
type = string
}
module dlq {
source = "../queue"
name = "${var.name}_Error"
}
module queue {
source = "../queue"
name = var.name
redrive_policy = module.dlq.id
}
Run Code Online (Sandbox Code Playgroud)
适合创建两种类型队列的单独队列资源:
# ./modules/queue/main.tf
variable "name" {
type = string
}
variable "redrive_policy" {
type = string
default = ""
}
resource "null_resource" "queue" {
provisioner "local-exec" {
command = "echo \"Created queue ${var.name}, redrive policy: ${var.redrive_policy}\""
}
# this is irrelevant to the question, it's just to make null resource change every time
triggers = {
always_run = timestamp()
}
}
output "id" {
value = null_resource.queue.id
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我们运行这个堆栈,我们可以看到以正确的顺序创建的资源:
| 归档时间: |
|
| 查看次数: |
1833 次 |
| 最近记录: |