简体   繁体   English

Terraform 输出:如何创建(地图?)以及如何使用 map? map 是正确的工具吗?

[英]Terraform Outputs: How do I create a (map?) and how do I use the map? Is a map even the right tool?

I am struggling with a few terraform concepts.我正在努力解决一些 terraform 概念。

I am successfully using the aztfmod/azurecaf provider to name my resourcegroup, but this means I need to get that name as an output for the compan.net.resource_group module, so that I can use that name again when calling the compan.net.key_vault module.我成功地使用aztfmod/azurecaf提供程序来命名我的资源组,但这意味着我需要为compan.net.resource_group模块获取该名称作为 output,以便我可以在调用compan.net.key_vault模块。

# terraform.tfvars

resource_groups = {
  rg1 = { 
    name     = "resourcegroup1"
    location = "eastus"
  }
  rg2 = { 
    name     = "resourcegroup2"
    location = "eastus"
  }
}
# root main.tf

provider "azurerm" {
  features {}
}

module "companynet" {
  source = "./modules/companynet"

  tenant_id         = var.tenant_id
  environment       = var.environment
  resource_groups   = var.resource_groups
  key_vaults        = var.key_vaults
  storage_accounts  = var.storage_accounts
  app_service_plans = var.app_service_plans
}
# modules/companynet/main.tf

module "resource_group" {
  source          = "../companynet.resource_group"
  environment     = var.environment
  resource_groups = var.resource_groups
}

module "key_vault" {
 source          = "../companynet.key_vault"
 tenant_id       = var.tenant_id
 environment     = var.environment
 resource_groups = "${module.resource_group.resource_groups.companynet}"
 key_vaults      = var.key_vaults
 
}

The module resource_group has the following main.tf :模块resource_group具有以下main.tf

# modules/companynet.resource_group/main.tf

resource "azurecaf_name" "resource_group" {
  for_each      = var.resource_groups
  name          = each.value.name
  resource_type = "azurerm_resource_group"
  suffixes      = ["${var.environment}", "001"]
}

resource "azurerm_resource_group" "resource_group" {
  for_each = var.resource_groups
  name     = azurecaf_name.resource_group[each.key].result
  location = each.value.location

}

but I don't know how to get the output of that resource_group name.但我不知道如何获取该资源组名称的 output。

I have tried a few different things that do not work我尝试了一些不同的方法,但都不起作用

# modules/companynet.resource_group/outputs.tf

output "resource_groups" {
 value = azurerm_resource_group.resource_group[*].name
}

value = azurerm_resource_group.resource_group.name

value = azurerm_resource_group.resource_group.compan.net.name

value = azurerm_resource_group.resource_group[compan.net].name

Each of these results in one error or another, all indicating a problem with modules/compan.net.resource_group/outputs.tf这些中的每一个都会导致一个或另一个错误,都表明modules/compan.net.resource_group/outputs.tf有问题

Ideally I would get an object that I can then iterate through in another module.理想情况下,我会得到一个 object,然后我可以在另一个模块中迭代它。 I expect to be able to call something like to get access to those resource group names in other modules such as:我希望能够调用类似的东西来访问其他模块中的那些资源组名称,例如:

# modules/companynet.key_vault/main.tf

resource "azurerm_key_vault" "key_vault" {
  for_each            = var.key_vaults
  name                = azurecaf_name.key_vault[each.key].result
  location            = var.resource_groups.location

  resource_groups     = "${module.resource_group.resource_groups.[companynet]}"

  sku_name            = "standard"
  tenant_id           = var.tenant_id
}

azurerm_resource_group.resource_group is declared with for_each , and so that expression refers to a map of objects where the keys match the keys of the for_each expression and the values are the corresponding declared resource instances. azurerm_resource_group.resource_group是用for_each声明的,因此该表达式引用 map 个对象,其中键与for_each表达式的键匹配,值是相应声明的资源实例。

In References to Resource Attributes there are various examples of referring to resource attributes in different situations, including the following about resources using for_each :References to Resource Attributes中有各种在不同情况下引用资源属性的示例,包括以下有关使用for_each的资源:

When a resource has the for_each argument set, the resource itself becomes a map of instance objects rather than a single object, and attributes of instances must be specified by key, or can be accessed using a for expression .当资源设置了for_each参数时,资源本身变为实例对象的 map 而不是单个 object,并且实例的属性必须由键指定,或者可以使用for表达式访问。

  • aws_instance.example["a"].id returns the id of the "a"-keyed resource. aws_instance.example["a"].id返回以“a”为键的资源的 ID。
  • [for value in aws_instance.example: value.id] returns a list of all of the ids of each of the instances. [for value in aws_instance.example: value.id]返回每个实例的所有 ID 的列表。

