Reference AWS Resource output in parent module

I’ve inherited some TF scripts that will create some AWS resources and an RDS instance by calling two locally created modules:
main.tf

module "rds" {
source = "../rds"
}
module "app" {
source = "../app"
}

The RDS module creates an AWS Aurora cluster by using the HashiCorp Registry module “terraform-aws-modules/rds-aurora/aws” , and I’d like to be able to pass the cluster_endpoint output to the “app” module but haven’t found the right code to be able to elevate the value to the main.tf so I can pass it as a variable to module “app”. Any help/guidance on how to get the output available so that it can be used as a variable value would be appreciated.

I hope the above makes sense.

Hi @glenn.comiskey,

From your description I understand that your ../rds module includes another module block with source = "terraform-aws-modules/rds-aurora/aws", and you are asking how to expose one of the output values from that nested module call as a result from your own ../rds module.

You can achieve that result by declaring an Output Value in your module, whose value is defined to refer to the nested module. For example (inside your ../rds module):

module "aurora" {
  source = "terraform-aws-modules/rds-aurora/aws"

  # ...
}

output "cluster_endpoint" {
  value = module.aurora.cluster_endpoint
}

(I’ve shown the module and output blocks in the same “file” in order to be concise about it, but a more typical convention would be to place the output block in a file called outputs.tf. Terraform doesn’t treat filenames as significant aside from the suffix, but other future maintainers who have experience with Terraform may expect this naming convention.)

Then you can modify the root module configuration you shared in your question as follows:

module "rds" {
  source = "../rds"
}
module "app" {
  source = "../app"

  db_cluster_endpoint = module.rds.cluster_endpoint
}

That new argument inside the module "app" block will work only if your ../app module declares an Input Variable of that name. For example:

variable "db_cluster_endpoint" {
  type = string
}

Then elsewhere in that module you can refer to var.db_cluster_endpoint to make use of the value.

1 Like