Terragrunt - 不支持的属性 --- 但 tfstate 具有该属性

Terragrunt - Unsupported attribute --- but tfstate has the attribute

我想不通,已经发生过几次了,这次我无法解决。

我从 terragrunt 得到的错误是

terragrunt.hcl:25,47-50: Unsupported attribute; This object does not have an attribute named "id".

我的问题是我要引用的 id 存在于 tfstate 中,为什么它看不到它?

下面是缩短的代码 terragrunt.hcl

dependency "vm01" {
  config_path = "../vm01"
}

include {
  path = find_in_parent_folders()
}

terraform {
  source = "//core/azurerm_virtual_machine_extension"
}

locals {
  vm_name = "vm01"
}

inputs = {
  name                  = local.vm_name
  virtual_machine_id    = dependency.vm01.id
.....
}

output.hcl

output "id" {
  value = azurerm_windows_virtual_machine.vm.id
}

直接从后端存储账户拉取的tfstate

{
  "version": 4,
  "terraform_version": "0.14.7",
  "serial": 4,
  "lineage": "abcde-guid-abcde",
  "outputs": {
    "id": {
      "value": "/subscriptions/abcde-guid-abcde/resourceGroups/rg-name/providers/Microsoft.Compute/virtualMachines/vm01",
      "type": "string"
    }
  }
...........
}

感谢 terraform github 的 yorinasub17 我找到了答案。

The outputs are nested under outputs, so the path to id is dependency.vm01.outputs.id, not dependency.vm01.id as you are referencing in the code snippet. See the dependency block reference for more info.

由于 对于这种情况是正确的,它也可能由于输出不可用而发生:

在您的 ../vm01 依赖项中,您需要先 terra grunt refresh 然后输出将在您的模块中可用