合并 terraform 局部变量

rda*_*pan 7 terraform

你能指导我如何结合当地人吗?尽管一些在线文档建议以这种方式构建,但这是行不通的。感谢您的帮助,谢谢!

# see README.md for developer guide
# prepare subscription where resources are created
locals {
  location_code           = "weu"
  environment_code        = "test"
}

locals {
  kv_name = "oamp-kv-${local.environment_code}-${location_code}"
  ai_name = "oamp-ai-${local.environment_code}-${location_code}"
}

# prepare azure rm configuration
provider "azurerm" {
    version = "~>2.15.0"
    use_msi = true
    features {}
}
Run Code Online (Sandbox Code Playgroud)

验证

Error: Invalid reference

  on main.tf line 20, in locals:
  20:   kv_name = "oamp-kv-${local.environment_code}-${location_code}"

A reference to a resource type must be followed by at least one attribute
access, specifying the resource name.

Run Code Online (Sandbox Code Playgroud)

Tho*_*ous 9

您可以按照文档中的说明将局部变量引用到局部变量

如上所示,可以使用 local.common_tags 等表达式从模块中的其他位置引用本地值,并且本地值可以互相引用,以便从更简单的值构建更复杂的值。

该错误来自于您需要使用属性前缀来访问您的资源。但是您没有添加前缀location_code

local在您的代码中,您错过了在 之前添加访问属性前缀location_code

您需要做的是正确地为变量添加前缀:

# see README.md for developer guide
# prepare subscription where resources are created
locals {
  location_code           = "weu"
  environment_code        = "test"
}

locals {
  kv_name = "oamp-kv-${local.environment_code}-${local.location_code}"
  ai_name = "oamp-ai-${local.environment_code}-${local.location_code}"
}

# prepare azure rm configuration
provider "azurerm" {
    version = "~>2.15.0"
    use_msi = true
    features {}
}
Run Code Online (Sandbox Code Playgroud)

  • 这确实只是一个错字。我不确定它是否值得作为问题/答案组合保留,因此我投票决定关闭它。 (2认同)