amazon-web-services - Terraform_Remote_State 错误 : This object has no argument, 嵌套 block ,或名为 "vpc_id"的导出属性

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

在 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" 
        }
}

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

最佳答案

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

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

data.terraform_remote_state.outputs.vpc

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

data.terraform_remote_state.outputs.vpc.vpc_id

关于amazon-web-services - Terraform_Remote_State 错误 : This object has no argument, 嵌套 block ,或名为 "vpc_id"的导出属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65363668/

相关文章:

Terraform - 使用本地命令结果作为 tf 文件的变量

mysql - 将数据从 oracle 数据库迁移到 AWS rds mysql 需要遵循哪些步骤?

amazon-web-services - 如何检测Azure/Amazon VM

hadoop - 自动缩放 EMR - 是否需要?我应该只使用 EC2 吗?我应该只使用 Qubole 吗?

ios - aws s3 sdk for iOS putObjectRegquest to "new"region 不工作

amazon-web-services - 使用 aws-java-sdk-s3 时出错

linux - bash: terraform: 未找到命令,导出 $PATH 未解析

amazon-web-services - 模板格式错误: Unresolved resource dependencies [ECSCluster] in the Resources block of the template

amazon-web-services - athena 查询返回列名称作为结果集

amazon-web-services - 如何使用 Terraform 循环创建模板