terraform/website/intro/getting-started/outputs.html.md

83 lines
2.4 KiB
Markdown
Raw Normal View History

2014-07-28 19:43:00 +02:00
---
layout: "intro"
page_title: "Output Variables"
sidebar_current: "gettingstarted-outputs"
2014-10-22 05:21:56 +02:00
description: |-
In the previous section, we introduced input variables as a way to parameterize Terraform configurations. In this page, we introduce output variables as a way to organize data to be easily queried and shown back to the Terraform user.
2014-07-28 19:43:00 +02:00
---
# Output Variables
In the previous section, we introduced input variables as a way
to parameterize Terraform configurations. In this page, we
introduce output variables as a way to organize data to be
easily queried and shown back to the Terraform user.
When building potentially complex infrastructure, Terraform
stores hundreds or thousands of attribute values for all your
resources. But as a user of Terraform, you may only be interested
in a few values of importance, such as a load balancer IP,
VPN address, etc.
Outputs are a way to tell Terraform what data is important.
This data is outputted when `apply` is called, and can be
queried using the `terraform output` command.
## Defining Outputs
Let's define an output to show us the public IP address of the
elastic IP address that we create. Add this to any of your
`*.tf` files:
2017-04-06 20:02:56 +02:00
```hcl
2014-07-28 19:43:00 +02:00
output "ip" {
2017-04-06 20:02:56 +02:00
value = "${aws_eip.ip.public_ip}"
2014-07-28 19:43:00 +02:00
}
```
This defines an output variable named "ip". The `value` field
2014-07-28 19:43:00 +02:00
specifies what the value will be, and almost always contains
one or more interpolations, since the output data is typically
dynamic. In this case, we're outputting the
`public_ip` attribute of the elastic IP address.
Multiple `output` blocks can be defined to specify multiple
output variables.
## Viewing Outputs
Run `terraform apply` to populate the output. This only needs
to be done once after the output is defined. The apply output
should change slightly. At the end you should see this:
```
$ terraform apply
...
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
ip = 50.17.232.209
```
`apply` highlights the outputs. You can also query the outputs
after apply-time using `terraform output`:
```
$ terraform output ip
50.17.232.209
```
This command is useful for scripts to extract outputs.
## Next
You now know how to parameterize configurations with input
variables, extract important data using output variables,
and bootstrap resources using provisioners.
Next, we're going to take a look at
[how to use modules](/intro/getting-started/modules.html), a useful
abstraction to organization and reuse Terraform configurations.