How would you output returned values from a child module?
How would you output returned values from a child module?
To output returned values from a child module in Terraform, you need to declare the output in both the child module and the root module. In the child module, you need to define an output block that specifies the value to be returned. In the root module, you must reference this output using module.<module_name>.<output_name> and optionally declare an additional output block if you want to display or use the value elsewhere in the root module. This allows the parent (root) module to correctly capture and utilize the values returned from the child module.
C. Declare the output in both the root and child module To output returned values from a child module, you need to declare the output in both the child and root module. In the child module, you need to declare an output block, which defines the values that the child module will return. In the root module, you will reference the output values from the child module using the syntax module.<module_name>.<output_name>. You can then declare an output block in the root module to display the values or use them elsewhere in the root module configuration.
chatGPT? ) In a parent module, outputs of child modules are available in expressions as module.<MODULE NAME>.<OUTPUT NAME>. For example, if a child module named web_server declared an output named instance_ip_addr, you could access that value as module.web_server.instance_ip_addr. https://developer.hashicorp.com/terraform/language/values/outputs
In Terraform, it is not necessary to declare an output in both the root and child module. The output declared in the child module will be available to the root module, so you only need to declare an output in the root module if you want to expose that value to the outside world or use it in another module. If you declare an output in both the root and child module with the same name, Terraform will use the output from the root module as the final output. This can be useful if you want to override the output value of the child module with a different value in the root module. However, it is generally a good practice to avoid duplicate output names between modules to prevent confusion and ensure that the output values are clearly defined and organized. In summary, it is not necessary to declare an output in both the root and child module, but if you do, the output from the root module will override the output from the child module. It is recommended to use unique output names to avoid confusion and ensure clear organization of output values.
Just tested it. If I don't declare an output block in my root module reffering to my child module's output , I get no returned output on the CLI.
B In a parent module, outputs of child modules are available in expressions as module.<MODULE NAME>.<OUTPUT NAME>. For example, if a child module named web_server declared an output named instance_ip_addr, you could access that value as module.web_server.instance_ip_addr.
child module: output "example_output" { value = "some value" } PARENT Module: module "example_module" { source = "./example_module" } output "example_output_from_child" { value = module.example_module.example_output }
The clue here is "returned value" from child module. So the value is already declared in child module, so you just need to call it in Parent
B. Declare the output in the child module When you want to output returned values from a child module, you should declare the output in the child module itself. Outputs in Terraform (or other Infrastructure as Code tools) are used to expose certain values or results from one module, which can then be consumed by other modules or scripts.
B for me.
you don't need to declare output variable in root it work with child fine
You can use outputs.tf in the child module and the output block in the root module.
B is the correct answer in my opinion
b - https://developer.hashicorp.com/terraform/language/values/outputs
Accessing Child Module Outputs In a parent module, outputs of child modules are available in expressions as module.<MODULE NAME>.<OUTPUT NAME>. For example, if a child module named web_server declared an output named instance_ip_addr, you could access that value as module.web_server.instance_ip_addr.
How would you output returned values from a child module? A. Declare the output in the root configuration B. Declare the output in the child module C. Declare the output in both the root and child module D. None of the above
Answer B