Terraform 无法从远程状态读取
Terraform cannot read from remote state
Terraform 版本
v0.12.1
AWS 提供商版本
v2.16.0
我已经配置了 Terraform 工作区,目前我的工作区指向 dev
,其中我有一个用于我的 VPC 和子网的 tfstate 文件,还有一个用于我的安全组的 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
Terraform 配置文件
安全组 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
输出有 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 状态读取输出
终于解决了,原来是 bucket key 的问题,因为我使用的是 Terraform workspace,所以 tfstate 文件是在文件夹 env:/dev/vpc_subnet/tfstate
下创建的,在更正 bucket key 之后,它能够解析 tfstate 文件。
Terraform 版本
v0.12.1
AWS 提供商版本
v2.16.0
我已经配置了 Terraform 工作区,目前我的工作区指向 dev
,其中我有一个用于我的 VPC 和子网的 tfstate 文件,还有一个用于我的安全组的 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
Terraform 配置文件
安全组 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
输出有 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 状态读取输出
终于解决了,原来是 bucket key 的问题,因为我使用的是 Terraform workspace,所以 tfstate 文件是在文件夹 env:/dev/vpc_subnet/tfstate
下创建的,在更正 bucket key 之后,它能够解析 tfstate 文件。