【问题标题】:Inappropriate value for attribute "vpc_id": string required属性“vpc_id”的值不合适:需要字符串
【发布时间】:2021-06-18 03:22:41
【问题描述】:

我正在设置我的第一个 terraform 目标组,目前卡在中间。

这是我的资源代码

resource "aws_lb_target_group" "ami_tg" {
  name     = "ami-lb-tg-${substr(uuid(), 0, 3)}"
  port     = var.tg_port
  protocol = var.tg_protocol
  vpc_id   = var.vpc_id
  health_check {
    healthy_threshold   = var.elb_healthy_threshold
    unhealthy_threshold = var.elb_unhealthy_threshold
    timeout             = var.elb_timeout
    interval            = var.elb_interval
  }
}

显然我传递给了我的 variable.tf 文件

variable "tg_port" {}
variable "tg_protocol" {}
variable "elb_healthy_threshold" {}
variable "elb_unhealthy_threshold" {}
variable "elb_timeout" {}
variable "elb_interval" {}
variable "vpc_id" {}

然后添加到我的 root/main.tf 模块文件中

   module "alb" {
  source                  = "./alb"
  tg_port                 = 80
  tg_protocol             = "HTTP"
  vpc_id                  = module.networking.vpc_id
  elb_healthy_threshold   = 2
  elb_unhealthy_threshold = 2
  elb_timeout             = 3
  elb_interval            = 30
}

但是每当我运行 terraform plan 时,我都会收到以下错误;

Error: Incorrect attribute value type

  on alb/main.tf line 16, in resource "aws_lb_target_group" "ami_tg":
  16:   vpc_id   = var.vpc_id
    |----------------
    | var.vpc_id is object with 19 attributes

Inappropriate value for attribute "vpc_id": string required.

这里的任何建议将不胜感激

【问题讨论】:

  • module.networking.vpc_id 到底是什么?您的module.networking 中是如何定义的?
  • 这意味着我在网络目录中定义的 vpc 资源代码因此传递到 tg 目录,因为我创建了一个新目录,在这种情况下是 alb #-----network/main.tf ...资源“aws_vpc”“HH_vpc”{ cidr_block = var.vpc_cidr enable_dns_hostnames = true enable_dns_support = true tags = { Name = "HH-vpc ${random_integer.random.id}
  • 但是output vpc_id 是如何定义的呢?
  • #---networking/outputs.tf 输出 "vpc_id" { value = aws_vpc.HH_vpc }

标签: amazon-web-services terraform


【解决方案1】:

你的输出:

output "vpc_id" { 
    value = aws_vpc.HH_vpc
}

返回整个 HH_vpc 对象,而不仅仅是它的ID。只返回 id 它应该是:

output "vpc_id" { 
    value = aws_vpc.HH_vpc.id
}

【讨论】:

  • 这就是我无法弄清楚的 - 感谢您的帮助
  • @Hamdi 没问题。如果答案有帮助,我们将不胜感激。
  • 一个提示可以帮助在更接近其原因的情况下发现此类错误,即确保始终为每个变量声明预期的类型。对于这个特定的变量,您可以通过在 variable "vpc_id" 块内写入 type = string 来做到这一点。在这种情况下,Terraform 会在 module "alb" 块内报告问题,这仍然不是直接原因,但至少减少了您在调试时需要查看的配置部分。
猜你喜欢
  • 2021-02-09
  • 2021-06-23
  • 1970-01-01
  • 2021-11-27
  • 2019-12-28
  • 1970-01-01
  • 2021-02-17
  • 2020-04-09
  • 2020-08-09
相关资源
最近更新 更多