Terraform,在我的本地人中使用 yamldeode() returns 错误的字符串

Terraform, using yamldeode() in my locals returns the wrong string

我正在创建一个遍历局部变量的 eventGrid 主题,它们通过 yamldecode 函数从 yaml 文件导入。

locals {
  app_name  = yamldecode(file("config.yaml"))["name"]
  version   = yamldecode(file("config.yaml"))["version"]
  functions = yamldecode(file("config.yaml"))["functions"]
}

resource "azurerm_eventgrid_topic" "function" {

  count = length(local.functions)

  name     = "topic-${local.functions[count.index]["name"]}"
  resource_group_name = azurerm_resource_group.core.name
  location = azurerm_resource_group.core.location

  depends_on = [
    null_resource.functions
  ]
}

返回的错误显示名称错误,这让我假设它以某种方式没有从本地变量获取输入,我做错了什么?

错误信息:

│ Error: invalid value for name (EventGrid topic name must be 3 - 50 characters long, contain only letters, numbers and hyphens.)
│ 
│   with azurerm_eventgrid_topic.function[2],
│   on main.tf line 66, in resource "azurerm_eventgrid_topic" "function":
│   66: resource "azurerm_eventgrid_topic" "function" {

这是 yaml 文件的样子

有什么想法吗?感谢您的帮助

我测试了你的代码并且它有效,你可以通过下面的 运行 自己尝试:

locals {
  app_name  = yamldecode(file("config.yaml"))["name"]
  version   = yamldecode(file("config.yaml"))["version"]
  functions   = yamldecode(file("config.yaml"))["functions"]
}

resource "null_resource" "test" {
  count = length(local.functions)

  provisioner "local-exec" {
    command = "echo topic-${local.functions[count.index]["name"]} >> test.out"
  }
  triggers = {
  build_number = timestamp()
}
}

output "null" {
  value = null_resource.test
}

所以问题出在事件网格主题名称上。我查看了 Microsoft 文档,主题名称有限制。它不能有下划线,它可以有连字符。 https://docs.microsoft.com/en-us/azure/event-grid/troubleshoot-errors 我注意到您在名称中使用了下划线。