【问题标题】:Terraform reporting error in locals and variables in an attribute属性中的局部变量和变量中的 Terraform 报告错误
【发布时间】:2021-06-08 12:29:23
【问题描述】:

我有包含以下内容的 lookups.tf 文件

locals {
  roles_mapping = {
    web    = "we"
    app    = "as"
    db     = "db"
    queue  = "qu"
    stream = "st"
  }

  environment_lookup = {
    dev  = "d"
    test = "t"
    int  = "i"
    prod = "p"
    prd  = "p"
    uat  = "a"
    poc  = "d"
    dr   = "r"
    lab  = "l"
  }

  region_lookup = {
    "Australia East"      = "aae"
    "Australia Southeast" = "aas"
  }

  region_pairs = {
    "Australia East"      = "Australia Southeast"
    "Australia Southeast" = "Australia East"
  }

  lookup_result = lookup(var.environment_lookup, var.environment)

  tags = merge(
    data.azurerm_resource_group.tf-azrg.tags, {
      Directory      = "bento.com",
      PrivateDNSZone = var.private_dns_zone,
      Immutable      = "False",
      ManagedOS      = "True",
    }
  )
}

data "azurerm_log_analytics_workspace" "log_analytics" {
  name                = "abc-az-lad1"
  resource_group_name = "abc-dev-aae"
}

现在,我有一个名为 variables.tf

的文件
variable "environment" {
  description = "The name of environment for the AKS Cluster"
  type        = string
  default     = "dev"
}

variable "identifier" {
  description = "The identifier for the AKS Cluster"
  type        = number
  default     = 001
}

现在,我想在 ma​​in.tf 中引用 lookups.tfvariable.tf 的名称,如下所示

resource "azurerm_log_analytics_workspace" "tf-alaw" {
  name                = var.tla-la-local.lookup_result-var.identifier
  location            = data.azurerm_resource_group.tf-azrg.location
  resource_group_name = data.azurerm_resource_group.tf-azrg.name
  sku                 = var.la_sku
  retention_in_days   = 30
}

但是,我收到如下错误:

Error: Reference to the undeclared input variable

  on ..\..\..\terraform-azurerm-aks\lookups.tf line 32, in locals:
  32:   lookup_result = lookup(var.environment_lookup, var.environment)

An input variable with the name "environment_lookup" has not been declared.
This variable can be declared with a variable "environment_lookup" {} block.

Error: Reference to the undeclared input variable

  on ..\..\..\terraform-azurerm-aks\main.tf line 2, in resource "azurerm_log_analytics_workspace" "tf-alaw":
   2:   name                = var.tla-la-local.lookup_result-var.identifier

An input variable with the name "tla-la-local" has not been declared. This
variable can be declared with a variable "tla-la-local" {} block.

请注意下面的名称,我期望 var.tla 指的是一个变量,然后是字符串/文本 -la- 现在 local.lookup_result 是来自本地的值,然后是文本 - 最后引用另一个变量 var.identifier

【问题讨论】:

    标签: terraform


    【解决方案1】:

    在定义locals 块时,您必须使用不同的语法。应该添加它而不是您当前拥有的:

    lookup_result = lookup(local.environment_lookup, var.environment)
    

    更多信息和示例在这里:https://www.terraform.io/docs/language/values/locals.html#using-local-values

    EDIT1:我刚刚在错误输出下看到了您的注释。因此,当您连接多个变量时,您仍然必须使用“旧”语法,即类似于 Terraform 0.11.x 中使用的语法。以下是该值的外观:

    "${var.tla}-la-${local.lookup_result}-${var.identifier}"
    

    【讨论】:

    • 我意识到我的错误。我学到的是使用旧语法进行连接,第二件事是本地引用本地。以及要使用 var 引用的变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 2020-05-30
    • 2019-04-04
    • 2013-10-06
    • 2012-06-09
    相关资源
    最近更新 更多