Exploring the 'terraform output' Command (with examples)
Terraform is an open-source infrastructure-as-code software tool that provides a consistent CLI workflow to manage infrastructure across various service providers. It works by defining cloud resources as code files and using those files to create the actual resources. One powerful aspect of Terraform’s CLI (Command-Line Interface) is the ‘terraform output
’ command, which allows users to query the outputs of their Terraform configurations. This command provides insights about deployed resources, helping users interact more effectively with the infrastructure.
Use case 1: Displaying All Outputs for the Root Module
Code:
terraform output
Motivation:
This use case is useful when you want a comprehensive overview of your deployed infrastructure. By executing terraform output
without any additional arguments, you can quickly obtain all output values defined in the root module of your Terraform configurations. This might include IP addresses, hostnames, or resource IDs that are essential for further manual configurations or troubleshooting.
Explanation:
- The command
terraform
refers to the Terraform CLI. - The argument
output
specifies that we are invoking the output command, which is dedicated to displaying values that have been defined as outputs in the Terraform configuration files.
Example Output:
database_url = "postgres://user:password@12.34.56.78:5432/mydatabase"
instance_ip = "34.229.24.9"
bucket_name = "my-terraform-bucket"
Here, each output name, like database_url
, is associated with its respective value, providing users with meaningful data immediately after deployment.
Use case 2: Outputting a Specific Value by Name
Code:
terraform output name
Motivation:
In scenarios where you are interested in a particular piece of information, such as a specific resource’s attribute that is crucial for further steps in your workflow, you can streamline your command to output just that one value. This use case is beneficial when you want to reduce clutter and retrieve only what’s necessary for your immediate needs.
Explanation:
name
is a placeholder for the specific output value you are interested in. This should be replaced with the actual output name as defined in your Terraform configuration (e.g.,instance_ip
).
Example Output:
34.229.24.9
By targeting a specific output, this approach offers a concise view, displaying only the desired information.
Use case 3: Converting the Output Value to a Raw String
Code:
terraform output -raw name
Motivation:
When integrating Terraform outputs within scripts, especially shell scripts, it’s often more efficient to handle the data as raw strings without additional formatting or extraneous characters. This simplifies parsing and makes it easier to use the output values as inputs for other command-line tools.
Explanation:
- The flag
-raw
directs Terraform to return the output as a raw string, which strips any additional formatting. name
is again a placeholder and should be replaced with the specific output name.
Example Output:
34.229.24.9
This uncluttered output is ideal for command substitution or other script-based operations where a plain string format is preferred.
Use case 4: Formatting Outputs as a JSON Object
Code:
terraform output -json
Motivation:
When working with complex data processing or automation tools, JSON formatting is a universal standard that is both human-readable and machine-friendly. By exporting outputs in JSON format, users can easily pass this structured data into other systems, such as analysis tools or further provisioning scripts, using programs like jq
.
Explanation:
- The flag
-json
instructs Terraform to present the outputs as a JSON object, providing structured data suitable for subsequent processing.
Example Output:
{
"database_url": {
"sensitive": false,
"type": "string",
"value": "postgres://user:password@12.34.56.78:5432/mydatabase"
},
"instance_ip": {
"sensitive": false,
"type": "string",
"value": "34.229.24.9"
},
"bucket_name": {
"sensitive": false,
"type": "string",
"value": "my-terraform-bucket"
}
}
This JSON output delivers a detailed description of each value, making it easily consumable by other applications or services.
Conclusion
The terraform output
command is an essential tool in managing infrastructure provisioned with Terraform. By mastering its varied use cases—from viewing all outputs to focusing on specific values or desired formats—users can significantly enhance their infrastructure management workflows, enabling seamless integrations across various systems and scripts. The command’s flexibility ensures that you can always get the data you need in the form that’s most useful for your circumstances.