DevOpsLesson
DevOpsLesson

Free, comprehensive DevOps tutorials and learning roadmaps. Master Docker, Kubernetes, CI/CD, and more.

Stay Updated

Get notified about new tutorials and features.

Tutorials

  • What is DevOps?
  • Docker Tutorial
  • Terraform Tutorial
  • CI/CD Pipeline
  • All Tutorials

Roadmaps

  • DevOps Engineer
  • Cloud Engineer
  • SRE Path
  • All Roadmaps

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 DevOpsLesson. All rights reserved.

DOCKERKUBERNETESTERRAFORMAWSCI/CDLINUXGITDEVOPS ROADMAPCLOUD ROADMAPSRE ROADMAPGIT CHEATSHEETDOCKER CHEATSHEETK8S CHEATSHEETTF CHEATSHEETLINUX CHEATSHEETDOCKERFILE LINTERYAML VALIDATORCRON PARSERREGEX TESTER

Terraform Tutorial

Introduction to Terraform
Installing Terraform
The Terraform Core Workflow
Terraform Variables and Outputs
Terraform State and Backends
Terraform Modules
Terraform Data Sources
Terraform Count and for_each
Terraform Expressions and Functions
Terraform Dynamic Blocks
Terraform Workspaces
Terraform with AWS
Terraform Provisioners
Terraform CI/CD Pipeline
Terraform Security

Terraform State Overview

PreviousPrev
Next

Learn what Terraform state is, why the terraform.tfstate file exists, how it maps to real infrastructure, and why state security and locking matter in production.

What Is Terraform State?

Terraform state is the record Terraform keeps of the infrastructure it manages. Every time Terraform creates, updates, or destroys resources, it writes information about those resources into state.

That state is usually stored in a file named terraform.tfstate when you are using local state. The file is machine-readable JSON, but the important idea is not the file format. The important idea is that Terraform needs a reliable memory of what it manages.

Without state, Terraform would have a serious problem: it could read your configuration, but it would not know whether the resources described in that configuration already exist, whether it created them before, or how those resources map to real objects in AWS, Azure, GCP, or another provider.

State solves that problem.

Why Terraform State Exists

At a beginner level, it is tempting to think Terraform simply reads your .tf files and sends create or update requests to the cloud. In reality, Terraform constantly compares three things:

  1. Your configuration: what you want
  2. The state: what Terraform believes it manages
  3. The real infrastructure: what actually exists in the provider

This comparison is what makes terraform plan possible.

Example: creating a VPC

Suppose your configuration contains this resource:

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

On the first terraform apply, Terraform creates the VPC in AWS. AWS returns a real resource ID such as vpc-0abc123def456. Terraform then writes that mapping into state.

Now Terraform knows:

  • the logical Terraform address is aws_vpc.main
  • the real AWS object is vpc-0abc123def456
  • the VPC has attributes such as CIDR block, default route table IDs, and more

On the next terraform plan, Terraform uses state to understand that aws_vpc.main already exists and should be compared, not recreated.

The terraform.tfstate File

When using local state, Terraform stores state in a file named terraform.tfstate in your working directory.

A simplified example looks like this:

{
  "version": 4,
  "terraform_version": "1.9.0",
  "serial": 12,
  "lineage": "9f8c7d6e-1234-5678-9abc-def012345678",
  "outputs": {
    "vpc_id": {
      "value": "vpc-0abc123def456",
      "type": "string"
    }
  },
  "resources": [
    {
      "mode": "managed",
      "type": "aws_vpc",
      "name": "main",
      "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
      "instances": [
        {
          "attributes": {
            "id": "vpc-0abc123def456",
            "cidr_block": "10.0.0.0/16"
          }
        }
      ]
    }
  ]
}

You do not need to memorize every field, but it helps to understand what state generally contains.

What the state file stores

Terraform state commonly includes:

  • resource addresses such as aws_instance.web
  • provider-specific IDs returned by the cloud API
  • resource attributes
  • dependency relationships
  • metadata about the state itself
  • output values

In other words, state is the data structure Terraform uses to map your code to real infrastructure.

How State Maps to Real Infrastructure

This mapping is the heart of Terraform.

Your Terraform configuration uses logical names:

