我在一个项目中使用 Terraform 远程状态时遇到了问题。状态文件被保存于 S3 上。我已经按如下方式导入:
data "terraform_remote_state" "management" {
backend = "s3"
config {
bucket = "testbucket"
key = "subfolder/terraform.tfstate"
region = "us-west-2"
}
}
我可以在上述状态文件的根级别看到输出:
"outputs": {
"cidr": {
"sensitive": false,
"type": "string",
"value": "10.90.0.0/16"
},
我这样使用远程状态:
module "dev-alpha-application" {
source = "../../modules/application"
envname = "test-app"
baseami = "ami-a042f4d8"
key_name = "pb-smarsh-test"
clui_baseami = "ami-xxxxxxxx"
adui_baseami = "ami-xxxxxxxx"
cidr = "10.80.0.0/16"
management_cidr = "${data.terraform_remote_state.management.cidr}"
vpn_gateway_id = "cgw-xxxxxxxx"
cidrs = "${list("${data.terraform_remote_state.management.cidr}", "${module.dev-alpha-application.cidr}")}"
除非我漏掉了一些愚蠢的东西,否则这应该可以工作。但是当我运行terraform apply时,我会得到以下错误:
* module.dev-alpha-application.var.management_cidr: Resource 'data.terraform_remote_state.management' does not have attribute 'cidr' for variable 'data.terraform_remote_state.management.cidr'
Terraform init的工作正常。非常感谢任何帮助。我尽可能包含了尽可能多的信息。
编辑 - 在调试模式下,看起来它可以成功打开远程状态文件。
-----------------------------------------------------
2018/04/10 09:05:10 [DEBUG] [aws-sdk-go] <?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Name>testbucket</Name><Prefix>env:/</Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated></ListBucketResult>
2018/04/10 09:05:10 [DEBUG] [aws-sdk-go] DEBUG: Request s3/GetObject Details:
---[ REQUEST POST-SIGN ]-----------------------------
GET /subfolder/terraform.tfstate HTTP/1.1
Host: testbucket.s3.us-west-2.amazonaws.com
User-Agent: aws-sdk-go/1.12.59 (go1.9.1; linux; amd64) APN/1.0 HashiCorp/1.0 Terraform/0.11.3
Authorization: AWS4-HMAC-SHA256 Credential=AKIAI6X7Y3APAUTZZQOQ/20180410/us-west-2/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=716689e2124dd2a689b528c0cb51b07aeaf791cf577fa1a4fd17a79fb401b957
X-Amz-Content-Sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
X-Amz-Date: 20180410T080510Z
Accept-Encoding: gzip
-----------------------------------------------------
2018/04/10 09:05:11 [DEBUG] [aws-sdk-go] DEBUG: Response s3/GetObject Details:
---[ RESPONSE ]--------------------------------------
HTTP/1.1 200 OK
Connection: close
Content-Length: 386104
Accept-Ranges: bytes
Content-Type: application/json
Date: Tue, 10 Apr 2018 08:05:12 GMT
Etag: "1df4aaad48451707a79306a8f6a1c51c"
Last-Modified: Mon, 09 Apr 2018 14:53:03 GMT
Server: AmazonS3
X-Amz-Id-2: 92XqUEj319Uq5KhYFWvrLQ3O7VooOMMQ4GxY0keRKYY72Q5mpOgjeZ78w20AzjCSFLuZZycfEqU=
X-Amz-Request-Id: 448DB8C5E4B70A9A
X-Amz-Server-Side-Encryption: AES256
X-Amz-Version-Id: GjI6glV8oa9_.eOFSc5GFGVtTJBnrEmL
但我也在调试日志中看到了这个:
2018/04/10 09:02:17 [DEBUG] Resource state not found for "data.terraform_remote_state.management": data.terraform_remote_state.management
data.terraform_remote_state.management.outputs..cidr
。 - ydaetskcoRtestbucket/subfolder/terraform.tfstate
是否存在于您预期的位置?TF 不会告诉您状态未找到,并允许您使用它,这可能是您出现此错误的原因。 - StephenTF_LOG=DEBUG terraform plan
。它可能会输出大量信息,但通常能够提供一些正在发生的情况的见解。 - Stephen