π Terraform Series β Day 11

Gujjar Apurv is a passionate DevOps Engineer in the making, dedicated to automating infrastructure, streamlining software delivery, and building scalable cloud-native systems. With hands-on experience in tools like AWS, Docker, Kubernetes, Jenkins, Git, and Linux, he thrives at the intersection of development and operations. Driven by curiosity and continuous learning, Apurv shares insights, tutorials, and real-world solutions from his journeyβmaking complex tech simple and accessible. Whether it's writing YAML, scripting in Python, or deploying on the cloud, he believes in doing it the right way. "Infrastructure is code, but reliability is art."
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 importHandle 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
.tffile with correct valuesMatch actual AWS configuration
β οΈ Important Notes
Import updates only state, not Terraform code
You must manually update
.tffilesIf 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/applyto syncterraform statecommands help manage resourcesterraform importis 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
π§ Email: gujjarapurv181@gmail.com
π GitHub: github.com/ApurvGujjar07
πΌ LinkedIn: linkedin.com/in/apurv-gujjar





