Terraform Outputs Management

LinuxBeginner
Practice Now

Introduction

In Terraform, output values are a way to expose meaningful data about your resources after they have been deployed. They are like the return values of a Terraform module and can be used to easily query information such as an IP address of a server, a database connection string, or a file path. This makes the information easily accessible from the command line and allows other Terraform configurations to use it.

In this lab, you will learn how to define, apply, and view output values. We will start with a basic Terraform configuration that creates a local file and then add an output block to expose the file's path.

Create outputs.tf file for output definitions

In this step, we will create a dedicated file for our output definitions. While you can place output blocks in any .tf file, it is a common convention and best practice to place them in a separate file named outputs.tf. This keeps your configuration organized and makes it easy to find all the outputs for your project.

All your work will be done in the ~/project directory, which is the default directory in your terminal.

First, let's verify the main.tf file that has been pre-created for you.

ls -l

You should see the main.tf file and a .terraform directory.

total 4
-rw-rw-r-- 1 labex labex 279 Oct 15 12:39 main.tf

Now, create the outputs.tf file using the touch command.

touch outputs.tf

This command creates an empty file named outputs.tf in the current directory. You can verify its creation by listing the files again.

ls -l

You will now see outputs.tf in the file list.

total 4
-rw-rw-r-- 1 labex labex 279 Oct 15 12:39 main.tf
-rw-rw-r-- 1 labex labex   0 Oct 15 12:39 outputs.tf

Define output for local_file filename

In this step, you will define an output block in the outputs.tf file. An output block declares a single output value for your Terraform configuration.

The basic syntax for an output block is:

output "NAME" {
  ## Arguments go here
}

Here, NAME is a string that provides a name for the output, which you will use later to query its value.

Let's open the outputs.tf file with the nano editor and add our first output block.

nano outputs.tf

Now, add the following code to the file. We will name our output file_path.

output "file_path" {

}

After adding the code, save the file and exit nano by pressing Ctrl+X, then Y, and finally Enter.

This block doesn't do much yet, as it's missing a value argument. We will add that in the next step.

Reference resource attribute in output block

In this step, we will make our output useful by telling it what value to display. This is done using the value argument inside the output block. The value is typically a reference to an attribute of a resource defined in your configuration.

The syntax for referencing a resource attribute is <RESOURCE_TYPE>.<RESOURCE_NAME>.<ATTRIBUTE>.

In our main.tf file, we have a resource defined as:

resource "local_file" "example" {
  ## ...
}

The local_file resource has an attribute called filename, which holds the path to the file it manages. To reference this attribute, we use local_file.example.filename.

Let's add this reference to our output block. Open outputs.tf again with nano.

nano outputs.tf

Modify the file to include the value argument as shown below:

output "file_path" {
  value = local_file.example.filename
}

This tells Terraform that the file_path output should have the value of the filename attribute from the local_file resource named example.

Save the file and exit nano by pressing Ctrl+X, Y, and Enter.

Run terraform apply to update configuration

In this step, you will apply the configuration changes. Whenever you add, remove, or modify outputs, you must run terraform apply to have Terraform recognize the changes and update the state file. The state file is where Terraform stores the values of your outputs.

Run the terraform apply command in your terminal.

terraform apply

Terraform will first show you an execution plan. It will detect that you've added an output and will plan to add it. Since the local_file resource doesn't exist yet, it will also plan to create it.

You will be prompted to confirm the action. Type yes and press Enter.

Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  ## local_file.example will be created
  + resource "local_file" "example" {
      + content              = "This is an example file managed by Terraform."
      + content_base64sha256 = (known after apply)
      + content_base64sha512 = (known after apply)
      + content_md5          = (known after apply)
      + content_sha1         = (known after apply)
      + content_sha256       = (known after apply)
      + content_sha512       = (known after apply)
      + directory_permission = "0777"
      + file_permission      = "0777"
      + filename             = "./example.txt"
      + id                   = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + file_path = "./example.txt"

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

local_file.example: Creating...
local_file.example: Creation complete after 0s [id=ec3adcab998872def2df6200fb03992ac6f237a4]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

file_path = "./example.txt"

As you can see in the output, the apply was successful, and Terraform now displays the file_path output with its value.

Run terraform output to view filename

In this step, you will use the terraform output command to view the output values from your state file. This is useful for retrieving information about your infrastructure without having to parse the state file manually or re-run terraform apply.

To see all the outputs defined in your configuration, simply run the command without any arguments.

terraform output

This will display all outputs in a human-readable format.

file_path = "./example.txt"

You can also query for a specific output value by providing its name as an argument. This is useful in scripts or when you only need a single piece of information.

terraform output file_path

This command will print only the raw value of the specified output.

"./example.txt"

This makes it easy to use the output in shell scripts, for example, by assigning it to a variable: FILE=$(terraform output -raw file_path). The -raw flag removes the quotes.

Summary

Congratulations on completing the lab!

In this lab, you have learned the fundamentals of managing outputs in Terraform. You successfully:

  • Organized your configuration by creating a dedicated outputs.tf file.
  • Defined an output block to declare an output value.
  • Referenced a resource attribute within an output block to expose its value.
  • Applied the configuration using terraform apply to make the output available.
  • Queried the output value from the command line using terraform output.

Terraform outputs are a powerful feature for extracting important information from your infrastructure and integrating your Terraform workflows with other tools and scripts.