0
0
Terraformcloud~5 mins

Local state behavior in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform keeps track of the resources it creates using a local state file. This file records what exists so Terraform can update or delete resources correctly later. Without this file, Terraform would not know what it has done before.
When you want to manage infrastructure on your own computer without sharing state with others
When you are testing Terraform configurations locally before using remote state
When working on small projects or learning Terraform without needing collaboration
When you want to quickly see how Terraform tracks resources on your machine
When you want to keep state files simple and stored alongside your configuration
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
}

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

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

This Terraform configuration creates an AWS S3 bucket named "example-terraform-local-state" in the us-east-1 region.

The terraform block sets the minimum Terraform version.

The provider block configures AWS with the region.

The resource block defines the S3 bucket to be created.

When you run Terraform commands, it will store the state locally in a file named terraform.tfstate in the same directory.

Commands
This command initializes the Terraform working directory. It downloads the AWS provider plugin and prepares Terraform to run.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. If you ever set or change modules or backend configuration, rerun this command to reinitialize your working directory.
This command applies the configuration to create the S3 bucket. The local state file is created or updated to record this resource.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example: Creating... aws_s3_bucket.example: Creation complete after 3s [id=example-terraform-local-state] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without prompting
This command shows the current state of the infrastructure as recorded in the local state file.
Terminal
terraform show
Expected OutputExpected
aws_s3_bucket.example: id = example-terraform-local-state arn = arn:aws:s3:::example-terraform-local-state bucket = example-terraform-local-state acl = private
This command deletes the resources defined in the configuration and updates the local state file to remove them.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
aws_s3_bucket.example: Destroying... aws_s3_bucket.example: Destruction complete after 2s Destroy complete! Resources: 1 destroyed.
-auto-approve - Automatically approve the destroy without prompting
Key Concept

If you remember nothing else from this pattern, remember: Terraform uses a local state file to keep track of your resources so it knows what to create, update, or delete.

Common Mistakes
Deleting the local state file manually
Terraform loses track of existing resources and may try to recreate or orphan them, causing conflicts or resource duplication.
Use 'terraform destroy' to clean up resources and state safely instead of deleting the state file.
Running Terraform commands from a different directory without the state file
Terraform cannot find the local state file and assumes no resources exist, leading to unexpected resource creation.
Always run Terraform commands from the directory containing the state file or configure remote state.
Summary
Initialize Terraform with 'terraform init' to prepare the working directory.
Apply infrastructure changes with 'terraform apply' which creates or updates the local state file.
Use 'terraform show' to view the current state recorded locally.
Destroy resources safely with 'terraform destroy' which updates the local state accordingly.