0
0
Terraformcloud~5 mins

Terraform output command - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform output command helps you see the values that your infrastructure creates. It shows useful information like IP addresses or URLs after your resources are ready.
When you want to find the IP address of a server you just created.
When you need to get the URL of a load balancer after deployment.
When you want to share important resource details with your team.
When you want to use output values as inputs for other tools or scripts.
When you want to verify that your infrastructure created the expected resources.
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" {
  value       = aws_instance.example.id
  description = "The ID of the AWS EC2 instance"
}

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

This file creates a small AWS server (EC2 instance) in the us-east-1 region.

The output blocks define values to show after deployment: the instance ID and its public IP address.

This helps you easily find important details about your new server.

Commands
This command sets up Terraform in your folder. It downloads the AWS provider plugin so Terraform can talk to AWS.
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 required for your infrastructure.
This command creates the AWS server defined in the config file. The -auto-approve flag skips asking for confirmation.
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.
-auto-approve - Skip manual approval to apply changes immediately
This command shows all the output values defined in the config file after deployment.
Terminal
terraform output
Expected OutputExpected
instance_id = "i-0abcd1234efgh5678" instance_public_ip = "54.210.123.45"
This command shows only the public IP address of the instance, useful when you want a specific value.
Terminal
terraform output instance_public_ip
Expected OutputExpected
"54.210.123.45"
Key Concept

If you remember nothing else from this pattern, remember: Terraform output command lets you see important details about your created resources easily.

Common Mistakes
Running terraform output before terraform apply
No resources exist yet, so no output values are available.
Always run terraform apply first to create resources before checking outputs.
Not defining output blocks in the config file
Terraform output command will show nothing if no outputs are defined.
Add output blocks in your Terraform config to specify which values to display.
Expecting terraform output to update automatically after changes
Outputs reflect the last applied state, so if you change resources, you must run terraform apply again.
Run terraform apply after changes, then use terraform output to see updated values.
Summary
Use terraform init to prepare your folder and download necessary plugins.
Use terraform apply to create or update your infrastructure.
Define output blocks in your config to specify which resource details to show.
Use terraform output to see all or specific output values after deployment.