Remote State Data Source with multiple sub-modules

Hi All,

I have a root module from where I am calling my submodules - vpc module (which creates VPC, subnets etc) and my ec2 module.
When I run the vpc module first and then uncomment my ec2 module and call it, all works fine.
But if I call them together, I get this error - unsupported attribute.
I am using remote state data source to pass outputs of vpc module to my ec2 module.

data “terraform_remote_state” “myvpc” {

backend = “local”

config = {

path = "../SIT/terraform.tfstate"

}

}

+++++++++++++++++++++++++++++++++++

output “sn1_subnet_id” {

value = module.my_vpc.sn1_subnet_id

}

output “sn3_subnet_id” {

value = module.my_vpc.sn3_subnet_id

}

output “sn2_subnet_id” {

value = module.my_vpc.sn2_subnet_id

}

output “sn4_subnet_id” {

value = module.my_vpc.sn4_subnet_id

}

output “sn5_subnet_id” {

value = module.my_vpc.sn5_subnet_id

}

output “sn6_subnet_id” {

value = module.my_vpc.sn6_subnet_id

}

Can someone please suggest what shall I use along side remote state data source for unprovisioned resources, so that I may not get the unsupported attribute error.

Hi @amrita.verma1507 ,
as your configuration is built within a single “stack” (vpc & ec2), there’s no need to query a remote state for getting access to input data.

You’d just pass outputs of the vpc module as inputs to the ec2 module.

No… the configuration is not in a single stack… This is how it looks like -

main.tf
outputs.tf
Networking
- vpc.tf
-subnet.tf
- nat&igw.tf
- outputs.tf
- variables.tf
Application
- ec2.tf
- eip.tf
- sg.tf
- variables.tf

and this is what my main.tf looks like -

module “my_vpc” {

source = "../TF-Modules/Networking"

vpc_region = "us-east-1"

}

module “my_ec2” {

source = “…/TF-Modules/Application”

}

As per my understanding, I’d still consider it as a “single stack” as a single terraform apply would be used in the end.
You’d just use outputs of the vpc module as inputs to the ec2 module.

In regards to the remote state.
Have you tried getting access to the values using data.terraform_remote_state.myvpc.<output_variable_name>?

Maybe I’m misunderstanding, so could you provide a sample repo?