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

Terraform中如何遍历列表并在安全组规则中引用IP列表?

解决Terraform安全组规则引用列表变量及遍历问题

1. 直接将列表变量传入cidr_blocks

你不需要用count或element来把整个列表传给cidr_blocks,因为这个字段本身就支持接受列表类型的值,直接引用变量即可:

variable "ip_bitbucket" {
  type        = list(string)
  description = "Bitbucket的CIDR地址列表"
  default     = ["10.0.0.0/24", "192.168.1.0/24"]
}

resource "aws_security_group_rule" "bitbucket_access" {
  type              = "ingress"
  from_port         = 443
  to_port           = 443
  protocol          = "tcp"
  security_group_id = aws_security_group.example.id
  cidr_blocks       = var.ip_bitbucket # 直接引用整个列表
}

这样创建的安全组规则会包含列表里所有的CIDR地址。

2. 用count为每个CIDR创建单独规则

如果需要为每个CIDR单独生成一条安全组规则(方便后续单独管理),可以用count配合列表长度遍历:

resource "aws_security_group_rule" "bitbucket_access" {
  count             = length(var.ip_bitbucket) # 列表有多少项就创建多少条规则
  type              = "ingress"
  from_port         = 443
  to_port           = 443
  protocol          = "tcp"
  security_group_id = aws_security_group.example.id
  cidr_blocks       = [var.ip_bitbucket[count.index]] # 通过索引取对应元素
  # 早期写法用element:[element(var.ip_bitbucket, count.index)],效果一致但不如索引直观
}

3. Terraform中遍历列表的方法及等效"for循环"

Terraform没有传统的for循环语法,但可以用for表达式实现类似循环的遍历逻辑,生成新的列表、映射等结构:

基础遍历(输出所有项)

比如给原列表的每个CIDR添加注释,生成新列表:

output "formatted_cidrs" {
  value = [for cidr in var.ip_bitbucket : "${cidr} (Bitbucket IP)"]
}

这等效于传统循环逻辑:

new_list = []
for cidr in ip_bitbucket:
    new_list.append(f"{cidr} (Bitbucket IP)")

过滤遍历

只保留符合条件的项,比如筛选掩码为/24的CIDR:

output "filtered_cidrs" {
  value = [for cidr in var.ip_bitbucket : cidr if length(split("/", cidr)[1]) == "24"]
}

用for_each遍历资源

除了count,还可以用for_each遍历列表(需转为集合,确保元素唯一):

resource "aws_security_group_rule" "bitbucket_access" {
  for_each          = toset(var.ip_bitbucket) # 转集合保证元素唯一性
  type              = "ingress"
  from_port         = 443
  to_port           = 443
  protocol          = "tcp"
  security_group_id = aws_security_group.example.id
  cidr_blocks       = [each.value]
}

这种方式比count更稳定,元素顺序变化不会触发资源重建,而count基于索引,顺序变动会导致资源重新创建。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 17:55:30