我在 terraform 模块中有两个列表...
cidr_blocks = ["1.2.3.4/32","5.6.7.8/32"]
Run Code Online (Sandbox Code Playgroud)
我还有另一个网络 ACL 设置列表
ingress_ports = [
{
from_port = 80
to_port = 80
protocol = "tcp"
},
{
from_port = 443
to_port = 443
protocol = "tcp"
},
{
from_port = 22
to_port = 22
protocol = "tcp"
}
]
Run Code Online (Sandbox Code Playgroud)
我想将这些列表连接在一起,这样我就可以使用结果列表来创建网络 ACL。
result = [
{
cidr_block = "1.2.3.4/32"
from_port = 80
to_port = 80
protocol = "tcp"
},
{
cidr_block = "1.2.3.4/32"
from_port = 443
to_port = 443
protocol = "tcp"
},
{
cidr_block = "1.2.3.4/32"
from_port = 22
to_port = 22
protocol = "tcp"
},
{
cidr_block = "5.6.7.8/32"
from_port = 80
to_port = 80
protocol = "tcp"
},
{
cidr_block = "5.6.7.8/32"
from_port = 443
to_port = 443
protocol = "tcp"
},
{
cidr_block = "5.6.7.8/32"
from_port = 22
to_port = 22
protocol = "tcp"
}
]
Run Code Online (Sandbox Code Playgroud)
这种事情在 Terraform 中可能吗?
在 terraform 0.12 中,我们最终可以使用setproduct,这使这项工作变得更加容易:
cronjobs = [
{
schedule_expression = "cron(5 0 * * ? *)"
command_and_arguments = ["app/aws-console.sh", "task1"]
description = "Hello World"
},
{
schedule_expression = "cron(0 5 1 * ? *)"
command_and_arguments = ["app/aws-console.sh", "task2"]
description = "Send Bills"
}
]
environments = ["dev", "stage"]
locals {
cronjobs_for_all_environments = setproduct(var.cronjobs, var.environments)
}
resource "aws_cloudwatch_event_rule" "cronjob" {
count = length(local.cronjobs_for_all_environments)
name = "cronjob-${local.cronjobs_for_all_environments[count.index][1]}"
description = "${local.cronjobs_for_all_environments[count.index][0].description} (${local.cronjobs_for_all_environments[count.index][1]})"
schedule_expression = local.cronjobs_for_all_environments[count.index][0].schedule_expression
}
Run Code Online (Sandbox Code Playgroud)
这是我使用的解决方案 下面是您的测试 terraform 脚本
provider "aws" {
region = "us-east-1"
}
variable "lista" {
default = ["1", "2", "3"]
}
variable "listb" {
default = ["A", "B", "C", "D"]
}
resource "aws_eip" "eip" {
count = "${length(var.lista) * length(var.listb)}"
tags {
Name = "test-eip ${count.index}. ${element(var.lista, ceil(count.index/length(var.listb)))}:${element(var.listb, count.index)}"
}
}
Run Code Online (Sandbox Code Playgroud)
测试运行terraform计划| grep tags.Name
下面是输出
tags.Name: "0. 1:A"
tags.Name: "1. 1:B"
tags.Name: "2. 1:C"
tags.Name: "3. 1:D"
tags.Name: "4. 2:A"
tags.Name: "5. 2:B"
tags.Name: "6. 2:C"
tags.Name: "7. 2:D"
tags.Name: "8. 3:A"
tags.Name: "9. 3:B"
tags.Name: "10. 3:C"
tags.Name: "11. 3:D"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3523 次 |
最近记录: |