Skip to main content

Command Palette

Search for a command to run...

πŸ“˜ Terraform Series – Day 11

Published
β€’4 min read
πŸ“˜ Terraform Series – Day 11

Terraform State Management & Import

πŸ“ Abstract

Terraform works by maintaining a record of infrastructure in a state file. This state file acts as the bridge between your Terraform configuration and real-world resources (like AWS EC2, Security Groups, etc.).

In this blog, we will understand how Terraform manages state, why it is important, how to handle state manually, and how to import existing resources into Terraform.

🎯 Objectives

After completing this blog, you will be able to:

  • Understand what Terraform state is

  • Know why state management is important

  • Sync Terraform state with real infrastructure

  • Use important state commands

  • Understand terraform import

  • Handle real-world interview scenarios

πŸ”· Step 1: What is Terraform State?

Terraform maintains a file called:

terraform.tfstate

🧠 This file stores:

  • All created resources (EC2, Security Groups, etc.)

  • Current status of resources (running, stopped, IDs, etc.)

  • Mapping between Terraform config ↔ real infrastructure

πŸ”· Step 2: Why State Management is Required

Terraform uses the state file to:

  • Know what resources are already created βœ…

  • Track infrastructure changes βœ…

  • Avoid recreating resources unnecessarily βœ…

  • Compare desired state vs actual state βœ…

πŸ”· Step 3: Important Scenario (Very Important)

πŸ”Ή Situation:

  • EC2 is running in Terraform state

  • You manually stop it from AWS Console

❓ Will Terraform automatically update state?

πŸ‘‰ ❌ NO

Terraform does not auto-detect manual changes.

πŸ”· Step 4: How to Sync State

To update Terraform state:

terraform refresh

OR

terraform apply

βœ” Result

  • Terraform reads real infrastructure

  • Updates state file

πŸ‘‰ Example:
running β†’ stopped

πŸ”· Step 5: State Management Commands

1️⃣ List All Resources

terraform state list

2️⃣ Show Resource Details

terraform state show aws_instance.my_instance

3️⃣ Remove Resource from State

terraform state rm aws_key_pair.my_key

πŸ”· Step 6: Important Interview Question

❓ What happens if you run:

terraform state rm aws_key_pair.my_key

πŸ”₯ Answer

  • ❌ Resource is NOT deleted from AWS

  • βœ… Only removed from Terraform state

πŸ‘‰ The actual resource still exists in AWS

πŸ”· Step 7: How to Restore Resource (Import Back)

If you removed resource from state by mistake:

terraform import aws_key_pair.my_key <key-Name-in aws>

βœ” Result

  • Resource is added back to Terraform state

  • Terraform starts managing it again

πŸ”· Step 8: Importing Existing EC2 Instance

🧩 Step 1: Create Resource Block

resource "aws_instance" "my_instance" {
  ami           = "ami-xxxx"   # placeholder
  instance_type = "t2.micro"
}

πŸ‘‰ Values can be temporary/dummy

🧩 Step 2: Run Import Command

terraform import aws_instance.my_instance <instance-id>

πŸ‘‰ Example:

terraform import aws_instance.demo-instance i-1234567890abcdef0

🧩 Step 3: Verify

terraform state show aws_instance.my_instance

🧩 Step 4: Fix Configuration (Important)

After import:

terraform plan

πŸ‘‰ Then:

  • Update .tf file with correct values

  • Match actual AWS configuration

⚠️ Important Notes

  • Import updates only state, not Terraform code

  • You must manually update .tf files

  • If configuration does not match β†’ Terraform will try to modify resource

πŸš€ Conclusion

  • Terraform state is the core of infrastructure tracking

  • Manual changes are not auto-detected

  • Use terraform refresh/apply to sync

  • terraform state commands help manage resources

  • terraform import is useful for existing infrastructure

πŸ‘¨β€πŸ’» About the Author

β€œA complete Terraform series covering everything from fundamentals to advanced real-world infrastructure automation in a DevOps environment.”

πŸ“¬ Let's Stay Connected

Terraform

Part 1 of 11

πŸš€ Terraform Series – Automate Your Infrastructure Starting a complete **Terraform series** where I’ll cover everything from **basic to advanced level** with real-world practicals. In this series, you will learn: β€’ What is Infrastructure as Code (IaC) & why it matters β€’ Terraform fundamentals (providers, resources, state) β€’ Writing and managing Terraform configurations β€’ Variables, outputs & modules β€’ Remote state & state management β€’ Provisioning infrastructure on AWS β€’ Automation & real-world use cases β€’ Advanced concepts like workspaces, modules, and best practices 🎯 Goal: Help you automate infrastructure and become job-ready in DevOps. Perfect for **beginners, students, and DevOps learners** who want hands-on experience. Stay tuned and let’s build infrastructure the smart way βš‘πŸ’» #Terraform #DevOps #Cloud #AWS #InfrastructureAsCode #Automation

Up next

πŸ“˜ Terraform Series – Day 10

Conditional Expressions (Ternary Operator) πŸ“ Abstract In real-world DevOps projects, infrastructure often needs to behave differently based on environments such as development, staging, and productio