O'Reilly logo

Getting Started with Terraform - Second Edition by Kirill Shirinkin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 the 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required