使用 terraform 变量创建多个元素的正确方法是什么?
Whats the right way to create multiple elements using terraform variables?
我正在使用 Terraform 创建 AWS SQS 队列。对于每项服务,我需要创建两个队列,一个正常队列和一个错误队列。每个的设置大部分相同,但我需要先创建错误队列,这样我才能将其 ARN 作为其重新驱动策略的一部分传递给普通队列。不是创建 10 个模块,而是必须有一种更好的方法来循环只替换名称。所以编程逻辑... foreach queue in queue_prefixes,创建错误模块,然后是常规模块。我确定我只是没有正确搜索或提出正确的问题。
sandbox/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
})
}
variables.tf
variable "queue_prefixes" {
description = "Create these queues with the enviroment prefixed"
type = list(string)
default = [
"Clops",
"Document",
"Ledger",
"Log",
"Underwriting",
"Wallet",
]
}
您可能需要考虑添加一个包装器模块来创建普通队列和死信队列。这将使按顺序创建资源变得更加容易。
考虑这个例子(使用空资源以便于测试):
创建所有队列的根模块:
# ./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
}
创建一组 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
}
单个队列适合创建两种类型队列的资源:
# ./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
}
现在如果我们 运行 这个堆栈,我们可以看到按正确顺序创建的资源:
我正在使用 Terraform 创建 AWS SQS 队列。对于每项服务,我需要创建两个队列,一个正常队列和一个错误队列。每个的设置大部分相同,但我需要先创建错误队列,这样我才能将其 ARN 作为其重新驱动策略的一部分传递给普通队列。不是创建 10 个模块,而是必须有一种更好的方法来循环只替换名称。所以编程逻辑... foreach queue in queue_prefixes,创建错误模块,然后是常规模块。我确定我只是没有正确搜索或提出正确的问题。
sandbox/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
})
}
variables.tf
variable "queue_prefixes" {
description = "Create these queues with the enviroment prefixed"
type = list(string)
default = [
"Clops",
"Document",
"Ledger",
"Log",
"Underwriting",
"Wallet",
]
}
您可能需要考虑添加一个包装器模块来创建普通队列和死信队列。这将使按顺序创建资源变得更加容易。
考虑这个例子(使用空资源以便于测试):
创建所有队列的根模块:
# ./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
}
创建一组 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
}
单个队列适合创建两种类型队列的资源:
# ./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
}
现在如果我们 运行 这个堆栈,我们可以看到按正确顺序创建的资源: