Terraform – 输出一个具有 `for_each` 值作为键的映射,同时进行资源创建

huangapple go评论51阅读模式
英文:

Terraform - Output a map with key as value of for_each while resource creation

问题

我正在尝试解决以下问题:

首先,根据提供给资源的列表条目创建资源。以下是我为此编写的Terraform代码:

resource "azurerm_key_vault" "application_key_vault" {
  foreach             = toset(var.app_names)
  name                = "${each.value}-kv"
  resource_group_name = azurerm_resource_group.aks_resource_group.name
  location            = var.location
  tenant_id           = local.tenant_id
  sku_name            = "standard"

  dynamic "contact" {
    for_each = var.key_vault_contact_emails
    content {
      email = contact.value
    }
  }

  network_acls {
    default_action = "Deny"
    bypass         = "AzureServices"
    virtual_network_subnet_ids = local.key_vault_allowed_subnets_set
  }

  tags = local.all_tags
  depends_on = [azurerm_resource_group.aks_resource_group]
}

现在,假设 "app_names" 具有以下值 ["app1", "app2", "app3"],并且创建的密钥保管库具有以下ID ["id1", "id2", "id3"]。是否有一种动态创建上述映射的方法,类似于这样:

{
 "app1" : "id1",
 "app2" : "id2",
 "app3" : "id3"
}

我尝试使用 "output",类似于以下方式,但无法弄清楚如何获取在创建每个密钥保管库中使用的 "app_name":

output "application_app_name_by_key_vault_id_map" {
  value = { for akv in azurerm_key_vault.application_key_vault : <not sure how to get app_name here> => akv.id }
}
英文:

I am trying to solve below :

At first, Create resources based on the entries of the list provided to the resource. Below is the tf code, i have written for it :

resource &quot;azurerm_key_vault&quot; &quot;application_key_vault&quot; {
  foreach             = toset(var.app_names)
  name                = &quot;${each.value}-kv&quot;
  resource_group_name = azurerm_resource_group.aks_resource_group.name
  location            = var.location
  tenant_id           = local.tenant_id
  sku_name            = &quot;standard&quot;

  dynamic &quot;contact&quot; {
    for_each = var.key_vault_contact_emails
    content {
      email = contact.value
    }
  }

  network_acls {
    default_action = &quot;Deny&quot;
    bypass         = &quot;AzureServices&quot;
    virtual_network_subnet_ids = local.key_vault_allowed_subnets_set
  }

  tags = local.all_tags
  depends_on = [azurerm_resource_group.aks_resource_group]
}

Now, lets say "app_names" has values ["app1", "app2", "app3"]. And the keyvaults created have ids ["id1", "id2", "id3"].
Is there a way i can create a map of above dynamically , which looks like this :

{
 &quot;app1&quot; : &quot;id1&quot;,
 &quot;app2&quot; : &quot;id2&quot;,
 &quot;app3&quot; : &quot;id3&quot;,
}

I tried using "output" something like this, but not able to figure out how should I get app_name which is used in creation of each keyvault :

output &quot;application_app_name_by_key_vault_id_map&quot; {
  value = { for akv in azurerm_key_vault.application_key_vault : &lt;not sure how to get app_name here&gt; =&gt; akv.id }
}

答案1

得分: 4

由于您正在使用 for_each 创建 azurerm_key_vault 资源,它的行为类似于任何其他键值映射。换句话说,您可以执行以下操作:

output "application_app_name_by_key_vault_id_map" {
  value = { for k, v in azurerm_key_vault.application_key_vault: k => v.id }
}
英文:

Since you are creating the azurerm_key_vault resource with for_each, it acts like any other key value map. In other words, you can do the following:

output &quot;application_app_name_by_key_vault_id_map&quot; {
  value = { for k, v in azurerm_key_vault.application_key_vault: k =&gt; v.id }
}

huangapple
  • 本文由 发表于 2023年2月16日 16:46:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定