DEV Community

Aisalkyn Aidarova
Aisalkyn Aidarova

Posted on

LAB: Terraform Workflow + Import (Production Style)

  1. Create an AWS resource manually (outside Terraform)
  2. Import it into Terraform state
  3. Manage it fully using Terraform

πŸ“ Project Structure (IMPORTANT)

terraform-import-lab/
β”‚
β”œβ”€β”€ provider.tf
β”œβ”€β”€ variables.tf
β”œβ”€β”€ terraform.tfvars
β”œβ”€β”€ main.tf
β”œβ”€β”€ outputs.tf
β”‚
└── README.md
Enter fullscreen mode Exit fullscreen mode

🧠 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
Enter fullscreen mode Exit fullscreen mode

⚠️ Save the bucket name β€” you will use it later


πŸ“„ provider.tf

provider "aws" {
  region = var.aws_region
}
Enter fullscreen mode Exit fullscreen mode

πŸ“„ variables.tf

variable "aws_region" {
  description = "AWS region"
  type        = string
}

variable "bucket_name" {
  description = "Existing S3 bucket name"
  type        = string
}
Enter fullscreen mode Exit fullscreen mode

πŸ“„ terraform.tfvars

πŸ‘‰ fill this manually

aws_region  = "us-east-2"
bucket_name = "REPLACE_WITH_YOUR_BUCKET_NAME"
Enter fullscreen mode Exit fullscreen mode

πŸ“„ main.tf

⚠️ IMPORTANT: This must match existing resource

resource "aws_s3_bucket" "existing" {
  bucket = var.bucket_name

  tags = {
    ManagedBy = "Terraform"
    Project   = "ImportLab"
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“„ outputs.tf

output "bucket_name" {
  value = aws_s3_bucket.existing.bucket
}
Enter fullscreen mode Exit fullscreen mode

πŸ“„ 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
Enter fullscreen mode Exit fullscreen mode

βš™οΈ STEP 2 β€” Initialize Terraform

terraform init
Enter fullscreen mode Exit fullscreen mode

βš™οΈ STEP 3 β€” Plan (IMPORTANT MOMENT)

terraform plan
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ You will see:

+ create aws_s3_bucket
Enter fullscreen mode Exit fullscreen mode

⚠️ THIS IS WRONG (resource already exists)


πŸ”₯ STEP 4 β€” Import Resource

terraform import aws_s3_bucket.existing <your-bucket-name>
Enter fullscreen mode Exit fullscreen mode

Example:

terraform import aws_s3_bucket.existing my-import-lab-aisal-123
Enter fullscreen mode Exit fullscreen mode

βš™οΈ STEP 5 β€” Plan Again

terraform plan
Enter fullscreen mode Exit fullscreen mode

βœ… Expected:

No changes. Infrastructure is up-to-date.
Enter fullscreen mode Exit fullscreen mode

🎯 STEP 6 β€” Modify Resource (Real DevOps Scenario)

Update main.tf:

tags = {
  ManagedBy = "Terraform"
  Project   = "ImportLab"
  Owner     = "Student"
}
Enter fullscreen mode Exit fullscreen mode

βš™οΈ STEP 7 β€” Apply Changes

terraform apply
Enter fullscreen mode Exit fullscreen mode

βœ… 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
Enter fullscreen mode Exit fullscreen mode

❌ Wrong bucket name

❌ Skipping import and running apply

πŸ‘‰ This will FAIL


πŸ‘‰ Import IAM user

resource "aws_iam_user" "example" {
  name = var.user_name
}
Enter fullscreen mode Exit fullscreen mode

Command:

terraform import aws_iam_user.example <username>
Enter fullscreen mode Exit fullscreen mode

🧩 Interview Questions (from this lab)

  1. What is terraform import?
  2. Does import create resource?
  3. What happens if configuration doesn’t match?
  4. Can Terraform import automatically generate code?
  5. 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)

Collapse
 
vandana_platform profile image
vandana.platform

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.