Terraform configurations often produce valuable data, like IP addresses, resource IDs, or other details generated during deployment. The terraform output
command allows you to extract and display this information in a readable format, making it easier to share or integrate into other workflows.
terraform output
. This lists all outputs defined in your configuration.terraform output <output_name>
, replacing <output_name>
with the name of the output you want to retrieve.-state
flag, e.g., terraform output -state=example.tfstate
.-json
flag, e.g., terraform output -json
.Suppose your configuration includes an output block for an EC2 instance's public IP address:
output "instance_ip" {
value = aws_instance.example.public_ip
}
After deploying the infrastructure, you can retrieve the public IP with:
terraform output instance_ip
Output:
54.123.45.67
To list all outputs, run:
terraform output
Output:
instance_ip = 54.123.45.67
instance_id = i-0abcd1234efgh5678
If you need the output in JSON format for integration with other tools, run:
terraform output -json
Output:
{
"instance_ip": {
"value": "54.123.45.67",
"type": "string"
},
"instance_id": {
"value": "i-0abcd1234efgh5678",
"type": "string"
}
}
Imagine you’re setting up a pipeline that needs the public IP of an EC2 instance to configure DNS records. By defining and retrieving the output with terraform output
, you can pass this information directly into your automation scripts, simplifying the process.
The terraform output
command bridges the gap between infrastructure deployment and integration. By making essential resource information accessible, it ensures your workflows remain efficient and interconnected.