Back to sections

Infrastructure as Code Interview Questions & Answers

A sample of real Infrastructure as Code interview questions with full answers and explanations - practice for interviews or certification exams.

easy

1. [IaC General] What does declarative infrastructure mean?

You manually click in the cloud console
You describe the desired end state; the tool figures out how to achieve itCorrect
You use imperative shell scripts only
You script step-by-step commands to configure resources

Declarative means you describe the outcome you want ('I want 3 servers running this image') and let the tool figure out how to get there, rather than writing out every individual step yourself like you would in an imperative script or by clicking through a console manually.

medium

2. [CloudFormation] How do you handle secrets (e.g., DB password) securely?

Hardcode in template
Store in S3
Use environment variables in Lambda
Use Parameters with NoEcho: true + AWS Secrets Manager referenceCorrect

Putting a password directly in a template means it's sitting in plaintext for anyone with template access to read. Marking the Parameter NoEcho: true hides its value from the console and CLI output, and pointing it at AWS Secrets Manager means the actual secret is fetched securely at deploy time instead of ever being stored in the template.

hard

3. [Ansible] How do you run a playbook against hosts with different OSes (e.g., Ubuntu/CentOS) using correct package managers?

Write separate playbooks
Assume all are Ubuntu
Use raw module
Use ansible_os_family fact + conditional tasksCorrect

Ubuntu uses apt and CentOS uses yum, so a single hardcoded package-install task would break on one of them. The ansible_os_family fact tells you which family a host belongs to, so a task can branch and use the right package manager automatically instead of you maintaining entirely separate playbooks per OS.

easy

4. What is the purpose of the terraform.tfstate file?

Stores the configuration code
Defines variable defaults
Tracks the current state of deployed infrastructure (real-world mapping)Correct
Logs all Terraform commands

Terraform needs some way to remember what it's already created and how that maps to your actual cloud resources, and that's exactly what the state file does. Without it, Terraform couldn't tell what's changed (drift) or compute an accurate plan for what to do next.

medium

5. [CloudFormation] What does DeletionPolicy: Retain do on an S3 bucket resource?

Automatically backs up the bucket
Prevents the bucket from being created
Encrypts the bucket
Keeps the bucket (and its data) after stack deletionCorrect

Deleting a CloudFormation stack normally deletes everything in it, including data you might actually want to keep, like an S3 bucket's contents. Setting DeletionPolicy: Retain tells CloudFormation to leave that specific resource (and its data) alone even when the rest of the stack gets torn down.

hard

6. [Terraform] Your terraform apply fails midway. How do you safely recover without manual intervention?

Use terraform refresh then terraform applyCorrect
Use terraform state rm to delete broken resource
Let Terraform auto-recover on next apply
Run terraform destroy and re-apply

When an apply fails partway through, Terraform's state file might not perfectly reflect what actually got created in the real world. Running terraform refresh first reconciles the state with reality, so the next apply computes an accurate plan for just what's still missing, instead of blindly retrying things that already succeeded.

easy

7. [Terraform] Which command initializes a working directory (downloads providers, modules)?

terraform apply
terraform initCorrect
terraform plan
terraform validate

Before Terraform can do anything, it needs to download the right provider plugins and set up its backend, and that's exactly what terraform init does. It's the required first step, nothing else (plan, apply, validate) works until init has run.

medium

8. A Terraform configuration creates an S3 bucket and, separately, runs a local-exec provisioner that executes a script referencing that bucket by name, but no resource attribute in the HCL actually points from one resource to the other. During apply, the script sometimes runs before the bucket exists yet. What is the correct fix?

Move both resources into the same module so they are created in the order they're written
Reorder the resource blocks alphabetically in the file so the bucket is declared before the provisioner's resource
Add a `sleep 30` at the start of the script so the bucket has time to finish creating first
Add `depends_on = [aws_s3_bucket.this]` to the resource running the provisioner, to explicitly declare the ordering Terraform can't infer on its ownCorrect

Terraform builds its dependency graph mainly from implicit references, meaning one resource's argument pointing at another resource's attribute; when there's no such reference, as with a provisioner that just shells out and touches a bucket by name, Terraform has no way to know the two are related and may create them in parallel. depends_on exists for exactly this situation, forcing an explicit ordering even without a direct attribute reference. The order resources are written in a file has no effect on apply order, since that's driven entirely by the dependency graph, and a fixed sleep is an unreliable workaround rather than an actual fix.

hard

9. Two engineers run terraform apply on the same state file within seconds of each other, from their own laptops, with no remote backend configured. What's the real risk?

Terraform will queue the second apply automatically
This only matters if they're modifying different resources
Nothing, Terraform detects and merges concurrent changes
State file corruption or lost changes, since local state has no locking - the two applies can race and overwrite each other's stateCorrect

Without a remote backend that supports locking, nothing stops two people from writing to the SAME state file at the exact same moment. Since Terraform rewrites the WHOLE state file on every apply, not just the parts that changed, a race between two applies can corrupt the file or silently erase one person's changes, even if they were working on totally different resources.

easy

10. [Terraform] How do you reference a variable named region in HCL?

var.region
{{ region }}
$regionCorrect
${region}

In Terraform's language (HCL), a variable you declared isn't referenced with a dollar sign or double curly braces, it always uses the var. prefix, like var.region, whether you're using it on its own or inside a string.

Want to practice under real conditions?

Try the full timed Infrastructure as Code quiz.

    Welcome to OpsQuiz!

    Real scenario-based DevOps questions, hands-on practice, and clear explanations for every answer.