Terraform for 循环获取索引

Gre*_*gor 8 terraform

我想知道如何在 for 循环中获取索引?

我正在研究我的 vpc 的 NACL:

[
    for myIpList in data.terraform_remote_state.globals.outputs.myIpRanges:
    {
      # allow inbound traffic for IPv6
      "ipv6_cidr_block": myIpList,
      "from_port": 0,
      "protocol": -1,
      "rule_action": "allow",
      "rule_number": (here  I would like to insert the index of the for loop),
      "to_port": 0
    }
  ]
Run Code Online (Sandbox Code Playgroud)

我试图为“rule_number”放置 for 循环的索引。

*对不起,我的英语不好

Mar*_*cin 16

您可以通过将其添加到 for 循环 ( index) 中来获取索引:

[
    for index, myIpList in data.terraform_remote_state.globals.outputs.myIpRanges:
    {
      # allow inbound traffic for IPv6
      "ipv6_cidr_block": myIpList,
      "from_port": 0,
      "protocol": -1,
      "rule_action": "allow",
      "rule_number": index,
      "to_port": 0
    }
  ]
Run Code Online (Sandbox Code Playgroud)

我假设您的代码中的其他所有内容都是正确的。