Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Terraform Output Variable Tutorials with Example

Terraform will store hundreds or even thousands of attribute values for all the defined resources in our infrastructure in state file.

An outputed attributes can not only be used for the user reference but it can also act as an input to other resources being created via Terraform.

Output variables provide a convenient way to get useful information about your infrastructure. 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.

Resource instances managed by Terraform each export attributes whose values can be used elsewhere in configuration. Output values are a way to expose some of that information to the user of your module.

We can use output variables to organize data to be easily queried and shown back to the Terraform user.

While Terraform stores hundreds or thousands of attribute values for all our resources, we are more likely to be interested in a few values of importance, such as a load balancer IP, VPN address, etc.

Output values are like the return values of a Terraform module, and have several uses:

  • A child module can use outputs to expose a subset of its resource attributes to a parent module.
  • A root module can use outputs to print certain values in the CLI output after running terraform apply.
  • When using remote state, root module outputs can be accessed by other configurations via a terraform_remote_state data source.

Terraform example code for output value of string type

String Type – Terraform example code for output value of aws_instance public_dns


output "instance_public_dns" {
  value = aws_instance.example.public_dns
}

Map Type – Terraform example code for output value of aws_instance root_block_device


output "instance_root_block_device" {
  value = aws_instance.example.root_block_device[0].volume_id
}

List Type – Terraform example code for output value of aws_instance security group


output "instance_security_group" {
  value = aws_instance.example.security_groups[0]
}

Rajesh Kumar
Follow me