Opscraft
Terraform · Lesson 12 of 12 · 5 min read

Interview questions and model answers

"What is Terraform state and why is it important?"

It's Terraform's record of which real resources correspond to which configuration blocks, with their IDs and attributes. Terraform uses it to compute diffs, dependency order and deletions. It contains sensitive values, so it must be stored remotely, encrypted, versioned and access-controlled — for example in S3 with KMS, versioning and locking — and never committed to Git.

"How do you manage state for a team on AWS?"

An S3 backend per environment or stack with versioning, SSE-KMS, Block Public Access and a restrictive bucket policy, and state locking — S3-native lock files in current Terraform, or a DynamoDB table in older setups. Separate keys or buckets per environment and layer, created by a small bootstrap configuration, with applies running only from CI using OIDC roles.

"count vs for_each?"

count creates indexed instances; removing an item from the middle shifts indexes and causes unrelated resources to be changed or recreated. for_each keys instances by map keys or set values, so adding or removing one item affects only that item. I use for_each for collections and count for conditional zero-or-one resources.

"Someone changed a security group in the console. What happens and what do you do?"

The next plan shows drift and would revert it. I'd find out why it was changed — if it was an emergency fix that should stay, I update the code; otherwise apply to revert. If we just need state to reflect reality, apply -refresh-only. Long term: scheduled drift detection with plan -detailed-exitcode, and restricting console write access in managed accounts.

"How do you bring existing resources under Terraform?"

With import blocks — declare the target address and the resource ID, optionally let plan -generate-config-out draft the configuration — then tune the code until plan shows no changes and apply. The older terraform import CLI does the same imperatively.

"A plan unexpectedly wants to replace the production RDS instance. What do you do?"

Stop and don't apply. Read which attribute is marked "forces replacement" — often an engine version format, a subnet group, an identifier, or a refactor that changed the resource address. Fix the config, use a moved block for address changes, or ignore_changes for externally managed attributes, and add prevent_destroy plus deletion protection as guardrails.

"Workspaces or separate directories for environments?"

I prefer separate directories or stacks per environment, ideally in separate AWS accounts with their own state and roles, calling the same versioned modules — it gives isolation, lets dev run newer module versions before prod, and makes it hard to apply to the wrong environment. Workspaces are fine for identical, short-lived copies like feature environments.

"How do you handle secrets in Terraform?"

Keep them out of code and committed tfvars, mark variables sensitive, prefer AWS-managed secrets such as RDS-managed master passwords in Secrets Manager, read others from Secrets Manager or SSM at apply time, use ephemeral resources or write-only arguments where supported, and protect the state as if it were a secret because values can end up there. CI uses OIDC roles rather than stored keys.

"Do you have Terraform experience?" (an honest answer when it isn't from production)

"In production I've used CloudFormation, and I've built a Terraform EKS platform in my portfolio repo: a VPC module sized for VPC CNI pod IPs, an EKS module with managed node groups, access entries and Pod Identity, S3 remote state with locking, and GitHub Actions running plan and apply with OIDC. The concepts map directly — declarative resources, dependency graphs, change preview — and the main differences I've learned to handle are state management, module versioning and careful plan review because there's no automatic rollback."

"CloudFormation vs Terraform?"

CloudFormation is AWS-native, AWS manages the state, and failed stack updates roll back automatically. Terraform is multi-cloud and multi-service with a huge provider and module ecosystem and a clearer plan output, but you manage the state yourself and there's no automatic rollback. For an AWS-only shop either works; Terraform is the more common skill and helps when you also manage things like Datadog, GitHub or Cloudflare.