Terraform - 在 for_each 循环中递增变量

Dan*_*ton 1 azure terraform terragrunt terraform-provider-azure

我正在尝试找出一种向资源添加递增数字的方法。

这是我的代码片段。我想让优先级成为一个递增的数字,而不是传递一个固定的数字。

当前代码


resource "azurerm_firewall_network_rule_collection" "netrc" {
   for_each            = {for network_rule_collection in var.network_rule_collections: network_rule_collection.name => network_rule_collection}
   name                = "netrc-${each.key}"
   azure_firewall_name = var.afw_name
   resource_group_name = var.resource_group_name
   priority            = var.priority
   action              = each.value.action

Run Code Online (Sandbox Code Playgroud)

基本上,我想看起来像这样:

     priority            = (each.index *  10) + 140
Run Code Online (Sandbox Code Playgroud)

我尝试使用each.key,但在这个模块中,each.key是一个字符串。

我也尝试了计数器,但不能将计数器与 for_each 循环结合起来。

有什么想法或建议吗?

编辑

我最终按照下面的建议使用了索引。

  priority            = (index(var.application_rule_collections, each.value) + 100)
Run Code Online (Sandbox Code Playgroud)

Vol*_*hat 6

我认为你可以尝试使用索引

priority = index(var.network_rule_collections, each.value)  + 140
Run Code Online (Sandbox Code Playgroud)

PS:您可能需要修改它,这只是一个例子