Retrieving module data with outputs

Another useful construct that Terraform provides are outputs. In an output, you define which data you want to be returned by the module. Add the following line to the very bottom of the ./modules/application/application.tf file:

output "hostname" { 
  value = "${aws_instance.app-server.private_dns}" 
} 

Now you can use this output inside template.tf like this:

module "crazy_foods" { 
  source = "./modules/application" 
  vpc_id = "${aws_vpc.my_vpc.id}" 
  subnet_id = "${aws_subnet.public.id}" 
  name = "CrazyFoods ${module.mighty_trousers.hostname}" 
} 

Besides the obvious ability to get data from the module, there is another use case for module outputs: forcing dependencies. Here is the graph before passing the output to second module:

Retrieving module data with outputs

Here is the graph with dependency forced:

Retrieving module data with outputs

That's completely different level of graph-beauty, I hope you agree. But sometimes, as we discussed previously, we have to do it. For example, one module creates a master server and the other one is responsible for slaves.

Since Terraform 0.8.0, you can specify a module inside the depends_on attribute. This will result to the resource dependant on all resources from the module. Prior 0.8.0, you have to trick Terraform to do it with a combination of outputs and other trickery. Note that Terraform still doesn't allow a module to depend on another module directly via depends_on--this attribute is not available for modules whatsoever.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.112.1