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