resource "aws_instance" "web" {
  ami           = "ami-0abcdef1234567890"
  instance_type = "t3.micro"
}

Here, aws_instance.web is a Terraform address. AWS does not know that name. AWS only knows the actual EC2 instance ID, such as i-0123456789abcdef0.

State connects those two worlds.

Terraform knowsProvider knows
aws_instance.webi-0123456789abcdef0
aws_vpc.mainvpc-0abc123def456
aws_s3_bucket.logsbucket name / ARN

Because of this mapping, Terraform can safely update an existing resource instead of creating a duplicate.

Why this matters during changes

Suppose you change:

instance_type = "t3.micro"

to:

instance_type = "t3.small"

Terraform reads state, sees which real EC2 instance corresponds to aws_instance.web, and asks the provider whether that instance can be updated in place or must be replaced.

Without state, Terraform would not know which object in AWS should be changed.

State and Drift Detection

Drift happens when real infrastructure changes outside Terraform.

For example, imagine someone manually changes a security group rule in the AWS console. Your .tf files still declare the original rule, but reality is now different.

During terraform plan, Terraform refreshes what it knows about the real infrastructure, compares that to state and configuration, and can detect the difference.

That is why Terraform is so valuable for infrastructure as code: your .tf files remain the source of truth, and state gives Terraform the context needed to identify drift.

Local State vs Remote State

By default, Terraform stores state locally in terraform.tfstate in the current working directory. This is called local state.

Local state is simple and great for learning, experimentation, or solo work. But it has important limitations.

Local state advantages

  • easy to understand
  • no backend setup required
  • perfect for practice and small local experiments

Local state disadvantages

  • hard to share with a team
  • no built-in locking for safe collaboration
  • vulnerable if your laptop is lost or corrupted
  • easy to expose accidentally if committed to Git

For teams, most projects use remote state stored in a backend such as:

  • Amazon S3
  • Terraform Cloud
  • Google Cloud Storage
  • Azure storage backends in Azure-focused environments

Remote state improves collaboration, durability, and access control.

State File Security Concerns

Terraform state is sensitive. This is one of the most important beginner lessons.

Why? Because state may contain:

  • database passwords
  • access keys
  • rendered secrets
  • resource metadata
  • internal hostnames
  • network details
  • infrastructure IDs

Even if your Terraform code marks a variable or output as sensitive, the actual value may still appear in state if the provider stores it.

Never commit state to Git

Real terraform.tfstate files should not be committed to a repository.

Good .gitignore entries usually include:

.terraform/
*.tfstate
*.tfstate.*
crash.log

If state gets committed accidentally, you may expose secrets and internal infrastructure details to everyone with repository access.

Treat state like a secret-bearing artifact

In production, state should be protected with:

  • strict access control
  • encrypted storage
  • remote backend protections
  • audit logging where possible

The terraform.tfstate.backup File

Terraform often creates a file named terraform.tfstate.backup alongside the main state file.

This backup is a previous copy of the state, typically created before Terraform writes a new version. It exists to give you a fallback if state becomes corrupted or a write operation fails.

That backup is useful, but it has the same security concerns as the main state file.

Important reminders:

  • do not commit the backup file either
  • protect it the same way you protect the main state
  • remember that backups may also contain secrets

The .terraform Directory vs the State File

Beginners often confuse the .terraform directory with the state file, but they are not the same thing.

What the .terraform directory is for

The .terraform directory stores Terraform's local working data, such as:

  • downloaded provider plugins or plugin metadata
  • downloaded modules
  • backend initialization details
  • dependency lock-related working files

What the state file is for

The state file stores Terraform's mapping of configuration to infrastructure.

Simple distinction

  • .terraform/ = Terraform working directory data
  • terraform.tfstate = infrastructure state record

Both are generated artifacts, but they serve very different purposes.

In most projects, .terraform/ should also be ignored in Git.

State Locking Concept

State locking prevents two Terraform operations from modifying the same state at the same time.

Imagine this scenario:

  • Engineer A runs terraform apply
  • Engineer B runs terraform apply 10 seconds later
  • both operations read the same starting state
  • both try to write updates back

Without locking, the state could become inconsistent or corrupted.

A remote backend with locking solves this by allowing only one writer at a time.

