Terraform for loop get index

Viewed 2589

I was wondering how can I get the index in a for loop?

I am working on NACL of my vpc:

[
    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
    }
  ]

And I was trying to place for the "rule_number" the index of the for loop.

*sorry for my bad english

1 Answers

You can get index by adding it in the for loop (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
    }
  ]

I assume that everything else is correct in your code.

Related