0
0
Terraformcloud~5 mins

Outputs as documentation in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you create infrastructure with Terraform, you often want to see important information after it finishes. Outputs let you show key details like IP addresses or URLs. This helps you understand what was created without searching through all the resources.
When you want to see the public IP address of a server after deployment.
When you need to share the URL of a load balancer with your team.
When you want to confirm the ID of a created database instance.
When you want to pass information from one Terraform module to another.
When you want to document important values for future reference.
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "example-instance"
  }
}

output "instance_id" {
  description = "The ID of the example instance"
  value       = aws_instance.example.id
}

output "instance_public_ip" {
  description = "The public IP address of the example instance"
  value       = aws_instance.example.public_ip
}

This Terraform file creates a simple AWS EC2 instance.

The output blocks define values to show after deployment:

  • instance_id shows the unique ID of the instance.
  • instance_public_ip shows the public IP address assigned.

These outputs help you quickly find important info without digging into the AWS console.

Commands
This command sets up Terraform in the current folder. It downloads the AWS provider plugin needed to create resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command creates the resources defined in the configuration. The -auto-approve flag skips the confirmation prompt to speed up deployment.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 15s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: instance_id = "i-0abcd1234efgh5678" instance_public_ip = "54.210.123.45"
-auto-approve - Automatically approve the apply without asking for confirmation
This command shows the output values defined in the configuration. It helps you quickly see important information about your deployed resources.
Terminal
terraform output
Expected OutputExpected
instance_id = "i-0abcd1234efgh5678" instance_public_ip = "54.210.123.45"
Key Concept

If you remember nothing else from this pattern, remember: Terraform outputs let you easily see and share important details about your infrastructure after deployment.

Common Mistakes
Not defining output blocks for important resource attributes.
Without outputs, you have to search through resource details or cloud consoles to find key info.
Always add output blocks for values you want to see or share after deployment.
Trying to access output values before running terraform apply.
Outputs depend on created resources, so they don't exist until after apply.
Run terraform apply first, then use terraform output to see values.
Summary
Use terraform init to prepare your working folder and download providers.
Run terraform apply to create resources and see output values immediately.
Use terraform output anytime to view important information about your deployed infrastructure.