Skip to the page

CI/CD and infrastructure as code · senior

Importing, structuring and reviewing Terraform, and Ansible

Large teams import existing infrastructure in steps, split state by component, review code for count and secrets problems, and use Ansible for configuration inside servers.

The notes

What you must know

  • Import: inventory first (AWS Config, Resource Explorer, tags) and decide what to delete. Then foundations (network, IAM), data stores, compute.
  • Use import blocks in code (Terraform 1.5 and later), reviewed in a pull request, instead of one-off CLI imports. plan -generate-config-out writes HCL as a starting point; clean it up and remove hard-coded ids.
  • Run plan until it shows no changes before you add new work.
  • Layout for many teams: a shared modules repository with tagged versions, and live repositories with a folder per environment and component, each with its own state key.
  • Workspaces share code and differ only in state. Folders make differences and permissions explicit, and most teams use folders for production.
  • Pull request checks: fmt, validate, tflint, a security scan and plan output as a comment. Apply runs from CI with a role per environment and approvals for production.
  • count keys resources by position. Removing the first item of a list shifts the others and replaces them. for_each over a map keys by name, so removing one entry removes only that resource.
  • Hard-coded keys in provider blocks end up in Git history; use the default credential chain and OIDC.
  • Ansible configures machines and is push-based and idempotent. Use handlers so Nginx reloads only when a config changed. Terraform creates infrastructure; Ansible configures what runs inside it.

The red pen

Common traps: mistake → fix

  • Mistake: Import everything at once

    Fix: do it component by component and plan until clean.

  • Mistake: count is fine for lists of subnets

    Fix: removing an item shifts the rest.

  • Mistake: Ansible and Terraform do the same job

    Fix: infrastructure versus configuration.

Easy to hard

Questions interviewers ask, easy to hard

  1. How do you bring 200 console-built resources under Terraform?
  2. Design Terraform for six teams.
  3. What is wrong with this Terraform for three subnets and a bastion?
  4. Write an Ansible playbook for Nginx that reloads only when the config changes.

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview