【发布时间】:2020-08-20 12:42:21
【问题描述】:
我正在尝试迭代端口和端口范围的映射以在 Terraform 中创建 AWS 安全组。对于 Ingress 端口,它们提供了一个 from_port 字段和一个 to_port 字段。这应该定义安全组中特定规则的端口范围。当规则是单个端口时,我可以很好地进行映射,因为从端口和到端口都是相同的。但是当规则需要一系列端口时,我无法弄清楚如何做到这一点。这是我一直在测试的代码。我认为我对如何执行此操作有正确的想法,但遇到了类型错误。
我实际上是在尝试使用 from_port 的索引值来查找应该映射到 to_port 的端口。
provider "aws" {
region = "us-east-2"
profile = "default"
}
locals {
start = [22, 33, 44] # from_ports
end = [25, 35, 45] # to_ports
vpcid = "vpc-xxxxxxxxxxxx"
}
resource "aws_security_group" "traffic_secgrp" {
name = "traffic_sec_grp"
description = "Allows traffic"
vpc_id = local.vpcid
dynamic "ingress" {
for_each = local.start
content {
from_port = ingress.value
to_port = element(local.end, [index(local.start, ingress.value)])
protocol = "UDP"
cidr_blocks = ["10.2.0.0/20"]
}
}
}
这是我从to_port 行得到的错误:
Error: Invalid function argument
on main.tf line 21, in resource "aws_security_group" "traffic_secgrp":
21: to_port = element(local.end, [index(local.start, ingress.value)])
|----------------
| ingress.value is 44
| local.start is tuple with 3 elements
Invalid value for "index" parameter: number required.
【问题讨论】:
标签: amazon-web-services terraform