Terraform
Basics & Fundamentals
- Infrastructure as Code (IaC)
- Declarative Syntax in IaC
- Terraform Configuration Files
- Terraform CLI
- Terraform Init
- Terraform Plan
- Terraform Apply
- Terraform Destroy
Providers & Resources
Variables & Outputs
- Input Variables
- Variable Types
- Default Values
- Environment Variables
- Output Values
- Variable Validation
State Management
- Terraform State File
- Terraform Remote State
- Terraform State Locking
- Terraform Drift Detection
- Terraform Refresh
- Terraform Import
Modules (Reusability)
- Terraform Modules
- Terraform Public Modules
- Terraform local modules
- Terraform Module Versioning
- Terraform Nested Modules
Provisioners & Lifecycle
Terraform Import
In many real-world environments, infrastructure is started manually (“ClickOps”), or deployed by scripts outside of an Infrastructure as Code (IaC) workflow. Eventually, you decide to adopt Terraform for management—but you already have existing resources. Rather than destroying everything and starting fresh, Terraform offers a mechanism to import those resources into its state so that they become managed by Terraform moving forward. This process is called Terraform Import.
1. What Is Terraform Import?
Definition: Terraform Import is the ability to map an existing cloud resource (e.g., an AWS EC2 instance, a GCP bucket, an Azure virtual network) into a Terraform state file under a specified resource address so that Terraform can manage its lifecycle going forward.
Important notes:
- It does not automatically generate the full configuration code—you must author the
resource
block (or use Terraform’s newer import-block feature). ([Spacelift][1]) - Running
terraform import
updates the state only; it doesn’t make changes to the actual resource. After import, you must runterraform plan
and reconcile differences in configuration. ([Terrateam][2])
In essence: you tell Terraform, “Here is a resource I already have; please recognize it and manage it as if you created it.”
2. Why Is Terraform Import Important?
2.1 Real-World Adoption
Many organisations already have infrastructure built without Terraform. To adopt Terraform incrementally (without major disruption), Terraform Import lets you migrate resources into Terraform management. ([zeet.co][3])
2.2 Avoiding Duplication & Drift
When unmanaged resources exist alongside Terraform-managed ones, risk of duplication or drift increases. Importing ensures everything is tracked in the same state.
2.3 Disaster Recovery & State-repair
If a state file is lost or corrupted, import helps rebuild the state by mapping existing cloud resources to a scratch Terraform state.
2.4 Certification & Interview Relevance
Terraform Import is a common topic in the HashiCorp Certified: Terraform Associate exam and other DevOps certifications. Knowing when and how to use it demonstrates professional maturity.
2.5 Collaboration & Governance
Once resources are imported, they can be version-controlled, peer-reviewed, enforced via policies, and managed via CI/CD—improving governance and consistency.
3. How Terraform Import Works (Step-by-Step)
-
Write a minimal
resource
block in your.tf
file that matches the existing resource type and address. -
Run
terraform init
to initialise the configuration. -
Execute the import command:
terraform import <RESOURCE_ADDRESS> <RESOURCE_ID>e.g.
terraform import aws_instance.web i-0abcd1234ef56789
([Spacelift][1]) -
Terraform queries the provider API, reads the resource attributes, and writes them into the state file under that address.
-
Run
terraform plan
and inspect what changes Terraform wants to make (often you will need to update the configuration to match the existing resource). -
Once configuration matches the real resource and plan shows no changes, you now fully manage the resource via Terraform.
Important caveat: import modifies state but not the actual resource. If your configuration is incomplete or mismatched, a subsequent terraform apply
could modify or destroy the resource unexpectedly.
4. Example Programs
Below are three unique example programs (AWS, GCP, Azure) showing how to import an existing resource.
4.1 Example 1: AWS EC2 Instance Import
Step A: Configuration (main.tf)
provider "aws" { region = "us-east-1"}
resource "aws_instance" "existing_server" { # Intentionally minimal; we will import the actual ID}
Step B: Initialise
terraform init
Step C: Execute Import
Assume you have an existing instance i-0a1b2c3d4e5f6g7h8
.
terraform import aws_instance.existing_server i-0a1b2c3d4e5f6g7h8
Step D: Reconcile Configuration
Run terraform plan
. You may see differences (for example instance type, tags, subnet). Update your resource
block accordingly:
resource "aws_instance" "existing_server" { ami = "ami-0c94855ba95c71c99" instance_type = "t3.micro" tags = { Name = "ProdAppServer" Environment = "Production" }}
Then run terraform plan
again until no changes are indicated.
4.2 Example 2: GCP Storage Bucket Import
Step A: Configuration (main.tf)
provider "google" { project = "my-gcp-project" region = "us-central1"}
resource "google_storage_bucket" "data_archive" { # Placeholder}
Step B: Initialise
terraform init
Step C: Import
Assume the existing bucket name is archive-bucket-2024
. For GCP, ID might be the bucket name.
terraform import google_storage_bucket.data_archive archive-bucket-2024
Step D: Update Configuration
Run terraform plan
, inspect differences, and then update:
resource "google_storage_bucket" "data_archive" { name = "archive-bucket-2024" location = "US" versioning { enabled = true }}
Ensure plan
shows no changes afterwards.
4.3 Example 3: Azure Virtual Network Import
Step A: Configuration (main.tf)
provider "azurerm" { features {}}
resource "azurerm_virtual_network" "corp_vnet" { # Placeholder}
Step B: Initialise
terraform init
Step C: Import
Assume the existing vnet has resource ID:
/subscriptions/11111111-2222-3333-4444/resourceGroups/rg-network/providers/Microsoft.Network/virtualNetworks/vnet-corp
terraform import azurerm_virtual_network.corp_vnet /subscriptions/11111111-2222-3333-4444/resourceGroups/rg-network/providers/Microsoft.Network/virtualNetworks/vnet-corp
Step D: Update Configuration
After terraform plan
, adjust config:
resource "azurerm_virtual_network" "corp_vnet" { name = "vnet-corp" location = "East US" resource_group_name = "rg-network" address_space = ["10.0.0.0/16"]}
Then ensure no pending changes.
5. How to Remember Terraform Import (Interview/Exam)
Use the mnemonic: “I.M.P.O.R.T.”
Letter | Meaning |
---|---|
I | Identify existing resource |
M | Map to Terraform address |
P | Place a minimal configuration block |
O | Obtain resource ID from provider |
R | Run terraform import |
T | Test with terraform plan , reconcile, then manage |
When you hear “Terraform Import”, think: “Identify, Map, Place, Obtain, Run, Test” — I.M.P.O.R.T.
Potential interview question:
“What does
terraform import
do and what are the steps?” You can respond: “It brings an existing resource into Terraform’s state. Steps: prepare configuration block, identify the resource ID, runterraform import <address> <id>
, then adjust configuration and run plan until no changes remain.”
6. Common Pitfalls & Why It’s Important
6.1 Pitfalls
- Missing configuration: If after import you don’t update configuration to match the resource, a subsequent
terraform apply
may try to destroy or modify it. - Wrong resource ID: Each provider/resource type has a specific ID format. ([HashiCorp Developer][4])
- Multiple imports of same resource: Importing the same resource to two different addresses can cause issues.
- State schema changes: Providers evolve — some resource types split or rename which affects import.
- Running import in production without review: Lack of peer review can lead to mis-imports and errors.
6.2 Importance
Understanding Terraform Import is critical because:
- It enables incremental IaC adoption.
- It avoids destructive re-creation of resources.
- It aligns your infrastructure with version-control and audit trails.
- It supports team collaboration, automated workflows, and compliance.
- It enhances your skills portfolio for senior data/DevOps roles.
7. Best Practices for Terraform Import
- Always version-control your
.tf
files and state files. - Use remote state backends with locking (e.g., S3 + DynamoDB) before heavy import operations.
- Document every import: resource type, ID, person who did it, date, issues encountered.
- After import, run
terraform plan
repeatedly until it reports “No changes”. - Use the import block feature (Terraform v1.5+) for automation. ([terramate.io][5])
- Limit manual imports in production; use CI/CD workflows and peer review.
- Understand dependencies: import base resources (networks, IAM) before dependent ones (instances, databases).
- Validate the final configuration corresponds exactly to the resource attributes.
8. Summary of Key Points
- Terraform Import lets you bring existing resources into Terraform management (state) without recreating them.
- It only updates state — you must still manage configuration and run
plan
+apply
. - Essential for teams migrating to Terraform, avoiding duplication, and managing legacy infrastructure.
- Failure to import correctly can cause destructive changes.
- Use the mnemonic I.M.P.O.R.T. to remember the process.
- Combine with best practices (documenting, versioning, state locking) for safe adoption.
Mastering Terraform Import is a gateway to making your infrastructure fully managed as code—even if it didn’t start that way. For senior data engineers, DevOps professionals, and cloud architects, knowing how to import resources safely demonstrates maturity and practical readiness.
By running through the example programs above (AWS, GCP, Azure), you gain hands-on experience. Pair that with the memory technique and best practices and you’ll be well-prepared for real projects and certification exams.
“Don’t rebuild what you already have—import it, claim it under Terraform, then manage it with confidence.”