Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform: Output a field from a module

Tags:

Code

Consider a terraform module:

module "blah-asg" {   source = "asg"    asg_max_size       = 1   asg_min_size       = "${var.min_blah}"   ... } 

My problem

How do I output variables from it?

What have I tried

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

My question

How can I output module fields in Terraform?

like image 948
Adam Matan Avatar asked Oct 31 '17 11:10

Adam Matan


People also ask

How do I get the output from a module 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.

How do I access the output value in Terraform?

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.

How do I reference a module in Terraform?

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.

How do you use output variables in Terraform?

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 .


2 Answers

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}" } 
like image 104
BMW Avatar answered Oct 05 '22 23:10

BMW


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}

like image 21
James Thorpe Avatar answered Oct 05 '22 22:10

James Thorpe