Hi,
We are trying to reference a module resource from another module but it does not seem to work.
So our directory structure is the following:
โโโ a
โย ย โโโ main.tf
โย ย โโโ terraform.tfstate
โโโ b
โย ย โโโ main.tf
โย ย โโโ terraform.tfstate
โโโ main.tf
โโโ terraform.tfstate
then we have the /main.tf in the root directory
module "a" {
source = "./a"
}
module "b" {
source = "./b"
}
output "mtmv" {
value = "${module.a.value}"
}
output "mumv" {
value = "${module.b.value}"
}
Then we have /a/main.tf
output "value" {
value = "aaa"
}
Then we have /b/main.tf
output "value" {
value = "bbb"
}
output "mtmv" {
value = "${module.a.value}"
}
When we ran terraform plan we have this error:
Error configuring: 1 error(s) occurred:
* output.mtmv: missing dependency: module.a.output.value
Thank you for your help,
Hi @comozo! Thanks for opening this. It looks like you're referring to ${module.a.value} in a file in a module nested deeper, instead of the input you defined? Is this intentional (which might indicate a need to refactor the modules) or a typo? The problem is in /b/main.tf, specifically this section:
output "mtmv" {
value = "${module.a.value}"
}
To achieve this you'd need to define a variable in module B and pass the value from module A to it in the composition root. Does that explanation make sense?
Thank you for the quick reply.
Yes it is intentional I would to use a resource defined in another module.
I'm sorry I didn't get your explanation.
Thanks
I managed to get it working with this sample config:
โโโ a
โ โโโ main.tf
โโโ x
โ โโโ main.tf
โโโ root.tf
module "x" {
source = "./x"
}
module "a" {
source = "./a"
XTopic = "${module.x.XTopicArn}"
}
resource "aws_sns_topic" "XTopic" {
name = "my-x-topic"
}
output "XTopicArn" {
value = "${aws_sns_topic.XTopic.arn}"
}
variable "XTopic" {
}
resource "aws_sqs_queue" "AQueue" {
name = "my-a-queue"
}
resource "aws_sns_topic_subscription" "ABucketTopicSubscription" {
topic_arn = "${var.XTopic}"
protocol = "sqs"
endpoint = "${aws_sqs_queue.AQueue.arn}"
}
@nkbt - This worked for me, thank you!
Cross module dependencies don't work indeed, +1 to have this feature.
@nkbt - Worked, you made my day!
Closing since we have a solution.
I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Most helpful comment
I managed to get it working with this sample config:
root.tf
x/main.tf
a/main.tf