3.3. Outputs
Preparation
Finish the Variables exercise and navigate to the directory:
cd $LAB_ROOT/basics/variables
Step 3.3.1: Create outputs.tf
Create a new file named outputs.tf in your working directory and add the following content:
output "number" {
value = random_integer.number.result
description = "random value created by terraform"
}
Step 3.3.2: Apply the configuration
Run the command
terraform apply
and you should see output similar to this:
Plan: 0 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ number = 15670
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
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
number = 15670
Step 3.3.3: Access the output
If you just want to access the output value without running apply, you can just run:
terraform output number
terraform output -raw number
Can you spot the difference between the outputs?
Step 3.3.4: Handling sensitive output
Add the sensitive keyword to the outputs.tf file as followed:
output "number" {
value = "The number is ${random_integer.number.result}"
description = "random value created by terraform"
sensitive = true
}
This will mask the console output of the value. The output is still available by explicitly specifying the name as followed:
terraform output number
Try it out
You can also print the the output in json format and use tools like jq to process it further:
terraform output -json | jq '.number.value'
This is useful when handling large JSON data structures.
Note
terraform output can be used to create input or configuration for other CLI tools like Ansible.