Azure terraform报告缺少资源实例密钥

Azure terraform报告缺少资源实例密钥,terraform,terraform-provider-azure,Terraform,Terraform Provider Azure,我有一个地形模块,如下所示 output "resource_gp" { value = "${azurerm_resource_group.test1[count.index]}" } main.tf resource "azurerm_resource_group" "test1"{ count = "${length(var.azurerm_resource_group_name)}" name = "${element(var.azurerm_re

我有一个地形模块,如下所示

output "resource_gp" {
    value = "${azurerm_resource_group.test1[count.index]}"
}
main.tf

resource "azurerm_resource_group" "test1"{
    count    = "${length(var.azurerm_resource_group_name)}"
    name     = "${element(var.azurerm_resource_group_name, count.index)}"
    location = "${element(var.azurerm_resource_group_location, count.index)}"
}
我对output.tf的定义如下

output "resource_gp" {
    value = "${azurerm_resource_group.test1[count.index]}"
}
对于同一模块,变量。tf如下所示:

variable "azurerm_resource_group_name" {
    type = "list"
    default = ["SimpleMoon-Terra"]
}
variable "azurerm_resource_group_location" {
    type = "list"
    default = ["Central US"]
}
调用模块,如下所示:

variable "azurerm_resource_group" {

    default={
    name     = "simpletestrg"
    location = "Central US"
    }
}

# Create Resource Group
module "Test_Resource_Groups" {
    source ="../../Modules/Test_Resource_Groups"
    #name                ="${var.azurerm_resource_group.name}"
    #location            ="${var.azurerm_resource_group.location}"
}
Error: Missing resource instance key

  on ../../Modules/Test_Resource_Groups/output.tf line 2, in output "resource_gp":
   2:     value = "${azurerm_resource_group.test1.name[count.index]}"

Because azurerm_resource_group.test1 has "count" set, its attributes must be
accessed on specific instances.

For example, to correlate with indices of a referring resource, use:
    azurerm_resource_group.test1[count.index]
但是,我得到如下错误:

variable "azurerm_resource_group" {

    default={
    name     = "simpletestrg"
    location = "Central US"
    }
}

# Create Resource Group
module "Test_Resource_Groups" {
    source ="../../Modules/Test_Resource_Groups"
    #name                ="${var.azurerm_resource_group.name}"
    #location            ="${var.azurerm_resource_group.location}"
}
Error: Missing resource instance key

  on ../../Modules/Test_Resource_Groups/output.tf line 2, in output "resource_gp":
   2:     value = "${azurerm_resource_group.test1.name[count.index]}"

Because azurerm_resource_group.test1 has "count" set, its attributes must be
accessed on specific instances.

For example, to correlate with indices of a referring resource, use:
    azurerm_resource_group.test1[count.index]

我无法理解实际错误是什么以及如何纠正它。

错误消息中显示的源代码片段与您为
输出共享的源代码不一致。tf

    value = "${azurerm_resource_group.test1.name[count.index]}" # in the error message
    value = "${azurerm_resource_group.test1[count.index]}"      # in the given source code
这里有几个问题,其中一个问题只存在于错误消息中显示的源代码中:


  • 因为
    azurerm\u resource\u group.test1
    设置了
    count
    集合,所以它在表达式中显示为对象列表。因此,应用于它的第一个操作必须是索引操作,给出类似于
    azurerm_资源组.test1[count.index].name的表达式:获取
    azurerm_资源组.test1
    列表的
    name
    属性

  • 您不能在输出值表达式中使用
    count.index
    ,因为在该表达式的作用域中没有
    count
    参数来知道要使用的
    count.index
    值。如果您的目标是返回所有资源组的所有名称的列表,那么可以将其写成
    azurerm_resource_group.test1[*].name
    ,这是一个

在这种特殊情况下,
output“resource\u gp”
的值将始终与
var.azurerm\u resource\u group\u name
的值相同,因为名称是从那里填充的。然而,通过资源的这种引用是有用的,因为它告诉Terraform,任何引用该输出值的东西都必须依次依赖于
azurerm_resource_group.test1
,从而有助于确保以适当的顺序应用所有操作



有一种不同的方法来编写您在此处编写的内容,这种方法可以在初始创建时获得类似的结果,但对于后续对
var.azurerm\u resource\u group\u name
的更改也会更好

首先,我们将变量定义为对象的映射,以便我们只能使用单个变量来指定资源组

variable "azurerm_resource_groups" {
  type = map(object({
    location = string
  }))
  default = {
    SimpleMoon-Terra = {
      location = "Central US"
    }
  }
}
然后,我们可以在定义资源组时使用,而不是:

resource "azurerm_resource_group" "test1"{
  for_each = var.azurerm_resource_groups

  name     = each.key
  location = each.value.location
}

除了使表达式更简单外,这还有另一个重要的作用:Terraform将使用地址标识资源组,例如
azurerm\u resource\u group.test1[“SimpleMon Terra”]
,而不是
azurerm\u resource\u group.test1[0]
,因此,当您从
var.azurerm\u资源组添加和删除项目时,Terraform可以跟踪哪个远程资源组对象属于地图的哪个元素


for_each
还会导致
azurerm_resource_group.test1
在表达式中以映射而不是列表的形式出现,因此我们需要更改输出值
来处理它。因为映射键是资源组名称,所以我们可以使用来获取它们。我们还将使用将其转换为一个集合,反映出这些集合不是以任何特定顺序排列的,因此此值的用户不应依赖于顺序:

output "resource_group_names" {
  value = toset(keys(azurerm_resource_group.test1))
}
然后,您的调用模块可以调用此模块,如以下示例所示:

module "test_resource_groups" {
  source = "../../Modules/Test_Resource_Groups"

  azurerm_resource_groups = {
    simpletestrg = {
      location = "Central US"
    }
  }
}

在该调用模块中,对
module.test\u resource\u groups.resource\u group\u names
的引用将生成一组资源组名称,然后可与配置中其他位置的每个
一起使用,以便为每个资源组生成一个对象。

var.azurerm(资源)组(名称)
这必须是
var.azurerm(资源)组
?在main.tfI中,我不这么认为,我现在为模块添加了variables.tf文件。请再查一下。Ty
azurerm\u资源组。test1
是一个资源,而不是导出的属性。您试图输出什么导出属性?真是令人惊讶的响应。我还没消化。我将在这方面做更多的练习,并询问是否需要进一步的帮助。