Reputation: 33
I'm working with terraform for a while and I didn't face any issues as I always followed a simple structure. But now I've started working on another project that doesn't seem like it adheres to best practices.
The project structure is like this:
> root-folder
> modules
> cloudfront
> route53
> team1
> service1
- main.tf
- variables.tf
- locals.tf
And what I'm trying to do is, use modules.cloudfront.some_output_value within the modules>team1>service1>main.tf file as seen below:
module "record" {
source = "../../route53"
zone_id = var.route53_zone_id
record_name = local.record_name
cdn_id = module.cloudfront.some_output_value //the question is about this line
}
But I'm unable to use it, since the IDE indicates there is no such module as module.cloudfront.
Does anyone know why I cannot use a module that is defined at an outer scope?
Terraform version: 1.2.0 (probably this's not related to a version based issue, but anyway.)
Upvotes: 0
Views: 1038
Reputation: 762
It depends from where you invoke the cloudfront
module.
If you invoke both modules, service1
and cloudfront
from the same parent, then you need to define a variable in service1
and explicitly pass the value:
service1
(Should be splitted into variables.tf
and main.tf
of course.)
variable "cdn_id" {
type = ...
description = "..."
}
module "record" {
source = "../../route53"
zone_id = var.route53_zone_id
record_name = local.record_name
cdn_id = var.cdn_id
}
parent module
(e.g. in root folder)
module "service1" {
source = "modules/team1/service1"
cdn_id = module.cloudfront.some_output_value
some_var_name = some_value
...
}
module "cloudfront" {
source = "modules/cloudfront"
some_var_name = some_value
...
}
If you invoke it from service1
, then the location of the module in the file hierarchy does not matter. You just have to set the source path right:
module "record" {
source = "../../route53"
zone_id = var.route53_zone_id
record_name = local.record_name
cdn_id = module.cloudfront.some_output_value
}
module "cloudfront" {
source = "../../cloudfront"
some_var_name = some_value
...
}
Upvotes: 1
Reputation: 238081
You can't query outer modules from inner modules. If you have any variables/outputs in your outermodules that should be used in the inner modules, you have to explicitly pass them from the outer into inner modules.
Upvotes: 0