Interview Prep/Terraform/Import, Already Exists and State Surgery

You need to import 60 SQS queues created by hand. Writing 60 import blocks is error-prone.

IntermediateTroubleshootingImport, Already Exists and State Surgery
Answer

Likely causes → Not an error, but a common place to make address mistakes.

Diagnose → List the real queues: aws sqs list-queues --queue-name-prefix orders- --query QueueUrls

Fix → Use for_each on import blocks (Terraform 1.7+), driven by the same map as the resource:

locals {
  queues = toset(["orders-created", "orders-paid", "orders-shipped"])
}
resource "aws_sqs_queue" "q" {
  for_each = local.queues
  name     = each.key
}
import {
  for_each = local.queues
  to       = aws_sqs_queue.q[each.key]
  id       = "https://sqs.ap-south-1.amazonaws.com/111122223333/${each.key}"
}

Prevent → Delete import blocks after a clean apply, or leave them; they're no-ops once imported.

All Terraform questions Roadmap: Terraform in production

More on Import, Already Exists and State Surgery