Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform for loop get index

Tags:

terraform

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

like image 459
Gregor Avatar asked May 13 '26 15:05

Gregor


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.

like image 178
Marcin Avatar answered May 16 '26 03:05

Marcin