# 📘 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:

```shell
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:

```shell
terraform refresh
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/ba441df5-cb1c-477b-a0d9-b7cbf0d3ec1f.png align="center")

OR

```shell
terraform apply
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/db04399c-de37-4aff-9cb6-b27b3cdee1c8.png align="center")

### ✔ Result

*   Terraform reads real infrastructure
    
*   Updates state file
    

👉 Example:  
`running → stopped`

> ### 🔷 Step 5: State Management Commands

### 1️⃣ List All Resources

```shell
terraform state list
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/b801175e-fbac-40a5-baae-636b1ccf0361.png align="center")

### 2️⃣ Show Resource Details

```shell
terraform state show aws_instance.my_instance
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/34de2843-92b5-4e45-b83e-0831968f56a3.png align="center")

### 3️⃣ Remove Resource from State

```shell
terraform state rm aws_key_pair.my_key
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/78c5667f-7b1b-47c0-96f5-e20bd9e7ab3b.png align="center")

> ### 🔷 Step 6: Important Interview Question

### ❓ What happens if you run:

```shell
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:

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

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/ce701b9f-e7ba-48db-909d-d299c237cd2c.png align="center")

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/d7b06138-3d2e-4c0f-b78c-26af4c75e4b3.png align="center")

### ✔ Result

*   Resource is added back to Terraform state
    
*   Terraform starts managing it again
    

> ### 🔷 Step 8: Importing Existing EC2 Instance

### 🧩 Step 1: Create Resource Block

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

👉 Values can be temporary/dummy

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/4a01b747-8661-421b-be46-d752e9fdb3e2.png align="center")

### 🧩 Step 2: Run Import Command

```shell
terraform import aws_instance.my_instance <instance-id>
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/821a4e6c-bfa5-47a2-a4c3-7843f9358a70.png align="center")

👉 Example:

```shell
terraform import aws_instance.demo-instance i-1234567890abcdef0
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/2fd4e194-a299-4d99-ae95-15d5f2531051.png align="center")

### 🧩 Step 3: Verify

```shell
terraform state show aws_instance.my_instance
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/a6a5d1a1-d2c0-44be-b905-4194d1ddabe9.png align="center")

### 🧩 Step 4: Fix Configuration (Important)

After import:

```shell
terraform plan
```

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/e888053d-a432-4e1a-8523-86dd497dcacc.png align="center")

👉 Then:

*   Update `.tf` file with <mark class="bg-yellow-200 dark:bg-yellow-500/30"> correct values</mark>
    
*   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
    

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/11b3ebfa-509e-4b2b-919d-ee65e1e0b554.png align="center")

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/ba99f752-a725-425c-87bd-0e32e4cd309d.png align="center")

### 🚀 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**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751797710818/123a7231-3dca-4273-ad68-7bd026f69b95.png?auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp align="center")

“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**](mailto:gujjarapurv181@gmail.com)
    
*   🐙 **GitHub**: [**github.com/ApurvGujjar07**](http://github.com/ApurvGujjar07)
    
*   💼 **LinkedIn**: [**linkedin.com/in/apurv-gujjar**](http://linkedin.com/in/apurv-gujjar)
