Terraform AWS – Day 04: Understanding tfstate File & Remote Storage in S3


Yesterday, I built my first VPC with subnets, route tables, and an internet gateway. But while doing that, I realized something important: how does Terraform even “remember” what it has created?
That’s where the terraform.tfstate
file comes in.
🔹 What is terraform.tfstate
?
The terraform.tfstate
file is where Terraform keeps the current state of your infrastructure.
Think of it like Terraform’s memory — it knows:
Which resources exist (EC2, VPCs, subnets, etc.)
Their configurations (AMI IDs, IPs, tags, etc.)
Any dependencies between them
👉 Without this file, Terraform wouldn’t know what’s deployed and would keep trying to recreate resources every time.
🔹 Why is it Important?
Tracks Infrastructure → Terraform knows exactly what exists.
Sync Between Teams → Multiple people can work without overwriting each other’s changes.
Avoids Drift → If someone changes infra outside Terraform (like manually in AWS), Terraform will detect it by comparing against state.
🔹 Where Can You Store tfstate
?
By default, Terraform stores terraform.tfstate
locally in your project folder. But in real-world scenarios, that’s risky because:
❌ If your laptop crashes, you lose state.
❌ Teams can’t collaborate properly.
❌ Risk of overwriting infra.
✅ Best practice → Store state remotely in the cloud. Terraform supports backends like:
S3 (AWS)
GCS (Google Cloud)
Azure Blob Storage
Terraform Cloud
🔹 Practical Demo: Store State in S3
Here’s how I configured Terraform to store the state file in AWS S3:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "6.8.0"
}
}
backend "s3" {
bucket = "mybucket-8964366a63341c51"
key = "backend-tfstate"
region = "ap-south-1"
}
}
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "myserver" {
ami = "ami-0144277607031eca2"
instance_type = "t2.micro"
tags = {
my = "server"
}
}
🔍 What this does:
Creates an EC2 instance (just for demo)
Saves the state file in S3 instead of locally
🔹 Key Takeaways from Day 04
terraform.tfstate
is Terraform’s source of truthAlways store it remotely for safety & collaboration
S3 is the most common backend for AWS users
🔗 Follow My Journey
💻 Code: GitHub
🐦 Updates: X (Twitter)
Subscribe to my newsletter
Read articles from Abdul Raheem directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Abdul Raheem
Abdul Raheem
Cloud DevOps | AWS | Terraform | CI/CD | Obsessed with clean infrastructure. Cloud DevOps Engineer 🚀 | Automating Infrastructure & Securing Pipelines | Bridging Gaps Between Code and Cloud ☁️ I’m on a mission to master DevOps from the ground up—building scalable systems, automating workflows, and integrating security into every phase of the SDLC. Currently working with AWS, Terraform, Docker, CI/CD, and learning the art of cloud-native development.