🐶
Terraform

Print Terraform Output in Terminal

By Ondřej Dolanský on 01/05/2025

Learn how to easily print the rendered output of your Terraform template directly to your terminal, making debugging and validation a breeze.

Print Terraform Output in Terminal

Table of Contents

Introduction

When working with Terraform, you might be tempted to preview the rendered output of your templates before actually applying them. However, Terraform doesn't offer a direct command for this purpose.

Step-by-Step Guide

You can't directly output rendered Terraform templates in the terminal before applying them.

Terraform doesn't have a built-in command to show the rendered output of a template file.

To see the results of your template, you need to run terraform apply.

During the apply process, Terraform will render the template and display the final values in the terminal output.

For example, if you have a template file like this:

resource "aws_instance" "example" {
  ami           = "ami-0c94855ba95c574c9"
  instance_type = "t2.micro"
  tags = {
    Name = "${var.environment}-instance"
  }
}

After running terraform apply, you'll see the rendered value of Name tag (e.g., "prod-instance") in the output.

If you need to debug templates, you can use tools like echo with string interpolation in your shell to test parts of your template logic.

For example:

$ export environment="test"
$ echo "Name = ${environment}-instance"
Name = test-instance

Remember that this won't reflect the full complexity of Terraform's interpolation and functions.

Code Example

This code snippet demonstrates a simple Terraform template that creates an AWS instance with a dynamically generated name tag based on an environment variable. It shows how to apply the template using the Terraform CLI and provides a basic debugging technique using shell commands to test template interpolation. The example highlights that this debugging method is limited and suggests more advanced options like Terraform console or testing frameworks for complex scenarios.

This example demonstrates how Terraform renders templates during the apply process and how to debug template logic using shell commands.

1. main.tf (Terraform Template)

resource "aws_instance" "example" {
  ami           = "ami-0c94855ba95c574c9"
  instance_type = "t2.micro"
  tags = {
    Name = "${var.environment}-instance"
  }
}

2. variables.tf (Define the 'environment' variable)

variable "environment" {
  type = string
}

3. Run Terraform Apply

terraform apply -var="environment=prod"

This command will apply your Terraform configuration. During the process, you'll see the rendered output of your template, including the final value of the Name tag (e.g., "prod-instance").

4. Debugging Template Logic with Shell Commands

You can use shell commands like echo with string interpolation to test parts of your template logic.

# Set the environment variable
export environment="test"

# Test the Name tag interpolation
echo "Name = ${environment}-instance"

This will output:

Name = test-instance

Important Notes:

  • This method of debugging is limited and won't reflect the full complexity of Terraform's interpolation and functions.
  • For more complex debugging, consider using tools like terraform console or third-party Terraform testing frameworks.

Additional Notes

  • Terraform's Philosophy: Terraform prioritizes showing you the effects of your code rather than the literal rendered code itself. This encourages focusing on infrastructure as code's desired state.
  • terraform plan: While not showing the fully rendered template, terraform plan gives a detailed view of what actions will be taken, including interpolated values. This is often sufficient for debugging.
  • Third-party Tools: Tools like terraform-compliance, tflint, and checkov can analyze your code and potentially offer insights into rendered values during static analysis.
  • Use Cases for Direct Rendering: There are limited scenarios where seeing the fully rendered template is crucial. Most needs are met by plan output or debugging individual expressions.
  • Security Considerations: Be cautious about directly printing rendered templates, especially when sensitive values are involved. This could expose secrets unintentionally.
  • Alternative Approaches:
    • Modularization: Break down complex templates into smaller, reusable modules. This improves readability and makes debugging easier.
    • Comments: Use comments liberally within your templates to explain logic and expected outputs.
    • Version Control: Leverage Git or other version control systems to track changes and revert to previous states if needed.

Summary

Feature Description
Direct Template Output Not supported by Terraform CLI.
Viewing Rendered Templates Rendered output is shown in the terminal during terraform apply.
Example A template with ${var.environment}-instance will show the final value (e.g., "prod-instance") after apply.
Debugging Templates Use shell commands like echo with string interpolation for basic testing.
Limitations Shell-based debugging doesn't cover the full scope of Terraform's interpolation and functions.

Conclusion

While you can't directly view rendered Terraform templates before deployment, understanding the rendering process is crucial. Terraform's approach emphasizes the outcome over the verbatim rendered code. Leverage terraform plan to preview changes, employ shell commands for basic template logic testing, and consider advanced tools like terraform console for complex debugging. Remember to prioritize security and explore alternative approaches like modularization and thorough commenting to enhance your infrastructure-as-code workflow.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait