Terraform 无法从远程状态读取



Terraform version

0.12.1 版

AWS 提供商版本

2.16.0 版

我已经配置了 Terraform 工作区,因为现在我的工作区指向dev,其中我的 VPC 和子网有一个 tfstate 文件,我的安全组有一个不同的文件,但是当我尝试将vpc_id从我的 VPC 远程 tfstate 引用到我的安全组时,我收到以下错误消息

No stored state was found for the given workspace in the given backend.

我的 s3 存储桶如下所示

nonprod-us-east-1
  |-- env
         |-- dev
                |-- vpc_subnet/tfstate
                |-- security_group/tfstate

地形配置文件

安全组 tf 配置
terraform {
    backend "s3"{
        # Configuration will be injected by environment variables.
    }
}
provider "aws" {
  region = "${var.region}"
}

data "terraform_remote_state" "vpc_subnet" {
  backend = "s3"
  config = {
    bucket  = "nonprod-us-east-1"
    key     = "vpc_subnet/tfstate"
    region  = "us-east-1"
  }
}
 vpc_id = "${data.terraform_remote_state.vpc_subnet.outputs.vpc_id}"

我已经验证了我的vpc_subnet/tfstate oputput 已经vpc_id

来自 VPC 子网 tf 状态的输出

outputs": {
    "private_subnet_cidr_blocks": {
      "value": [
        "10.0.3.0/24",
        "10.0.4.0/24",
        "10.0.5.0/24"
      ],
      "type": [
        "tuple",
        [
          "string",
          "string",
          "string"
        ]
      ]
    },
    "private_subnet_ids": {
      "value": [
        "subnet-042a16dd291e90add",
        "subnet-02e8322d996968a3f",
        "subnet-078f525c24015b364"
      ],
      "type": [
        "tuple",
        [
          "string",
          "string",
          "string"
        ]
      ]
    },
    "public_subnet_cidr_blocks": {
      "value": [
        "10.0.0.0/24",
        "10.0.1.0/24",
        "10.0.2.0/24"
      ],
      "type": [
        "tuple",
        [
          "string",
          "string",
          "string"
        ]
      ]
    },
    "public_subnet_ids": {
      "value": [
        "subnet-0ba92a28f6e8ddd95",
        "subnet-08efcb80bed22f4e2",
        "subnet-0b641797bfe207a0b"
      ],
      "type": [
        "tuple",
        [
          "string",
          "string",
          "string"
        ]
      ]
    },
    "vpc_id": {
      "value": "vpc-0bb7595ff05fed581",
      "type": "string"
    }
  }

预期行为

它应该能够从远程 tf 状态位置读取vpc_id

实际行为

无法从远程 tf 状态读取输出

终于解决了,结果是存储桶密钥的问题,因为我使用的是 Terraform 工作区,因此 tfstate 文件是在文件夹 env:/dev/vpc_subnet/tfstate 下创建的,更正存储桶密钥后,它能够解析 tfstate 文件。

最新更新