If you’re working with Terraform, you’ve probably seen this file pop up: terraform.tfstate
.
What is it? Why does Terraform need it? Should you be scared of it? 😅
I’m going to explain everything about the Terraform state file — in simple, everyday language — and show you how to manage it safely. We’ll walk through examples, look at common commands, and even talk about remote state.
In the previous posts, we have covered the topic - "Terraform Conditionals, Loops & Built-in Functions Explained"
Let’s get into it!"
What is Terraform State File?
"So first off — what even is this state file?
When you run terraform apply
, Terraform actually builds your infrastructure and then writes down what it created.
It keeps track of things like:
- what resources it made
- their IDs
- any settings it used
All of that gets saved in a file called terraform.tfstate
.
Think of it like Terraform’s memory. Without it, it forgets what it’s done."
Why Terraform state file is important?
"Now you might be wondering — why does Terraform need to remember anything?
Let’s say you created an EC2 instance on AWS.
Next week, you change the instance type in your code.
Without the state file, Terraform has no idea that the instance even exists — so it might try to create a new one, or worse, delete something by mistake.
The state helps Terraform figure out what to change, and what to leave alone."
"By default, Terraform saves the state file right in your project folder — it's just a normal file called terraform.tfstate
.
Let me show you real quick:"
terraform init
terraform apply
"Once that runs, boom — there’s your terraform.tfstate file.
You can open it, but… fair warning — it’s just a big chunk of JSON. Not fun to read. 😅"
How to Inspect Terraform State?
"Thankfully, you don’t have to read that JSON yourself. Terraform gives us easy commands to peek inside the state. Like these:"
terraform show
terraform state list
terraform state show <resource>
"terraform show gives you a summary of everything.
terraform state list will just list all the resources Terraform is tracking.
And terraform state show lets you look at one resource in detail."
Understanding Terraform State Commands 'mv' and 'rm':
"Okay, now let’s talk about two state commands that people get confused about — and they’re super useful once you get the hang of them.
That’s terraform state mv and terraform state rm."
🔄 terraform state mv – Rename a Resource
"This command is like telling Terraform,
‘Hey, I renamed this thing in my code, but don’t worry — it’s still the same resource.’
Here's an example:"
# Old name
# You change it to:
resource "aws_instance" "web2" {ami = "ami-0e86e20dae9224db8"instance_type = "t2.micro"subnet_id = "subnet-0105b1aef1e7755cd"key_name = "demov2"ebs_block_device {device_name = "/dev/sda1"volume_size = 10}associate_public_ip_address = "true"vpc_security_group_ids = ["sg-04dd813e22c5a0b2f"]tags = {Name = "Web2"}}
"Now, if you don’t do anything, Terraform thinks the old one is gone and the new one is something totally different — so it tries to destroy and recreate it.
We don’t want that.
Instead, run this:"
terraform state mv aws_instance.web1 aws_instance.web2
"Now Terraform knows:
'Ah, okay — you just renamed it. Got it.'
No resources get recreated. Everything stays the same in the cloud. You just updated the state."
🗑️ terraform state rm – Remove from State
"Alright, now let’s talk about terraform state rm. This one removes a resource from the state file.
That means Terraform forgets that the resource exists — but it doesn’t delete it from your cloud provider.
For example:"
terraform state rm aws_instance.web2
"After this, Terraform no longer tracks that ec2 web2 instance.
It still exists in AWS — you can see it in the console — but Terraform won’t manage it anymore.
Why would you do this?
- Maybe you’re importing that resource into a different project
- Or maybe you want to recreate it cleanly
- Or maybe you just don’t want Terraform managing it anymore
Just keep in mind — if the resource is still in your .tf code, and you run terraform apply again, Terraform might try to create it again. So use this carefully."
How to create Terraform Remote Backend?
"Now, what if you don’t want the state file sitting in your folder? Maybe you’re on a different machine, or want to back it up.
You can use something called a remote backend — and the easiest one is S3."
terraform {
backend "s3" {
bucket = "skumarsumar0710"
key = "terraform_demo/terraform.tfstate"
region = "us-east-1"
}
}
"This will store the state file in an S3 bucket. When you run terraform init, it’ll ask you if you want to migrate your local state to remote — say yes.
0 Comments