Gregor
Gregor

Reputation: 449

Terraform for loop get index

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

Upvotes: 9

Views: 15310

Answers (1)

Marcin
Marcin

Reputation: 239010

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.

Upvotes: 18

Related Questions