我是 terraform 的新手,正在尝试创建一个具有入口和出口规则的 AWS 安全组。我尝试使用 terraformlookup函数,而不是对值进行硬编码并创建多个入口和出口块。
main.tf文件看起来像这样:
provider "aws" {
version = "~> 2.0"
region = var.region
profile = var.profile
}
resource "aws_security_group" "this" {
name = "test-sg"
description = "test security group"
dynamic "ingress" {
for_each = var.ingress_rules
content {
description = lookup(ingress.value, "description", null)
from_port = lookup(ingress.value, "from_port", null)
to_port = lookup(ingress.value, "to_port", null)
protocol = lookup(ingress.value, "protocol", null)
cidr_blocks = lookup(ingress.value, "cidr_blocks", null)
}
}
egress {
from_port = 0
to_port = 0
protocol …Run Code Online (Sandbox Code Playgroud)