That second item shows how to use a for expression to produce a list of the ids of aws_instance.example , but it doesn't show exactly how to produce a map and instead expects you to refer to the linked documentation about for expressions to learn about that:第二项显示了如何使用for表达式生成aws_instance.example的 id列表,但它并未准确显示如何生成 map 而是希望您参考有关for表达式的链接文档以了解那:

The type of brackets around the for expression decide what type of result it produces. for表达式周围括号的类型决定了它产生的结果类型。

The above example uses [ and ], which produces a tuple.上面的示例使用[],它生成一个元组。 If you use { and } instead, the result is an object and you must provide two result expressions that are separated by the => symbol:如果您改用{} ,则结果为 object,您必须提供两个由=>符号分隔的结果表达式:

 {for s in var.list: s => upper(s)}

This expression produces an object whose attributes are the original elements from var.list and their corresponding values are the uppercase versions.此表达式生成一个 object,其属性是var.list中的原始元素,它们的对应值是大写版本。 For example, the resulting value might be as follows:例如,结果值可能如下所示:

 { foo = "FOO" bar = "BAR" baz = "BAZ" }

A for expression alone can only produce either an object value or a tuple value, but Terraform's automatic type conversion rules mean that you can typically use the results in locations where lists, maps, and sets are expected.单独的for表达式只能生成 object 值或元组值,但 Terraform 的自动类型转换规则意味着您通常可以在需要列表、映射和集合的位置使用结果。

This section describes how to produce an object and then notes that you can use the result in a location where a map is expected.本节介绍如何生成 object,然后说明您可以在需要 map 的位置使用结果。 In practice it's often possible to use object-typed values and mapped-type values interchangeably in Terraform, because they both have in common that they have elements identified by string keys.在实践中,通常可以在 Terraform 中互换使用对象类型值和映射类型值,因为它们的共同点是它们都具有由字符串键标识的元素。 The difference is that an object type can have a separate type for each of its attributes, whereas a map must have the same type for all attributes.区别在于 object 类型的每个属性可以有一个单独的类型,而 map 的所有属性必须有相同的类型。

Given all of this information, we can produce an object value describing the names for each resource group like this:鉴于所有这些信息,我们可以生成一个object值来描述每个资源组的名称,如下所示:

output "resource_groups" {
  value = { for k, g in azurerm_resource_group.resource_group : k => g.name }
}

For most purposes it doesn't really matter that this is an object-typed result rather than specifically a map, but since we know that .name is always a string we can infer that all of the attributes of this object have string-typed values, and so it would also be valid to explicitly convert to a map of strings using the tomap function (which is a "location where [...] maps [...] are expected", per the above documentation):对于大多数用途来说,这是一个对象类型的结果而不是特定的 map 并不重要,但是由于我们知道.name始终是一个字符串,我们可以推断出这个 object 的所有属性都具有字符串类型的值,因此使用tomap function显式转换为 map 字符串也是有效的(根据上述文档,这是“预期 [...] 映射 [...] 的位置”):

output "resource_groups" {
  value = tomap({
    for k, g in azurerm_resource_group.resource_group : k => g.name
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 AsyncSnapshot 数据转换为 Map? - How do I turn AsyncSnapshot data into a Map? 如何在不覆盖整个 Map 的情况下将新的 Map 条目添加到 Map? - How do I add a new Map entry to a Map without overwriting the entire Map? 如何使用 terraform 中的 map 创建多个不同大小的 ebs 卷? - How can I create several ebs volumes each of a different size using a map in terraform? 如何遍历 terraform 中的 map 变量 - How can I iterate through a map variable in terraform 如何在 React 中使用 Map function 渲染 Firestore 集合? - How do I render Firestore collection using the Map function in React? 如何将用户的 Firestore Map 读取为 Swift 字典? - How do I read a User's Firestore Map to a Swift Dictionary? 在使用 map 渲染这些元素时,如何在每个元素上使用类似和不同的功能? - How do I use like & unlike functionality on every element while using map to render those elements? 如何为 Terraform 配置 AWS MFA? - How do I configure AWS MFA for Terraform? 如何在 terraform 中的“su.net_mapping”中使用“for_each”,这样我就可以 map 为每个“su.net_id”创建的每个弹性 IP - How to use "for_each" inside the "subnet_mapping" in terraform so I can map the each elastic IPs created to each "subnet_id" 如何强制 terraform 重新创建资源? - How do I force terraform to recreate a resource?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM