Terraform Show vs Terraform State: Navigating State Presentation and Management

ยท

3 min read

Terraform Show  vs  Terraform State: Navigating State Presentation and Management

Introduction

In the vast landscape of Terraform, the terraform show and terraform state commands stand as distinctive tools, each offering unique perspectives and functionalities. This guide delves into the nuances between terraform show and terraform state, providing clarity on their purposes with new examples to illustrate their applications.

1. Terraform Show: Illuminating Resource Configurations

The terraform show command serves as an illuminator, shedding light on the human-readable representation of the Terraform state. It unveils the configuration details of provisioned resources, offering insights into their attributes, relationships, and configurations.

Example Scenario: AWS S3 Bucket Configuration

Consider a Terraform configuration for provisioning an AWS S3 bucket:

# main.tf

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
  bucket = "terraform-show-example-bucket"
  acl    = "private"
}

Executing terraform show allows users to inspect the configuration details of the provisioned AWS S3 bucket:

terraform show

Output:

# aws_s3_bucket.example:
resource "aws_s3_bucket" "example" {
  acl                         = "private"
  arn                         = "arn:aws:s3:::terraform-show-example-bucket"
  bucket                      = "terraform-show-example-bucket"
  bucket_domain_name          = "terraform-show-example-bucket.s3.amazonaws.com"
  bucket_regional_domain_name = "terraform-show-example-bucket.s3.us-east-1.amazonaws.com"
  force_destroy               = false
  hosted_zone_id              = "XXXXXXXXXXXXXX"
  id                          = "terraform-show-example-bucket"
  region                      = "us-east-1"
  tags                        = {}
  website_domain              = null
  website_endpoint            = null
}

2. Terraform State: Peering into Resource State

Contrastingly, the terraform state command is a multifaceted tool that provides users with insights into, and control over, the Terraform state. It enables users to perform actions like listing resources, showing detailed information, and even removing resources from the state.

Example Scenario: AWS VPC and Subnets

Consider a Terraform configuration for provisioning an AWS VPC and its associated subnets:

# main.tf

provider "aws" {
  region = "us-east-1"
}

resource "aws_vpc" "example_vpc" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "example_subnet" {
  vpc_id                  = aws_vpc.example_vpc.id
  cidr_block              = "10.0.1.0/24"
  availability_zone       = "us-east-1a"
  map_public_ip_on_launch = true
}

Commands:

  • terraform state list: Lists all resources in the Terraform state.

      terraform state list
    
  • terraform state show aws_subnet.example_subnet: Shows detailed information about a specific resource.

      terraform state show aws_subnet.example_subnet
    

Outputs:

List Command Output:

aws_subnet.example_subnet
aws_vpc.example_vpc

Show Command Output:

# aws_subnet.example_subnet:
resource "aws_subnet" "example_subnet" {
  availability_zone       = "us-east-1a"
  cidr_block              = "10.0.1.0/24"
  id                      = "subnet-0123456789abcdef0"
  map_public_ip_on_launch = true
  vpc_id                  = "vpc-0123456789abcdef0"
}

3. Key Differences: Show vs. State

3.1. Purpose

  • Show: Presents a human-readable representation of the Terraform state.

  • State: Provides insights into and control over the Terraform state.

3.2. Usage

  • Show: Used for inspecting resource configurations.

  • State: Used for various operations on the Terraform state, such as listing, showing, pulling, or removing resources.

3.3. Output

  • Show: Human-readable output for resource configurations.

  • State: Detailed information about resources in a format suitable for further processing.

4. Conclusion: Navigating Terraform State with Precision

In the Terraform journey, terraform show and terraform state are invaluable companions, each offering unique perspectives on infrastructure configurations and state management. Whether illuminating the human-readable representation of resource configurations or providing insights into and control over the Terraform state, these commands play vital roles in the Terraform ecosystem.

May your exploration of Terraform configurations and states be guided by the precision of terraform show and the versatility of terraform state! ๐ŸŒ๐Ÿ”๐Ÿ’ก

ย