I have the following simple setup:
~$ tree
.
├── main.tf
└── modules
└── world
└── main.tf
~$ cat main.tf
output "root_module_says" {
value = "hello from root module"
}
module "world" {
source = "modules/world"
}
~$ cat modules/world/main.tf
output "world_module_says" {
value = "hello from world module"
}
I then run:
~$ terraform get
~$ terraform apply
I expect to see world_module_says in the outputs, but I do not, I only see root_module_says. This is really confusing as to why?
If it helps:
~$ terraform --version
v0.10.8
From the virtual machine module, create an output for the IP address, then use the IP address as an input value for the firewall child module. Below is an example of adding an output value to a Terraform configuration. Use the output keyword followed by an identifier for the output.
Terraform Output Command To get the raw value without quotes, use the -raw flag. To get the JSON-formatted output, we can use the -json flag. This is quite useful when we want to pass the outputs to other tools for automation since JSON is way easier to handle programmatically.
Terraform output values allow you to export structured data about your resources. You can use this data to configure other parts of your infrastructure with automation tools, or as a data source for another Terraform workspace. Outputs are also necessary to share data from a child module to your root module.
Using a variable A variable's value can be accessed from within the terraform module block by using var. <variable_name> . Below we have an example demonstrating this. The variable's value can only be accessed in an expression within the modules where it was declared.
Terraform only shows the output from root (by default pre v0.12) https://www.terraform.io/docs/commands/output.html
Prior to Terraform 0.12 you can get the output from the world module with:
terraform output -module=world
I think the logic here is that the output from the module would be consumed by root and if you actually needed the output then you'd output it in root too so main.tf might contain this:
output "root_module_says" {
value = "hello from root module"
}
output "world_module_says" {
value = "${module.world.world_module_says}"
}
module "world" {
source = "modules/world"
}
Beginning with Terraform 0.12 this is the only way to get the output from within a module.
The below command shows the output for a specific module
terraform output -module=example_module
The below command fails in Terraform 0.12 and above with an error:
terraform output -module=example_module
Error: Unsupported option
The -module option is no longer supported since Terraform 0.12, because now
only root outputs are persisted in the state.
To get outputs from a module in Terraform 0.12 and above, you must export them from the root (e.g example_module
) module using an output block in the caller module.
This can be now done simply by adding one line in the caller module like below:
output "example_module_outputs" {
value = module.example_module
}
terraform output
When accessing a module output group (using the 'terraform output' command from a console or a script) then Terraform will printout all output variables within this group in a formatted way, or in json format using '-json' flag.
When extracting output values, we can use the '-raw' flag only if there is a single variable which has a string format.
If we have more than 1 outout in the output group within the module:
Lets say we declared in the 'root' module the following outout group in a module:
output "world_world_module_says" {
value = module.world.world_module_says
}
and in the module main.tf we have 2 vars:
output "world_module_says" {
value = "hello from world module"
}
output "world_module_says_again" {
value = "hi again from world module"
}
terraform output -json world_world_module_says |jq '.'"world_module_says"
output "world_world_module_says" {
value = module.world.world_module_says
}
output "world_world_module_says_again" {
value = module.world.world_module_says_again
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With