Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resource does not have attribute for variable - Terraform remote state

Tags:

terraform

Having an issue getting Terraform remote state working in one of my projects. The statefile is remote in S3. I am importing like so:

data "terraform_remote_state" "management" {
  backend = "s3"

  config {
    bucket = "testbucket"
    key    = "subfolder/terraform.tfstate"
    region = "us-west-2"
  }
}

I can see the output at the root level of the above statefile:

            "outputs": {
            "cidr": {
                "sensitive": false,
                "type": "string",
                "value": "10.90.0.0/16"
            },

I am using the remote state like so:

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

Unless I have overlooked something stupid, this should work, however when I run a terraform apply, I get the following error:

* 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 works as expected. Any assistance would be greatly appreciated. I have tried to include as much info as possible .

Edit - In Debug mode, it appears it is opening the remote state file ok

-----------------------------------------------------
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

But I also see this in the debug logs:

2018/04/10 09:02:17 [DEBUG] Resource state not found for "data.terraform_remote_state.management": data.terraform_remote_state.management
like image 233
mehstg Avatar asked Apr 09 '18 11:04

mehstg


People also ask

Where does Terraform store its remote state?

Terraform supports storing state in Terraform Cloud, HashiCorp Consul, Amazon S3, Azure Blob Storage, Google Cloud Storage, Alibaba Cloud OSS, and more. Remote state is implemented by a backend or by Terraform Cloud, both of which you can configure in your configuration's root module.

How do you transfer Terraform resources from one state to another?

The terraform state mv command moves resources from one state file to another. You can also rename resources with mv . The move command will update the resource in state, but not in your configuration file.

What is Terraform_remote_state?

The terraform_remote_state data source uses the latest state snapshot from a specified state backend to retrieve the root module output values from some other Terraform configuration. You can use the terraform_remote_state data source without requiring or configuring a provider.


1 Answers

For those who has similar problem. It is mostly because the documentation of terraform is not up-to-date. For terraform <=0.11 you need to access the output variables directly without 'outputs' e.g. data.terraform_remote_state.management.cidr.

With >=0.12 it will be with 'outputs' e.g. data.terraform_remote_state.management.outputs.cidr.

Documentation on the website will also be fixed but it is currently not deployed.

https://github.com/hashicorp/terraform/commit/142ecfefe063c8f78cdbcbaaa3b5bb963831a98e#diff-4b355b6363c6f1e57a6132decd8502e9

like image 80
Vincent Wagner Avatar answered Sep 28 '22 04:09

Vincent Wagner