You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform安全组模块创建及调用报错:动态块不支持问题解决

Terraform安全组模块调用错误解决方案

问题根源

你的安全组模块内部已经通过dynamic "ingress"结合var.ingress_rules变量处理了入站规则逻辑,模块对外暴露的是变量参数而非直接的ingress块定义。外部调用时直接写dynamic "ingress"块,Terraform会判定该模块不支持这个块类型,因此抛出「Unsupported block type」错误。

正确调用方式

1. 确认模块变量定义(security-group/variable.tf)

确保模块对外暴露的ingress_rules变量类型正确,能接收规则列表:

variable "ingress_rules" {
  type = list(object({
    from_port   = number
    to_port     = number
    protocol    = string
    cidr_blocks = list(string)
    description = optional(string) # 可选描述字段
  }))
  description = "安全组入站规则列表"
  default     = []
}

2. 模块内部规则处理(security-group/main.tf)

保持用dynamic "ingress"遍历变量生成规则:

resource "aws_security_group" "this" {
  name        = var.name
  description = var.description
  vpc_id      = var.vpc_id

  dynamic "ingress" {
    for_each = var.ingress_rules
    content {
      from_port   = ingress.value.from_port
      to_port     = ingress.value.to_port
      protocol    = ingress.value.protocol
      cidr_blocks = ingress.value.cidr_blocks
      description = ingress.value.description
    }
  }

  # 默认出站规则(按需调整)
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

3. 外部主文件调用模块

直接向模块的ingress_rules变量传入规则列表即可,无需写dynamic块:

module "web_server_sg" {
  source = "./security-group"

  name        = "web-server-security-group"
  description = "允许HTTP和SSH访问的Web服务器安全组"
  vpc_id      = aws_vpc.main.id

  ingress_rules = [
    {
      from_port   = 80
      to_port     = 80
      protocol    = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
      description = "允许公网HTTP访问"
    },
    {
      from_port   = 22
      to_port     = 22
      protocol    = "tcp"
      cidr_blocks = ["192.168.1.0/24"]
      description = "允许内网SSH登录"
    }
  ]
}

额外说明

如果需要模块支持更灵活的入站规则配置(比如不同的规则类型、自定义块参数),可以调整模块设计,改用variable "ingress_blocks"接收原始的ingress块结构,但这种方式不如列表变量直观易维护。当前场景下,通过变量列表封装规则是更规范的模块化实践。

内容的提问来源于stack exchange,提问作者Leonardo Arango

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 19:18:36