π Terraform Series β Day 11

Search for a command to run...

No comments yet. Be the first to comment.
π 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
Secure State Management (S3 + DynamoDB Locking) π Abstract In Terraform, the state file (terraform.tfstate) is the most critical component that connects your configuration with real infrastructure. H
Master GitLab CI/CD Variables, Secrets Management, Artifacts & Best Practices π Introduction In the previous article, we learned how GitLab Runners execute CI/CD pipelines and how to configure Self-H

Learn GitLab Runners, Hosted vs Self-Hosted Runners, Runner Registration, Tags, Pipeline Editor & Parallel Jobs π Introduction In the previous article, we created our first GitLab CI/CD pipeline and

Learn Continuous Integration, Continuous Delivery, Pipelines, Stages, Jobs & .gitlab-ci.yml π Introduction In the previous articles, we explored GitLab fundamentals, repository management, and collab

Learn Docker Scout, Multi-Stage Builds, Docker Hardened Images (DHI), SBOM, Docker Model Runner, Ask Gordon AI, and production-ready container security through practical, real-world examples. Introduc

Import Repositories, Mirroring & Repository Management Best Practices π Introduction In the previous articles, we learned the fundamentals of GitLab, created projects, and configured secure authentic

Terraform State Management & Import
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.
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
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)
EC2 is running in Terraform state
You manually stop it from AWS Console
π β NO
Terraform does not auto-detect manual changes.
π· Step 4: How to Sync State
To update Terraform state:
terraform refresh
OR
terraform apply
Terraform reads real infrastructure
Updates state file
π Example:running β stopped
π· Step 5: State Management Commands
terraform state list
terraform state show aws_instance.my_instance
terraform state rm aws_key_pair.my_key
π· Step 6: Important Interview Question
terraform state rm aws_key_pair.my_key
β 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>
Resource is added back to Terraform state
Terraform starts managing it again
π· Step 8: Importing Existing EC2 Instance
resource "aws_instance" "my_instance" {
ami = "ami-xxxx" # placeholder
instance_type = "t2.micro"
}
π Values can be temporary/dummy
terraform import aws_instance.my_instance <instance-id>
π Example:
terraform import aws_instance.demo-instance i-1234567890abcdef0
terraform state show aws_instance.my_instance
After import:
terraform plan
π Then:
Update .tf file with correct values
Match actual AWS configuration
Import updates only state, not Terraform code
You must manually update .tf files
If configuration does not match β Terraform will try to modify resource
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
βA complete Terraform series covering everything from fundamentals to advanced real-world infrastructure automation in a DevOps environment.β
π§ Email: gujjarapurv181@gmail.com
π GitHub: github.com/ApurvGujjar07
πΌ LinkedIn: linkedin.com/in/apurv-gujjar