Terraform: cross module dependency

Created on 10 Jan 2016  ยท  8Comments  ยท  Source: hashicorp/terraform

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,

core question

Most helpful comment

I managed to get it working with this sample config:

โ”œโ”€โ”€ a
โ”‚   โ””โ”€โ”€ main.tf
โ”œโ”€โ”€ x
โ”‚   โ””โ”€โ”€ main.tf
โ””โ”€โ”€ root.tf

root.tf

module "x" {
  source = "./x"
}

module "a" {
  source = "./a"
  XTopic = "${module.x.XTopicArn}"
}

x/main.tf

resource "aws_sns_topic" "XTopic" {
  name = "my-x-topic"
}

output "XTopicArn" {
  value = "${aws_sns_topic.XTopic.arn}"
}

a/main.tf

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}"
}

All 8 comments

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

root.tf

module "x" {
  source = "./x"
}

module "a" {
  source = "./a"
  XTopic = "${module.x.XTopicArn}"
}

x/main.tf

resource "aws_sns_topic" "XTopic" {
  name = "my-x-topic"
}

output "XTopicArn" {
  value = "${aws_sns_topic.XTopic.arn}"
}

a/main.tf

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.

Was this page helpful?
0 / 5 - 0 ratings