Outputs Explained

In this lesson, you'll learn how to write an output in Terraform, how they work, and how they are defined.

Terraform project output#

Output in your Terraform project shows a piece of data after Terraform successfully completes. Outputs are useful as they allow you to echo values from the Terraform run to the command line. For example, if you are creating an environment and setting up a bastion jump box as part of that environment, it’s handy to be able to echo the public IP address of the newly created bastion to the command line. Then, after the terraform apply finishes, you are given the IP of the newly created bastion which is ready for you to ssh straight onto it.

Terraform project output example#

Let’s start with an example of outputs:

Terraform project example

Try running this project by connecting with the terminal. After running terraform init and terraform apply. You will see that Terraform runs and then prints the following:

This code requires the following environment variables to execute:
access_key_id
Not Specified...
secret_access_key
Not Specified...
Terminal 1
Terminal

Click to Connect...

📝Note: Once you are done with the project, do not forget to run terraform destroy and then confirm with yes to delete all of the resources in this project.

Let’s dive into the project output#

You can see from the message above that Terraform states that nothing changed (0 added, 0 changed, 0 destroyed.). You then see Outputs: under which Terraform prints out the values of all of the outputs you have defined. We defined a single output with the identifier message and gave it the value Hello world, so that is what Terraform printed.

Output definition in Terraform#

To define an output, you open an output block by using the output keyword. You then start the output block with {. You are only allowed to set a single property called value. Whatever value you give to the value property will be outputted to the console after a successful terraform apply. You then close the output block with }.

📝Note: Outputs are used in modules too and have slightly different semantics. This will be covered in the chapter on modules.

🎉Quiz
Outputting Resource Properties
Mark as Completed
Report an Issue