Consider a terraform module:
module "blah-asg" { source = "asg" asg_max_size = 1 asg_min_size = "${var.min_blah}" ... }
How do I output
variables from it?
output "blah-es-asg" { value = "${asg.blah-asg.arn}" }
Which failed with
Error getting plugins: module root: 1 error(s) occurred: * output 'blah-asg': unknown resource 'asg.blah' referenced in variable asg.blah-asg.arn
How can I output module fields in Terraform?
Outputs from child modules are not displayed as outputs in the main module. You need to explicitly create outputs in the main module if you want to output any child module outputs. Run your Terraform init , plan , and apply commands again. The root module outputs the results of the child modules.
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.
Modules on the public Terraform Registry can be referenced using a registry source address of the form <NAMESPACE>/<NAME>/<PROVIDER> , with each module's information page on the registry site including the exact address to use. The above example will use the Consul module for AWS from the public registry.
There are several uses for output variables. Here are some examples: We can use outputs to expose a subset of a child module's resource attributes to a parent module. We can use outputs to print values from a root module in the CLI output after running terraform apply .
So first, you need to set the output in the module asg
:
$ cat asg/output.tf output "blah-es-asg" { value = "${aws_autoscaling_group.blah-asg.arn}" }
Then you call the module with source = "asg"
:
module "blah-asg" { source = "asg" asg_max_size = 1 asg_min_size = "${var.min_blah}" ... }
You can output it in current code with this format now:
output "blah-es-asg" { value = "${module.blah-asg.blah-es-asg}" }
The module itself knows nothing of the name blah-asg
- that's just in the script that's calling it - indeed it could be called multiple times with different names and parameters.
The output should just reference things within the module the same way you would elsewhere in that same module. For instance, if you wanted to output the arn of the following resource:
resource "aws_lb" "test" { # ... }
You would use:
output "blah-es-asg" { value = "${aws_lb.test.arn}" }
Note that the output is defined along side the rest of the module code, not in the script that's calling it.
This output can then by used by the script calling the module as ${module.blah-asg.blah-es-asg}
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