Hi @apparentlymart I am trying to achieve module inter-dependency amongst different folder levels.
Here’s what I want to achieve.
This is the folder structure:
org:
modules:
gcp_buket:
main.tf
variables.tf
data.tf
outputs.tf
gcp_service_account:
main.tf
variables.tf
data.tf
outputs.tf
environment:
dev:
bucket_module:
main.tf (please refer [1] )
outputs.tf
variables.tf
service_account_module:
main.tf (please refer [2] )
outputs.tf
variables.tf
The above is the folder structure, where the org/modules have all the reusable code which has the “resource” types, and below are the “module” types re-using the above resources.
In the below bucket_module, I am creating two buckets.
[1] bucket_module: main.tf
module "bucket_1" {
source = "../../../org/modules/gcp_buket"
a = var.a
b = var.b
..... more variables
}
module "bucket_2" {
source = "../../../org/modules/gcp_buket"
a = var.a
b = var.b
..... more variables
}
and here I am creating two service account modules.
[1] service_account_module: main.tf
module "service_account_module_1" {
source = "../../../org/modules/gcp_service_account"
a = var.a
b = var.b
..... more variables
}
module "service_account_module_2" {
source = "../../../org/modules/gcp_service_account"
a = var.a
b = var.b
..... more variables
}
Now my question is how do I achieve module inter-dependency from another folder ?
basically I want to achieve the below.
module "bucket_1" {
source = "../../../org/modules/gcp_buket"
a = var.a
b = var.b
..... more variables
depends_on = [module.service_account_module_1]
}
but since the service_account_module_1, is in another folder I cannot do depends_on cause it says, module not found. so I tried using “data” source like below.
data "terraform_remote_state" "service_account" {
backend = "local"
config = {
path = "../service_account/.terraform/terraform.tfstate"
}
}
module "bucket_1" {
source = "../../../org/modules/gcp_buket"
service_account = data.terraform_remote_state.service_account.outputs.service_account.email
a = var.a
b = var.b
..... more variables
}
If I run terraform plan and apply inside the service_account folder, everything works. I can see the outputs as well, but when i try to run it at the root_level, it gives me the below error. Ideally it should wait for the service account to create it first, export the outputs, then pass it to the bucket, but since the service_account is yet to be created, and there are no outputs in the local “terraform.tfstate” , it fails.
│ │ data.terraform_remote_state.service_account.outputs is object with no attributes
│
│ This object does not have an attribute named "service_account_a_name".
Can you please advise how do i achieve this inter-module dependency, for the modules that are in a different folder. Thanks in advance.