0
0
Terraformcloud~5 mins

Output declaration syntax 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. Output declarations let you show useful details like IP addresses or resource IDs automatically.
When you want to see the public IP address of a server you just created.
When you need to pass resource IDs from one Terraform module to another.
When you want to confirm the name of a created storage bucket after deployment.
When you want to display connection details for a database instance.
When you want to share outputs with your team without looking into the cloud console.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
}

provider "aws" {
  region = "us-east-1"
}

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

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

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

This Terraform file creates a small AWS EC2 instance.

The output blocks declare values to show after deployment:

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

The description helps explain what each output means.

Commands
This command sets up Terraform in the current folder by downloading necessary plugins and preparing the environment.
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 infrastructure defined in the configuration without asking for confirmation. It will also show the output values declared.
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 - Skip manual approval prompt to apply changes immediately
This command shows only the public IP address output value after deployment.
Terminal
terraform output instance_public_ip
Expected OutputExpected
54.210.123.45
Key Concept

If you remember nothing else from this pattern, remember: output declarations let you see and share important information about your created infrastructure automatically.

Common Mistakes
Not declaring an output block for a value you want to see after apply
Terraform will create the resource but won't show the value automatically, making it hard to find important info.
Always add an output block with a clear value expression for any key info you want to see.
Using incorrect resource references inside the output value
Terraform will fail to apply or show errors because it can't find the resource attribute.
Use the exact resource name and attribute as defined in your configuration, like aws_instance.example.id.
Expecting outputs to update automatically without reapplying
Outputs only refresh after terraform apply or terraform refresh commands.
Run terraform apply or terraform output commands again to see updated output values.
Summary
Use terraform init to prepare your working folder and download providers.
Define output blocks in your Terraform files to declare values you want to see after deployment.
Run terraform apply to create resources and automatically display output values.
Use terraform output to view specific output values anytime after deployment.