dimiguel
dimiguel

Reputation: 1549

Reference output from an online nested module within a nested module

I have the following file structure:

├── lambda
│   ├── main.tf
├── main.tf

main.tf creates the my_custom_lambda_function module off of the lambda folder.

module "my_custom_lambda_function" {
  source = "./lambda"
  function_name = "some-name"
}

Then, I have a nested module within lambda that refers to an online module:

# lambda/main.tf
variable "function_name" {
  type = string
}

module "lambda_template" {
  source = "terraform-aws-modules/lambda/aws"
  function_name = "{var.function_name}"
}

I need to access an output in the module module.lambda_template from module.my_custom_lambda_function. However, when I try to do this:

# lambda/main.tf
output "function_arn" {
  value = module.lambda_template.this_lambda_function_arn
}

I get the following error:

No module call named "lambda_function" is declared in the root module.

Which makes sense, because this syntax is referring to a module within the root. But I want to access the module that is relative to the module that I'm currently on. I have not yet found a way to do this. Something like:

# lambda/main.tf
output "function_arn" {
  value = module.${this}.module.lambda_function.this_lambda_function_arn
}

Where the value of this would be my_custom_lambda_function.

This code is not valid syntax, but it expresses what I need. Is there a way to do this within Terraform?

Upvotes: 1

Views: 460

Answers (1)

Marcin
Marcin

Reputation: 238081

Based on the comments and chat discussion.

I tried to replicate the setup, and I found nothing wrong with it. It all works as expected.

It also works now for the OP. Upon chat discussion, the issue could had been caused before by a spelling mistake in module or output names.

Upvotes: 1

Related Questions