Terraform_Remote_State Error: This object has no argument, nested block, or exported attribute named "vpc_id"

10.2k views Asked by At

Working with Terraform (TF) in AWS and am stuck with an error when trying to call the vpc_id using terraform_remote_state. We segmented out the different pieces of the network in order to mitigate state slippage. However, it also requires interacting with the statefiles of each individual piece of the infrastructure (ie. a statefile for vpc, sgs, roles, etc.). When I try to grab the vpc_id from the statefile, held within S3 bucket, I get the following error:


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

This is my terraform_remote_state call in the main.tf file:

  backend = "s3"
  workspace = var.workspace
  config = {
    bucket = "terraform-east"
    key = "terraform-vpc.tfstate"
    region ="us-east-1" 
  }
}

This is the call within that same 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"
    )
  )
}

And here's the outputs.tf file within the vpc directory:

output "vpc_id" {
  value       = module.vpc.vpc_id
  description = "The VPC ID"
}

output "vpc_cidr" {
  value       = var.vpc_cidr
  description = "The VPC CIDR block"
}

Here's the tf_remote_state declaration:

data "terraform_remote_state" "vpc" {
    backend = "s3"
    workspace = var.workspace
    config = {
         bucket = "correct bucket (trust me)
         key = "correct key"
         region = us-east-1
    }
}

Finally, here's the backend.tf info from the vpc directory:

terraform {
    backend "s3" {
        bucket = "terraform-east"
        key = "terraform-vpc.tfstate"
        region ="us-east-1" 
        }
}

I've tried with outputs.vpc_id (as above), without outputs (outputs is required after tf 0.12 update), with outputs.vpc_id.value (as the statefile makes it seem like that's the structure), and with outputs[1].value.. It gives different errors, but nonetheless they all fail. Help would be appreciated.

3

There are 3 answers

0
Chris Halcrow On

The way to reference state values changed in terraform v 0.12. See the reference here:

https://www.terraform.io/upgrade-guides/0-12.html#remote-state-references

From the guide:

In previous releases, a reference to a vpc_id output exported by the remote state data source might have looked like this:

data.terraform_remote_state.vpc.vpc_id

This value must now be accessed via the new outputs attribute:

data.terraform_remote_state.vpc.outputs.vpc_id

4
Martin Atkins On

The terraform_remote_state data source exports the outputs from the remote state under an attribute called outputs, which is itself an object value containing one attribute for each output value.

So, to refer to the vpc output value in particular you'd need to write:

data.terraform_remote_state.outputs.vpc

...and then to get the vpc_id attribute from that nested vpc object:

data.terraform_remote_state.outputs.vpc.vpc_id
0
CoderCal165 On

Turns out it was simple. Needed to make sure VPN connection was established. -_- Thanks for viewing though!