【问题标题】:Terraform output error: Error: Variables not allowedTerraform 输出错误:错误:不允许使用变量
【发布时间】:2022-11-19 09:27:48
【问题描述】:

我真的不确定为什么会看到这个错误。我知道这是简单而正确的事情。我的问题是我如何传递 subnet_mapping 块中的 subnet_id。这是下面的代码:

main.tf

resource "aws_lb" "lb" {
  name                             = var.name
  internal                         = var.internal
  load_balancer_type               = var.lb_type
  enable_cross_zone_load_balancing = var.enable_cross_zone_load_balancing

  subnet_mapping {
    allocation_id                  = aws_eip.lb.id
    subnet_id                      = var.subnet_id[1]
  }
}

resource "aws_eip" "lb" {
  vpc                              = true
}

variables.tf

variable "name" {
  type        = string
}

variable "internal" {
  type        = bool
  default     = false
}

variable "lb_type" {
  type        = string
  default     = "network"
}

variable "enable_cross_zone_load_balancing" {
  type        = bool
  default     = true
}

variable "vpc" {
  type        = bool
  default     = true
}

variable "vpc_id" {
  type        = string
}

variable "subnet_id" {
  type        = list(string)
  default     = []
}

terragrunt.hcl

include {
  path = find_in_parent_folders()
}

dependency "test" {
  config_path = "../../../folder/test"
  mock_outputs = {
    vpc_id            = "vpc-12345"
    public_subnet_ids = ["subnet-1", "subnet-2"]
  }
}

# var to pass in to use the module specified in the terragrunt configuration above
inputs = {
  vpc_id    = dependency.test.outputs.vpc_id
  subnet_id = dependency.test.outputs.public_subnet_ids[1]
  xxx...

错误

Error: Variables not allowed

 on <value for var.subnet_id> line 1:
 (source code not available)

Variables may not be used here.

我会很感激一些反馈。过去几个小时一直很痛。

【问题讨论】:

    标签: terraform terragrunt


    【解决方案1】:

    这个错误消息非常具有误导性,正如几个people raising the issue on GitHub 所证明的那样。在你的情况下,问题是你有一个名为subnet_id 的变量,并且你还在输入块中使用了相同的标识符(在subnet_id = dependency.test.outputs.public_subnet_ids[1] 中)。重命名其中之一应该可以解决问题。

    【讨论】:

    • thx for the note,我已经按照您在 terragrunt 文件中的建议将 subnet_id 更新为 subnet,其余部分保持原样。main.tf 第 20 行的新错误```,在资源“aws_lb”“lb”中": 20: subnet_id = var.subnet_id[1] |---------------- | var.subnet_id 是空字符串列表给定的键未标识此集合值中的元素。 ```
    • 将变量传递给模块时,必须确保提供子网 ID 列表。如果您将 variable "subnet_id" 重命名为 variable "subnet",则必须更新您的模块实例化代码以使用 subnet 而不是 subnet_id
    【解决方案2】:

    很难确切地看到这里发生了什么,因为 Terragrunt 正在运行带有一些生成的命令行参数的 Terraform CLI,但是您无法从它的输出中轻松地确切地看到它正在运行什么命令。

    我怀疑 Terragrunt 正在使用如下命令行运行 Terraform:

    terraform apply -var='vpc_id=vpc-12345' -var='subnet_id=subnet_1'
    

    此参数针对 vpc_id 的格式正确,因为您已将其声明为 type = string,但您已将 subnet_id 声明为 list(string),因此 Terraform 期望将列表表达式视为该参数的值,像这样:

    terraform apply -var='vpc_id=vpc-12345' -var='subnet_id=["subnet-1","subnet-2"]'
    

    Terraform 报告“不允许使用变量”,因为当解析为表达式而不是原始字符串时,subnet_1 看起来像是对名为 subnet_1 的符号的引用,而不是文字值。

    如果我对原因的看法是正确的(我可能不是,因为我只是根据症状进行猜测),那么我认为您有两种不同的选择来解决它。你应该选择只有一个以下两个选项:

    1. 改为将变量更改为 type = string,然后 Terraform 会将 -var='subnet_id=subnet_1' 理解为将字符串 "subnet_1" 分配给变量。
    2. 将变量保留为list(string),但更改 Terragrunt 配置以将变量作为列表而不是字符串传递。我不太了解 Terragrunt,无法确切知道如何实现这一点,但我首先要尝试的是 subnet_id = dependency.test.outputs.public_subnet_ids 来分配整个子网 ID 列表,而不仅仅是第一个元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-30
      • 2018-02-13
      • 1970-01-01
      • 2018-01-03
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多