【问题标题】:Terraform_Remote_State Error: This object has no argument, nested block, or exported attribute named "vpc_id"Terraform_Remote_State 错误:此对象没有参数、嵌套块或名为“vpc_id”的导出属性
【发布时间】:2021-03-29 11:58:35
【问题描述】:

在 AWS 中使用 Terraform (TF) 并在尝试使用 terraform_remote_state 调用 vpc_id 时遇到错误。我们分割了网络的不同部分以减轻状态滑移。但是,它还需要与基础设施的每个单独部分的状态文件(即 vpc、sgs、角色等的状态文件)进行交互。当我尝试从 S3 存储桶中保存的状态文件中获取 vpc_id 时,我收到以下错误:


  on main.tf line 78, in module "vpc_sg":
  78:   vpc_id = data.terraform_remote_state.vpc.vpc_id

This object has no argument, nested block, or exported attribute named
"vpc_id".

这是我在 main.tf 文件中的 terraform_remote_state 调用:

  backend = "s3"
  workspace = var.workspace
  config = {
    bucket = "terraform-east"
    key = "terraform-vpc.tfstate"
    region ="us-east-1" 
  }
}

这是同一个 main.tf 中的调用

  // output "sg_id"
  source = "git::https://url_to_sg.git/reference?
  vpc_cidr = data.terraform_remote_state.vpc.vpc_cidr -- This line also doesn't work.. but same issue.
  *vpc_id = data.terraform_remote_state.vpc.vpc_id* -- Here's the troublesome line.
  deployment_name = "*redacted*"
  workspace = var.workspace
  tags = merge(
    local.common_tags,
    map(
      "Name", "vpc_sg-${var.workspace}",
      "module", "vpc_sg"
    )
  )
}

这是 vpc 目录中的 outputs.tf 文件:

output "vpc_id" {
  value       = module.vpc.vpc_id
  description = "The VPC ID"
}

output "vpc_cidr" {
  value       = var.vpc_cidr
  description = "The VPC CIDR block"
}

这是 tf_remote_state 声明:

data "terraform_remote_state" "vpc" {
    backend = "s3"
    workspace = var.workspace
    config = {
         bucket = "correct bucket (trust me)
         key = "correct key"
         region = us-east-1
    }
}

最后,这是来自 vpc 目录的 backend.tf 信息:

terraform {
    backend "s3" {
        bucket = "terraform-east"
        key = "terraform-vpc.tfstate"
        region ="us-east-1" 
        }
}

我尝试过使用 output.vpc_id(如上),没有输出(tf 0.12 更新后需要输出),使用 output.vpc_id.value(因为 statefile 使它看起来像这样的结构),以及使用输出[1].value.. 它给出了不同的错误,但它们都失败了。帮助将不胜感激。

【问题讨论】:

  • 值为module.vpc.vpc_id的输出块的名称是什么?
  • 哎呀。它的输出“vpc_id”{

标签: amazon-web-services amazon-s3 terraform terraform-remote-state


【解决方案1】:

terraform_remote_state 数据源在名为 outputs 的属性下导出远程状态的输出,该属性本身是一个对象值,每个输出值包含一个属性。

因此,要特别引用 vpc 输出值,您需要编写:

data.terraform_remote_state.outputs.vpc

...然后从嵌套的vpc 对象中获取vpc_id 属性:

data.terraform_remote_state.outputs.vpc.vpc_id

【讨论】:

  • 好的,我试过了。但是,看起来我忽略了说明我是如何声明数据源调用的。 data "terraform_remote_state" "vpc" { backend = "s3" workspace = var.workspace config = { bucket = "correct bucket (trust me) key = "correct key" region = us-east-1 } }
  • 我继续,只是将代码添加到原始帖子中。. 格式在评论中搞砸了。
  • 其实是data.terraform_remote_state.vpc.outputs.vpc_id
  • 这是错误的。就像 Diogo 说的,它是:data.terraform_remote_state.vpc.outputs.vpc_id
【解决方案2】:

在 terraform v 0.12 中引用状态值的方式发生了变化。请参阅此处的参考:

https://www.terraform.io/upgrade-guides/0-12.html#remote-state-references

来自指南:

在以前的版本中,对 vpc_id 输出的引用由 远程状态数据源可能看起来像这样:

data.terraform_remote_state.vpc.vpc_id

现在必须通过新的输出属性访问此值:

data.terraform_remote_state.vpc.outputs.vpc_id

【讨论】:

    【解决方案3】:

    事实证明这很简单。需要确保已建立 VPN 连接。 -_- 感谢观看!

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 2021-05-07
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多