1
votes

I am trying to call data from a remote state to reference a vpc_id for a network acl. When I run plan/apply, I receive the error "This object has no argument, nested block, or exported attribute named "vpc_id"."

I've tried using "data.terraform_remote_state.*.vpc_id", as well as "${}" syntax. I tried defining the data.remote info in the variables.tf for the child module, and the parent module.

I ultimately need to be able to call this module for different VPCs/subnets dynamically.

The relevant VPC already exists and all modules are initialized.

s3 bucket stage/network/vpc/terraform.tfstate:

"outputs": {
    "vpc_id": {
      "value": "vpc-1234567890",
      "type": "string"
    }
  },

enter code here

modules/network/acl/main.tf:

data "terraform_remote_state" "stage-network" {
  backend = "s3"

  config = {
    bucket          = "bucket"
    key             = "stage/network/vpc/terraform.tfstate"
  }
}

resource "aws_network_acl" "main" {
  vpc_id        = data.terraform_remote_state.stage-network.vpc_id
# acl variables here

stage/network/acl/main.tf:

data "terraform_remote_state" "stage-network" {
  backend = "s3"

  config = {
    bucket          = "bucket"
    key             = "stage/network/vpc/terraform.tfstate"
  }
}

module "create_acl" {
  source              = "../../../modules/network/acl/"

vpc_id = var.vpc_id
# vpc_id = data.terraform_remote_state.stage-network.vpc_id
# vpc_id = "${data.terraform_remote_state.stage-network.vpc_id}"
# vpc_id = var.data.terraform_remote_state.stage-network.vpc_id

I am expecting the acl parent module to be able to associate to the VPC, and from there the child module to be able to configure the variables.

2
Don't use the remote state data source for this, use the actual aws_vpc data source instead and save yourself a ton of hassle.ydaetskcoR
I get the same error message (Error: Unsupported attribute - This object has no argument, nested block, or exported attribute named "vpc_id".) when I change the files to use aws_vpc data source. This is how I changed it: main.tf in parent NACL: resource "aws_network_acl" "main" { vpc_id = var.vpc_id #other variables } variables.tf in NACL parent module: data "aws_vpc" "vpc" { id = data.aws_vpc.vpc.vpc_id } variable "vpc_id" { description = "(Required) The ID of the associated VPC." }Kevlar_Axis
Sorry for the formatting there, not sure how to get the code portions to look correct in a comment.Kevlar_Axis

2 Answers

6
votes

This is one of the breaking changes that the 0.12.X version of Terraform introduce.

The terraform_remote_state data source has changed slightly for the v0.12 release to make all of the remote state outputs available as a single map value, rather than as top-level attributes as in previous releases.

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

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

0
votes

In first state: .....

output "expose_vpc_id" {
  value = "${module.network.vpc_id}"
}

In another state, to share between terraform configs:

data "terraform_remote_state" "remote" {
  backend = "s3"
  config = {
    bucket = "terraform-ex1"
    key    = "tera-ex1.tfstate"
    region = "us-east-1"
  }
}

output "vpc_id" {
  value = "${data.terraform_remote_state.remote.outputs.expose_vpc_id}"
}