- Create an AWS resource manually (outside Terraform)
- Import it into Terraform state
- Manage it fully using Terraform
π Project Structure (IMPORTANT)
terraform-import-lab/
β
βββ provider.tf
βββ variables.tf
βββ terraform.tfvars
βββ main.tf
βββ outputs.tf
β
βββ README.md
π§ Concept
πΉ What is Terraform Import?
Terraform import is used when:
- Resource already exists in AWS
- But Terraform DOES NOT manage it yet
π We "attach" Terraform to existing infrastructure
π STEP 1 β Create Resource Manually (AWS Console)
Students MUST create manually:
Create S3 Bucket:
- Go to AWS Console
- Create S3 bucket
- Give unique name (example):
my-import-lab-<yourname>-123
β οΈ Save the bucket name β you will use it later
π provider.tf
provider "aws" {
region = var.aws_region
}
π variables.tf
variable "aws_region" {
description = "AWS region"
type = string
}
variable "bucket_name" {
description = "Existing S3 bucket name"
type = string
}
π terraform.tfvars
π fill this manually
aws_region = "us-east-2"
bucket_name = "REPLACE_WITH_YOUR_BUCKET_NAME"
π main.tf
β οΈ IMPORTANT: This must match existing resource
resource "aws_s3_bucket" "existing" {
bucket = var.bucket_name
tags = {
ManagedBy = "Terraform"
Project = "ImportLab"
}
}
π outputs.tf
output "bucket_name" {
value = aws_s3_bucket.existing.bucket
}
π README.md (for students)
Terraform Import Lab
Steps:
1. terraform init
2. terraform plan
(Expected: Terraform wants to CREATE bucket β BUT DON'T APPLY)
3. Import existing resource:
terraform import aws_s3_bucket.existing <bucket_name>
4. Run:
terraform plan
(Expected: NO changes)
5. Modify tags in main.tf
6. Run:
terraform apply
βοΈ STEP 2 β Initialize Terraform
terraform init
βοΈ STEP 3 β Plan (IMPORTANT MOMENT)
terraform plan
π You will see:
+ create aws_s3_bucket
β οΈ THIS IS WRONG (resource already exists)
π₯ STEP 4 β Import Resource
terraform import aws_s3_bucket.existing <your-bucket-name>
Example:
terraform import aws_s3_bucket.existing my-import-lab-aisal-123
βοΈ STEP 5 β Plan Again
terraform plan
β
Expected:
No changes. Infrastructure is up-to-date.
π― STEP 6 β Modify Resource (Real DevOps Scenario)
Update main.tf:
tags = {
ManagedBy = "Terraform"
Project = "ImportLab"
Owner = "Student"
}
βοΈ STEP 7 β Apply Changes
terraform apply
β Now Terraform manages the resource
1. Terraform Workflow
-
initβ setup -
planβ preview -
applyβ execute
2. Import Concept
Before import:
- Terraform wants to CREATE resource β
After import:
- Terraform recognizes existing resource β
3. State File Role
π Import updates terraform.tfstate
4. Real DevOps Scenario
Used when:
- Company already has infrastructure
- You start using Terraform later
π¨ Common Mistakes (Teach Them)
β Wrong resource name
aws_s3_bucket.wrong_name
β Wrong bucket name
β Skipping import and running apply
π This will FAIL
π Import IAM user
resource "aws_iam_user" "example" {
name = var.user_name
}
Command:
terraform import aws_iam_user.example <username>
π§© Interview Questions (from this lab)
- What is terraform import?
- Does import create resource?
- What happens if configuration doesnβt match?
- Can Terraform import automatically generate code?
- Where is imported data stored?
π‘ Real Production Insight
In real companies:
- 80% infra exists BEFORE Terraform
- Import is used during Terraform adoption phase
Top comments (1)
Great hands-on lab. This teaches one of the most important real-world Terraform skills: adopting existing infrastructure safely instead of rebuilding it blindly. The step where plan shows a create before import is especially valuable because that is exactly where many teams make mistakes in production. Clean, practical, and very useful for anyone learning true Terraform workflow beyond just init/plan/apply.