I have defined a variable tenants
which is a map:
variable tenants {
type = map
default = {
tenant1 = {
name = "Tenant1"
},
tenant2 = {
name = "Tenant2"
}
}
}
In the root file, I call a module tenant
as follows:
module "tenant" {
source = "./modules/tenant"
for_each = var.tenants
tenant = each.value.name
}
The child module tenant
looks as follows:
resource "mso_tenant" "tenant" {
name = var.tenant
}
And I have defined an output in the child module with:
output "mso_tenant" {
value = mso_tenant.tenant
In the output file under root, I do the following to print all tenants:
output "tenant_names" {
value = { for p in sort(keys(var.tenants)) : p => module.tenant[p].mso_tenant.name }
}
So far so good, all the above works.
Now I have another variable called schemas
, also a map. A tenant can have multiple schema's. So I have defined the following
variable schemas {
type = map
default = {
schema1 = {
name = "Schema1",
template_name = "Template1",
tenant = <refer to tenant1> <==== refer to tenant module
},
schema2 = {
name = "Schema2"
template_name = "Template2",
tenant = <refer to tenant2> <==== refer to tenant module
},
schema3 = {
name = "Schema3"
template_name = "Template3",
tenant = <refer to tenant1> <==== refer to tenant module
},
}
}
In the main.tf file under root I want to do the following:
module "schema" {
source = "./modules/schema"
for_each = var.schemas
name = each.value.name
template_name = each.value.template_name
tenant_id = each.value.tenant
}
How could I reference the respective tenants in either the schema variable or else directly in the schema module?
Update:
Tried solution 1:
In variables file, I passed the tenant as follows:
schema1 = {
name = "Schema1",
template_name = "Template1",
tenant = module.tenant["tenant1"].mso_tenant
}
Error: Variables not allowed
Tried solution 2:
module "tenant" {
source = "./modules/tenant"
for_each = var.tenants
tenant = each.value.name
}
module "schema" {
source = "./modules/schema"
for_each = var.schemas
name = each.value.name
template_name = each.value.template_name
tenant_id = module.tenant[each.value.tenant].mso_tenant
}
Resulting in following error:
on main.tf line 30, in module "schema":
30: tenant_id = module.tenant[each.value.tenant].mso_tenant
|----------------
| each.value is object with 2 attributes
This object does not have an attribute named "tenant".
If you want to refer to the tenant resources of
tenant1
you can usemodule.tenant["tenant1"].mso_tenant
and assign this directly to the schema or in the input variables of the second module.As you are using module
for_each
module.tenant
is a map of objects keyed by the tenant_id. Each object consists of all the outputs of your tenant child module.REFACTORED:
root variable definitions:
calling the modules like this in the root:
root output definitions could look like:
this leads to the following outputs:
schema module used to simulate:
tenant module used to simulate:
hope in the full context this is easier and more clear ;)