如何在 Terraform 中对资源名称使用多个循环
How to use multiple loops for a resource name in Terraform
我正在寻找一种通过循环两个 list(string)
类型变量来动态创建 AWS S3 存储桶的方法。这是我目前所拥有的:
variable "network" {
type = list(string)
default = ["blue", "green", "orange"]
}
variable "type" {
type = list(string)
default = ["ClientA", "ClientB"]
}
resource "aws_s3_bucket" "this" {
for_each = var.type
content {
bucket = "${each.key}-${var.network}.mysite.com"
tags = local.tags
}
}
问题是我不确定如何也让它循环通过var.network
。理想情况下,它应该根据上面的示例创建以下存储桶:
ClientA-blue.mysite.com
ClientA-green.mysite.com
ClientA-orange.mysite.com
ClientB-blue.mysite.com
ClientB-green.mysite.com
ClientB-orange.mysite.com
有人知道我如何实现吗?
你做的是flatten the outcome of some nested for's.
locals {
networks = ["blue", "green", "orange"]
types = ["ClientA", "ClientB"]
stuff = flatten([for network in local.networks : [
for type in local.types : [
"${type}-${network}.mysite.com"
]
]])
}
output "stuff" {
value = local.stuff
}
产生:
Changes to Outputs:
+ stuff = [
+ "ClientA-blue.mysite.com",
+ "ClientB-blue.mysite.com",
+ "ClientA-green.mysite.com",
+ "ClientB-green.mysite.com",
+ "ClientA-orange.mysite.com",
+ "ClientB-orange.mysite.com",
]
在您的情况下,该资源如下所示:
resource "aws_s3_bucket" "this" {
for_each = flatten([for network in var.network : [
for type in var.type : [
"${type}-${network}"
]
]])
content {
bucket = "${each.key}.mysite.com"
tags = local.tags
}
}
虽然双 for
循环可能有效,但我建议使用 setproduct
:
resource "aws_s3_bucket" "this" {
for_each = toset([for p in setproduct(var.type, var.network) : "${p[0]}-${p[1]}"])
bucket = "${each.key}.mysite.com"
tags = local.tags
}
我正在寻找一种通过循环两个 list(string)
类型变量来动态创建 AWS S3 存储桶的方法。这是我目前所拥有的:
variable "network" {
type = list(string)
default = ["blue", "green", "orange"]
}
variable "type" {
type = list(string)
default = ["ClientA", "ClientB"]
}
resource "aws_s3_bucket" "this" {
for_each = var.type
content {
bucket = "${each.key}-${var.network}.mysite.com"
tags = local.tags
}
}
问题是我不确定如何也让它循环通过var.network
。理想情况下,它应该根据上面的示例创建以下存储桶:
ClientA-blue.mysite.com
ClientA-green.mysite.com
ClientA-orange.mysite.com
ClientB-blue.mysite.com
ClientB-green.mysite.com
ClientB-orange.mysite.com
有人知道我如何实现吗?
你做的是flatten the outcome of some nested for's.
locals {
networks = ["blue", "green", "orange"]
types = ["ClientA", "ClientB"]
stuff = flatten([for network in local.networks : [
for type in local.types : [
"${type}-${network}.mysite.com"
]
]])
}
output "stuff" {
value = local.stuff
}
产生:
Changes to Outputs:
+ stuff = [
+ "ClientA-blue.mysite.com",
+ "ClientB-blue.mysite.com",
+ "ClientA-green.mysite.com",
+ "ClientB-green.mysite.com",
+ "ClientA-orange.mysite.com",
+ "ClientB-orange.mysite.com",
]
在您的情况下,该资源如下所示:
resource "aws_s3_bucket" "this" {
for_each = flatten([for network in var.network : [
for type in var.type : [
"${type}-${network}"
]
]])
content {
bucket = "${each.key}.mysite.com"
tags = local.tags
}
}
虽然双 for
循环可能有效,但我建议使用 setproduct
:
resource "aws_s3_bucket" "this" {
for_each = toset([for p in setproduct(var.type, var.network) : "${p[0]}-${p[1]}"])
bucket = "${each.key}.mysite.com"
tags = local.tags
}