0
0
Terraformcloud~5 mins

Output values after apply in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you create resources with Terraform, you often want to see important information about them after they are created. Output values let you display this information clearly after running your setup.
When you want to see the IP address of a server you just created.
When you need to get the URL of a website hosted on cloud resources.
When you want to share resource IDs with other teams or tools.
When you want to verify that your resources have the expected properties.
When you want to pass information from one Terraform configuration to another.
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 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 server instance.

The output blocks define values to show after the resources are created:

  • instance_id shows the unique ID of the server.
  • instance_public_ip shows the server's public IP address.
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 resources defined in the configuration and automatically approves the changes without 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. Outputs: instance_id = "i-0abcd1234efgh5678" instance_public_ip = "54.210.123.45"
-auto-approve - Automatically approve the apply without manual confirmation
This command shows only the public IP address output value from the last apply, useful for quick access.
Terminal
terraform output instance_public_ip
Expected OutputExpected
54.210.123.45
This command shows all output values defined in the configuration after the last apply.
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: output values let you see and share important information about your created resources after Terraform finishes.

Common Mistakes
Not defining output blocks in the Terraform configuration.
Without output blocks, Terraform will not show any useful information after apply.
Always add output blocks for any resource details you want to see or share.
Running terraform output before terraform apply.
No outputs exist before resources are created, so the command returns empty or errors.
Run terraform apply first to create resources and generate outputs.
Using terraform apply without -auto-approve and expecting no prompt.
Terraform will pause and ask for confirmation, which can interrupt automated scripts.
Use -auto-approve flag in scripts to skip confirmation.
Summary
Define output blocks in your Terraform files to show resource details after creation.
Run terraform apply to create resources and see outputs immediately.
Use terraform output commands to view specific or all output values anytime after apply.