terraform - Azure terraform 报告缺少资源实例密钥
问题描述
我有一个像下面这样的 Terraform模块
主文件
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]}"
}
对于同一个模块,variables.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]
我无法理解实际的错误是什么以及如何纠正它。
解决方案
您的错误消息中显示的源代码片段与您共享的源代码不一致outputs.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_resource_group.test1
已经count
设置,所以它在表达式中显示为对象列表。因此,应用于它的第一个操作必须是索引操作,给出如下表达式azurerm_resource_group.test1[count.index].name
:获取列表元素的name
属性。count.index
azurerm_resource_group.test1
- 您不能
count.index
在输出值表达式中使用,因为count
范围内没有参数可以知道count.index
要使用的值。如果您的目标是返回所有资源组的所有名称的列表,则可以将其写为azurerm_resource_group.test1[*].name
,这是一个splat 表达式。
在这种特殊情况下, 的值output "resource_gp"
将始终与 的值相同var.azurerm_resource_group_name
,因为名称是从那里填充的。但是,通过资源进行这样的引用很有用,因为它告诉 Terraform 引用该输出值的任何内容都必须反过来依赖于azurerm_resource_group.test1
,因此有助于确保所有操作都以适当的顺序应用。
有一种不同的方式来编写您在此处编写的内容,这将在初始创建时获得类似的结果,但对于后续更改var.azurerm_resource_group_name
.
首先,我们将变量定义为对象映射,以便我们只能使用单个变量来指定资源组。
variable "azurerm_resource_groups" {
type = map(object({
location = string
}))
default = {
SimpleMoon-Terra = {
location = "Central US"
}
}
}
然后我们可以在定义资源组时使用for_each
而不是:count
resource "azurerm_resource_group" "test1"{
for_each = var.azurerm_resource_groups
name = each.key
location = each.value.location
}
除了使表达式更简单之外,这还有另一个重要的效果:Terraform 将使用类似的地址azurerm_resource_group.test1["SimpleMoon-Terra"]
而不是来识别资源组azurerm_resource_group.test1[0]
,因此当您从var.azurerm_resource_groups
Terraform 添加和删除项目时,可以跟踪哪个远程资源组对象属于哪个元素地图。
for_each
还会导致azurerm_resource_group.test1
在表达式中以映射而不是列表的形式出现,因此我们需要更改输出value
以使用它。因为映射键是资源组名称,所以我们可以使用函数keys
来获取它们。我们还将使用它toset
来将其转换为一个集合,以反映它们没有任何特定的顺序,因此该值的用户不应依赖于排序:
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_resource_groups.resource_group_names
将生成一组资源组名称,然后可以将其与for_each
配置中的其他位置一起使用,从而为每个资源组生成一个对象。