How do you structure Terraform code for multiple environments like dev, staging and production?
What the interviewer wants to hear
- Reusable, versioned modules
- Separate state per environment and often per component
- Environment-specific variables, not copy-pasted code
- Plan in CI on pull requests, apply with approvals
- Pin provider and module versions; limit blast radius
Model answer
I put shared building blocks such as the network, the Kubernetes cluster and the database into reusable modules, versioned with Git tags or a private registry. Each environment then has a small root configuration that calls those modules with its own variables, for example instance sizes or replica counts. Every environment has its own remote state with locking, and I often split state by component, like network and apps, so one bad apply cannot break everything; this limits the blast radius. Provider and module versions are pinned. In CI, every pull request runs fmt, validate, a linter and terraform plan, and posts the plan for review. Apply to production happens from the pipeline after approval, not from laptops. Tools like Terragrunt or Terraform Stacks can reduce repetition when there are many environments.
Can you answer it under time pressure?
Take a timed medium round and get scored on every answer.
More medium DevOps Engineer questions
- How do you manage secrets such as database passwords and API keys across CI/CD pipelines and Kubernetes?
- A Pod is stuck in CrashLoopBackOff after a deployment. How do you troubleshoot it?
- Compare rolling, blue-green and canary deployments. How do you decide which to use?
- Developers complain that the CI pipeline takes 40 minutes. How would you make it faster?
- Your team is burning out from on-call pages. How would you redesign alerting and the on-call process?
- When would you package Kubernetes manifests with Helm and when with Kustomize? Can they be combined?
- Users report that one service on Kubernetes became slow since this morning. Walk me through how you would investigate.
- How would you promote a release from dev to staging to production in a GitOps setup with Argo CD?
- How do you include database schema migrations in a continuous delivery pipeline without causing downtime?