Terrafrom Show

The Terraform state file is the backbone of Terraform’s operations, containing all the information about your managed resources. But state files can be complex and difficult to read in raw form. The terraform show command provides a human-readable view of your state or plan files, making it easier to understand your infrastructure and debug issues.

Why Use terraform show?

  • To inspect the current state of your infrastructure.
  • To review execution plans before applying changes.
  • To debug and analyze resources managed by Terraform.

How to Use It?

  • To display the current state: Run terraform show. This outputs the latest state of your infrastructure in a readable format.
  • To view a specific state file: Use the -state flag, e.g., terraform show -state=example.tfstate.
  • To view a saved plan file: Provide the plan file name, e.g., terraform show example.tfplan.

Example: Viewing Terraform State

Suppose you have deployed an EC2 instance and want to inspect its current state. Running terraform show will display information like this:

# aws_instance.example:
resource "aws_instance" "example" {
    ami           = "ami-123456"
    instance_type = "t2.micro"
    id            = "i-0abcd1234efgh5678"
    tags = {
        Name = "example-instance"
    }
}

If you’ve saved a plan file to example.tfplan using terraform plan -out=example.tfplan, you can view its contents by running:

terraform show example.tfplan

This allows you to review the planned actions without applying them.

Use Case

Imagine you’re collaborating with a team, and someone wants to confirm the current configuration of a specific resource. By running terraform show, you can provide a detailed, easy-to-read summary of the resource’s state without exposing the raw state file.

Conclusion

The terraform show command makes it easier to understand and analyze your Terraform-managed resources. Whether inspecting the current state or reviewing a saved plan, this command is a valuable tool for maintaining visibility and control over your infrastructure.