Can I automatically see all child module's output variables without re-defining them in the main root module's output.tf variables?

Viewed 1095

I have a module structure like below:

$ tree complete-module/
.
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
├── ...
├── modules/
│   ├── nestedA/
│   │   ├── README.md
│   │   ├── variables.tf
│   │   ├── main.tf
│   │   ├── outputs.tf
│   ├── nestedB/
│   ├── .../
├── examples/
│   ├── exampleA/
│   │   ├── main.tf
│   ├── exampleB/
│   ├── .../

The module nestedA has output varibles defined.

In order to see the nestedA's output variables after terraform apply, I need to propagate the root /main.tf variables and output them again in the root /outputs.tf.

While it works, I wonder if there is any shortcut so that I automatically see all child module's output variables after terraform apply without doing the plumbing code and re-define them in root /outputs.tf?

1 Answers

Not full automation, but semi-automation.

Using your example above, let's say NestedA outputs the following:

# NestedA/outputs.tf
output email {
  value = string
}
output dob {
  value = string
}

and let's say NestedB instantiates NestedA internally so:

# NestedB/main.tf
module nested_a {
  source = "../NestedA"
  ...
}

you can declare the entirety of module.nested_a as an output of NestedB:

# NestedB/outputs.tf
output nested_a {
  value = module.nested_a
}

so if another project such as exampleB instantiates NestedB directly, it can access all of NestedA attributes without having declared them all individually:

# examples/exampleB/main.tf
module nested_b {
  source = "../../modules/NestedB/"
  ...
}
locals {
  email = module.nested_b.nested_a.email
  dob = module.nested_b.nested_a.dob
}
Related