Terraform-迭代并创建安全组的入口规则

问题描述 投票:1回答:1

我一直在为AWS基础设施编写可重用模块。在创建安全组时,我的方法是为安全组创建通用模块,并在控制代码中提供端口列表。但是,使用count时,它将为每个端口创建一个安全组。有没有办法解决这种情况下迭代特定部分的问题?

SG模块

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

  count = min(length(var.ingress_ports))
  ingress {
    from_port   = var.ingress_ports[count.index]
    to_port     = var.ingress_ports[count.index]
    protocol    = "tcp"
    cidr_blocks = ["10.0.0.0/8"]
  }

  egress {
    from_port       = 0
    to_port         = 0
    protocol        = "-1"
    cidr_blocks     = ["0.0.0.0/0"]
  }
}

控制代码

module "qliksense_sg" {
  source = "modules/aws-sg"

  sg_name = "My-SG"
  description = "A security group"
  vpc_id = module.vpc.vpc_id

  ingress_ports = ["80", "443"]
}
amazon-web-services terraform terraform-provider-aws aws-security-group
1个回答
0
投票

要在Terraform 0.12中执行此操作,可以使用dynamic blocks。实际上,该文档链接中给出的示例用于在端口列表上添加入口规则:

dynamic
© www.soinside.com 2019 - 2024. All rights reserved.