Q1
Why must production teams NEVER store terraform.tfstate inside Git or local laptops—and how do Remote Backends + State Locking prevent infrastructure corruption?
tfstate contains plaintext secrets (DB passwords, keys), and local copies drift across teammates. Remote Backends (S3 + DynamoDB / native .tflock, Azure Blob lease, GCS) encrypt state centrally and enforce locking so two terraform apply runs never corrupt resources concurrently.
Q2
Why is for_each = toset(...) vastly safer than count = length(list) when removing an item from the middle of a list?
count indexes resources numerically (aws_iam_user.u[0], [1], [2]). Deleting index [1] shifts [2] down into [1], forcing Terraform to destroy and recreate unrelated downstream production resources! for_each keys resources by stable string map keys.
Q3
What does terraform apply actually do if someone manually tweaked an AWS Security Group rule inside the Console (Configuration Drift)?
During plan, Terraform refreshes live cloud state against tfstate, detects the manual console rule isn't declared in .tf, and reverts/removes the unauthorized change on the next apply.
Q4
How does lifecycle { create_before_destroy = true } + prevent_destroy = true protect critical databases and zero-downtime Load Balancer certs?
create_before_destroy spins up replacement certs/Launch Templates *before* deleting old ones (preventing brief outages), while prevent_destroy = true makes Terraform abort any plan that would accidentally wipe a production RDS database.
Q5
Why can marking a Terraform variable sensitive = true STILL leak secrets inside terraform.tfstate?
sensitive = true ONLY masks terminal CLI stdout/stderr display! Terraform STILL writes literal cleartext values into the state file—requiring encrypted remote backends, KMS encryption, and tight S3 IAM access.
Q6
What is the key difference between terraform taint (now -replace) vs deleting a resource block from .tf?
terraform apply -replace='aws_instance.app' forces replacement of a damaged resource while keeping it declared in HCL, whereas deleting the block removes it permanently.
Q7
Why do depends_on abuses inside Terraform modules slow plans down and hide data-flow dependencies?
Prefer implicit dependencies (vpc_id = aws_vpc.main.id) so Terraform parallelizes independent branches cleanly. Overusing depends_on on whole modules forces serial execution and causes unexpected known-after-apply cascades.
Q8
When should you split monolithic main.tf into isolated Directory/State folders (dev/, staging/, prod/) instead of sharing terraform workspace?
Workspaces share the exact same backend bucket and module code—one bad merge can accidentally run apply against prod if a developer stays on the wrong workspace! Separate directories with distinct AWS Accounts/backends isolate blast radius completely.
Q9
How do moved {} blocks (Terraform 1.1+) allow safely refactoring a resource into a reusable Module WITHOUT destroying and recreating live infra?
Adding moved { from = aws_s3_bucket.logs to = module.storage.aws_s3_bucket.logs } updates state paths smoothly in-place instead of treating the refactor as destroy + create.
Q10
Why does putting .terraform/ and *.tfstate in .gitignore yet committing .terraform.lock.hcl stabilize team CI builds?
.terraform/ holds giant OS-specific binary plugins (hundreds of MB), whereas .terraform.lock.hcl pins exact verified provider hashes so every CI runner and developer installs identical deterministic provider versions.