How locking works conceptually

  1. Terraform tries to acquire a lock
  2. If the lock is free, it proceeds
  3. If another process holds the lock, Terraform waits or fails
  4. After the operation completes, Terraform releases the lock

Locking is one of the biggest reasons teams move from local state to a remote backend.

How State Supports Teamwork

Once multiple people work on the same infrastructure, state management becomes a team discipline, not just a local file concern.

Teams need state to be:

  • shared
  • protected
  • backed up
  • locked during changes
  • available to CI/CD automation

That is why remote backends are considered standard practice in production Terraform setups.

Common Beginner Misunderstandings

“Terraform reads the cloud directly, so why does it need state?”

Terraform does query providers, but it still needs state to know which real objects belong to which Terraform resource addresses and how to reason about changes safely.

“Sensitive variables mean my state is safe.”

No. Sensitive settings mostly affect display in CLI output. State security is a separate concern.

“The .terraform folder is my state.”

No. The .terraform directory is local working data. The state file is a separate artifact.

“Local state is fine forever.”

Local state is fine for learning and solo demos. Real team environments usually require a remote backend.

Best Practices for Terraform State

  1. Never commit state files to Git.
  2. Use remote state for teams and CI/CD.
  3. Protect state like a secret-bearing file.
  4. Understand that backups may also contain sensitive data.
  5. Use backend locking for collaborative work.
  6. Do not manually edit state unless you fully understand the risks.
  7. Keep your .gitignore updated for .terraform/ and *.tfstate*.

Final Thoughts

Terraform state is not just an implementation detail. It is one of the core reasons Terraform can plan changes accurately, detect drift, and manage infrastructure safely over time. The terraform.tfstate file gives Terraform the memory it needs to connect your code to real cloud resources.

For beginners, the most important lessons are simple:

  • state records what Terraform manages
  • state maps logical resource names to real provider IDs
  • state can contain sensitive information
  • local state is fine for learning, but teams usually need remote state
  • locking prevents concurrent updates from causing problems

Once you understand state, the next step is learning how backends store and protect it in real-world environments.

One final beginner tip: treat state as part of the Terraform system, not an optional side file. If state is missing, outdated, exposed, or changed carelessly, every later Terraform operation becomes harder to trust. Healthy Terraform workflows start with healthy state management.


Knowledge Check

Exercise

Question 1: Purpose of State

Why does Terraform need state?

Exercise

Question 2: Security

Why should terraform.tfstate usually never be committed to Git?

Exercise

Question 3: Locking

What is the purpose of state locking in Terraform?

PreviousPrev
Next

Continue Learning

Terraform Local Values

Learn how Terraform local values work, when to use locals instead of variables or data sources, and how to simplify repetitive expressions in your infrastructure code.

8 min read·Easy

Terraform Output Values

Learn Terraform output values in depth, including output blocks, sensitive outputs, terraform output commands, module outputs, and CI/CD automation patterns.

10 min read·Easy

Terraform State and Backends

Learn the big picture of Terraform state, remote backends, and state management commands so you can work safely with real infrastructure and collaborate with confidence.

5 min read·Medium

Explore Related Topics

AW

AWS Tutorials

Provision AWS infrastructure with Terraform

CI

CI/CD Tutorials

Run terraform plan/apply in CI pipelines

Try the Tool

YAML Validator

Validate Terraform and Kubernetes YAML configs before applying.

On This Page

What Is Terraform State?Why Terraform State ExistsExample: creating a VPCThe `terraform.tfstate` FileWhat the state file storesHow State Maps to Real InfrastructureWhy this matters during changesState and Drift DetectionLocal State vs Remote StateLocal state advantagesLocal state disadvantagesState File Security ConcernsNever commit state to GitTreat state like a secret-bearing artifactThe `terraform.tfstate.backup` FileThe `.terraform` Directory vs the State FileWhat the `.terraform` directory is forWhat the state file is forSimple distinctionState Locking ConceptHow locking works conceptuallyHow State Supports TeamworkCommon Beginner Misunderstandings“Terraform reads the cloud directly, so why does it need state?”“Sensitive variables mean my state is safe.”“The `.terraform` folder is my state.”“Local state is fine forever.”Best Practices for Terraform StateFinal ThoughtsKnowledge Check