Query data with outputs
In the previous tutorial, you used an input variable to parameterize your Terraform configuration. In this tutorial, you will use output values to organize data to be easily queried and displayed to the Terraform user.
If you have not yet completed the Define Input Variables tutorial, do so before following this one.
Initial configuration
After following the previous tutorials, you will have a
directory named learn-terraform-docker-container
with the following configuration.
Ensure that your configuration matches this, and that you have initialized your configuration in the learn-terraform-docker-container
directory.
Apply the configuration before continuing this tutorial. Respond to the confirmation prompt with a yes
.
Output Docker container configuration
Create a file called outputs.tf
in your learn-terraform-docker-container
directory.
Add the configuration below to outputs.tf
to define outputs for your container's ID and the image ID.
Inspect output values
You must apply this configuration before you can use these output values. Apply
your configuration now. Respond to the confirmation prompt with yes
.
Terraform prints output values to the screen when you apply your configuration.
Query the outputs with the terraform output
command.
You can use Terraform outputs to connect your Terraform projects with other parts of your infrastructure, or with other Terraform projects. To learn more, follow our in-depth tutorial, Output Data from Terraform.
Destroy infrastructure
Destroy your infrastructure. Respond to the confirmation prompt with yes
.
Next steps
That concludes the getting started tutorials for Terraform. Hopefully you're now able to not only see what Terraform is useful for, but you're also able to put this knowledge to use to improve building your own infrastructure.
For more hands-on experience with the Terraform configuration language, or to learn more of the building blocks of Terraform, review the tutorials below.
Configuration Language - Get more familiar with variables, outputs, dependencies, meta-arguments, and other language features to write more sophisticated Terraform configurations. These tutorials use the AWS provider. To configure your AWS credentials, use the AWS Get Started Build tutorial.
Modules - Organize and re-use Terraform configuration with modules.
Provision - Use Packer or Cloud-init to automatically provision SSH keys and a web server onto a Linux VM created by Terraform in AWS.
Import - Import existing infrastructure into Terraform.
To read more about available configuration options, explore the Terraform documentation.
Learn more about HCP Terraform
Although HCP Terraform can act as a standard remote backend to support Terraform runs on local machines, it works even better as a remote run environment. It supports two main workflows for performing Terraform runs:
- A VCS-driven workflow, in which it automatically queues plans whenever changes are committed to your configuration's VCS repo.
- An API-driven workflow, in which a CI pipeline or other automated tool can upload configurations directly.
For a hands-on introduction to the HCP Terraform VCS-driven workflow, follow the HCP Terraform getting started tutorials. HCP Terraform also offers commercial solutions which include team permission management, policy enforcement, agents, and more.