Bird
Raised Fist0
Terraformcloud~5 mins

Local state behavior in Terraform - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does Terraform's local state file primarily store?
easy
A. Information about resources Terraform manages on your computer
B. User credentials for cloud providers
C. Terraform configuration files
D. Logs of Terraform commands executed

Solution

  1. Step 1: Understand Terraform state purpose

    The state file keeps track of resources Terraform creates or manages.
  2. Step 2: Identify local state file role

    Local state stores this info on your computer for Terraform to know current resource status.
  3. Final Answer:

    Information about resources Terraform manages on your computer -> Option A
  4. Quick Check:

    Local state = resource info stored locally [OK]
Hint: Local state stores resource info on your PC, not configs or logs [OK]
Common Mistakes:
  • Confusing state file with configuration files
  • Thinking state stores user credentials
  • Assuming state file contains command logs
2. Which of the following is the correct way to specify a local state file path in Terraform CLI?
easy
A. terraform apply -state-file=./myterraform.tfstate
B. terraform apply --state-file ./myterraform.tfstate
C. terraform apply --state ./myterraform.tfstate
D. terraform apply -state=./myterraform.tfstate

Solution

  1. Step 1: Recall Terraform CLI syntax for state file

    The correct flag to specify state file path is '-state=path'.
  2. Step 2: Match the correct option

    terraform apply -state=./myterraform.tfstate uses '-state=./myterraform.tfstate' which is valid syntax.
  3. Final Answer:

    terraform apply -state=./myterraform.tfstate -> Option D
  4. Quick Check:

    Use '-state=' flag to specify local state file [OK]
Hint: Use '-state=' flag to set local state file path [OK]
Common Mistakes:
  • Using '--state-file' which is invalid
  • Using '--state' without '=' sign
  • Confusing flag names
3. Given this Terraform command sequence on a local state setup:
terraform init
terraform apply
terraform state rm aws_instance.example
terraform apply

What happens after the second terraform apply?
medium
A. Terraform deletes the aws_instance.example resource from the cloud
B. Terraform ignores aws_instance.example and makes no changes
C. Terraform recreates the aws_instance.example resource
D. Terraform throws an error about missing resource

Solution

  1. Step 1: Understand 'terraform state rm' effect

    This command removes the resource from the state file but does not delete it in the cloud.
  2. Step 2: Analyze second 'terraform apply'

    Terraform sees the resource missing in state, so it plans to create it again.
  3. Final Answer:

    Terraform recreates the aws_instance.example resource -> Option C
  4. Quick Check:

    Removing from state causes resource to be recreated [OK]
Hint: Removing resource from state makes Terraform recreate it [OK]
Common Mistakes:
  • Thinking 'state rm' deletes resource in cloud
  • Assuming Terraform ignores missing state entries
  • Expecting an error on apply
4. You run Terraform with local state and get this error:
Error: Failed to load state: open terraform.tfstate: permission denied
What is the most likely cause and fix?
medium
A. The state file permissions prevent access; fix file permissions
B. The state file is locked by another process; wait and retry
C. The state file is corrupted; delete and reinitialize Terraform
D. Terraform version mismatch; upgrade Terraform to latest

Solution

  1. Step 1: Interpret the error message

    'permission denied' means Terraform cannot read or write the state file due to OS permissions.
  2. Step 2: Determine the fix

    Adjust file permissions to allow Terraform access to the state file.
  3. Final Answer:

    The state file permissions prevent access; fix file permissions -> Option A
  4. Quick Check:

    Permission denied = fix file permissions [OK]
Hint: Permission denied means fix file access rights [OK]
Common Mistakes:
  • Assuming state file is locked by another process
  • Deleting state file without backup
  • Blaming Terraform version without checking permissions
5. You want to share your Terraform project with a teammate but keep using local state. What is the best practice to avoid state conflicts?
hard
A. Share the local state file directly via email or chat
B. Use a remote backend like Terraform Cloud or S3 for state storage
C. Each teammate runs Terraform independently with their own local state
D. Manually merge local state files before each apply

Solution

  1. Step 1: Understand local state sharing risks

    Local state files are not designed for sharing; conflicts and overwrites can occur.
  2. Step 2: Identify best practice for team collaboration

    Using a remote backend centralizes state and prevents conflicts.
  3. Final Answer:

    Use a remote backend like Terraform Cloud or S3 for state storage -> Option B
  4. Quick Check:

    Remote backend avoids local state conflicts in teams [OK]
Hint: Use remote backend to share state safely in teams [OK]
Common Mistakes:
  • Sharing local state file directly causing conflicts
  • Each teammate using separate local states causing drift
  • Trying to manually merge state files