Terraform & IaC Questions with Answers

24 practice questions on Terraform & IaC (DevOps & Kubernetes) with answers and step-by-step solutions. Try 10 samples below, then practise all 24 free in the AptiNest app.

Sample questions

Question 1Easy

What is a Terraform provider?

  1. A plugin that maps resources to a platform's API
  2. A person who approves plans
  3. A reusable set of modules
  4. A backend that stores the state remotely for a team and handles locking between applies

Answer: A. A plugin that maps resources to a platform's API

Explanation

Providers such as aws, google or kubernetes know how to create, read, update and delete resources.

Step-by-step solution

  1. Terraform providers connect to cloud platforms like AWS or Google Cloud.
  2. Providers translate Terraform code into API calls for those platforms.
  3. Each provider has specific functions for managing resources on its platform.
  4. These functions include creating, reading, updating and deleting resources.
  5. This makes providers essential for deploying infrastructure as code.

Why the other options are wrong

  • B. A person who approves plans is not a Terraform provider; that would be a reviewer role.
  • C. A set of modules is a module collection, not a provider; providers connect to platform APIs.
  • D. A remote state backend stores state and locks it; it is not a provider.

Tip: Remember: Providers are tools that let Terraform interact with different platforms.

Question 2Easy

Infrastructure as Code (IaC) means:

  1. Hiring a team to configure servers by hand
  2. Writing application code on servers directly
  3. Clicking through a cloud console and taking screenshots of each step
  4. Defining infrastructure in versioned files

Answer: D. Defining infrastructure in versioned files

Explanation

Config files can be reviewed, versioned and applied repeatably, unlike manual console changes.

Step-by-step solution

  1. Infrastructure as Code uses text files to define systems.
  2. These files are stored in version control systems.
  3. Changes are tracked and repeated automatically.
  4. Manual steps like screenshots or hand configuration are not used.
  5. Final answer: Defining infrastructure in versioned files.

Why the other options are wrong

  • A. Hiring people to configure servers by hand is the manual approach IaC replaces.
  • B. Writing application code on servers is app development, not defining infrastructure.
  • C. Screenshots of console clicks are a manual record and cannot recreate the setup.

Tip: Use code to manage servers, not manual clicks.

Question 3Easy

What does terraform init do?

  1. Creates all resources
  2. Prepares the directory: downloads providers and sets up the backend
  3. Writes a starter configuration with example resources for every major cloud
  4. Destroys all resources

Answer: B. Prepares the directory: downloads providers and sets up the backend

Explanation

Run it first in a new working directory or after changing providers, modules or backend.

Step-by-step solution

  1. Terraform init downloads required provider plugins.
  2. It also sets up the backend configuration for state management.
  3. The command prepares the working directory for future operations.
  4. It checks for changes in providers or modules and updates accordingly.
  5. This ensures all necessary tools are ready before applying configurations.
  6. Final answer: Prepares the directory: downloads providers and sets up the backend.

Why the other options are wrong

  • A. Creating resources is apply's job; init only prepares the directory.
  • C. init does not write a starter configuration; you write the configuration yourself.
  • D. Destroying resources is terraform destroy, not init.

Tip: Always run terraform init first in a new directory or after changing providers/backend.

Question 4Easy

Why review Terraform changes through pull requests?

  1. Git refuses to store .tf files that have not passed through a pull request with an approved review
  2. Teammates can check the plan before it changes real infrastructure
  3. Pull requests encrypt the state
  4. It makes terraform apply faster

Answer: B. Teammates can check the plan before it changes real infrastructure

Explanation

Code review plus a posted plan catches mistakes such as accidental deletions before apply.

Step-by-step solution

  1. Terraform changes can create, alter or delete real infrastructure.
  2. A pull request lets teammates review the code and the plan before it is merged and applied.
  3. The correct answer is option B, teammates can check the plan before it changes real infrastructure.

Why the other options are wrong

  • A. Git does not refuse to store files based on whether a pull request was approved.
  • C. A pull request does not encrypt the state.
  • D. A review does not make terraform apply run faster.

Tip: Post the plan in the pull request and review any deletions before apply.

Question 5Medium

count vs for_each when creating several similar resources:

  1. They behave identically in every case
  2. for_each keys by name, so removing one does not shift others
  3. count is keyed by map keys, while for_each is keyed by numeric indexes starting from zero
  4. for_each can only create exactly two resources

Answer: B. for_each keys by name, so removing one does not shift others

Explanation

With count, deleting an item in the middle renumbers later ones and may recreate them; for_each uses stable keys.

Step-by-step solution

  1. Count renumbers resources when one is deleted.
  2. For_each keeps keys stable even if some are removed.
  3. So removing one in count shifts others, but not in for_each.
  4. This makes for_each safer for partial deletions.
  5. Final answer: for_each keys by name, so removing one does not shift others.

Why the other options are wrong

  • A. They are not identical; count indexes by position, while for_each keys by name.
  • C. This swaps the two: for_each uses map or set keys, and count uses numeric indexes starting at zero.
  • D. for_each is not limited to two resources; it can create any number from a map or set.

Tip: Use for_each for predictable resource naming and deletion behavior.

Question 6Medium

Why is applying the same Terraform configuration twice usually safe?

  1. The second run is ignored by the cloud provider
  2. Terraform refuses to run twice in one day
  3. It deletes everything before each apply, then recreates it, so the result is always identical
  4. It is declarative: if reality matches, nothing changes

Answer: D. It is declarative: if reality matches, nothing changes

Explanation

Idempotent convergence is a core property of declarative IaC.

Step-by-step solution

  1. Terraform uses a declarative approach to define desired infrastructure.
  2. If the current state matches the desired state, no changes are made.
  3. This ensures that applying the same configuration twice has no effect.
  4. Thus, it is safe to run multiple times without unintended changes.
  5. The final answer is It is declarative: if reality matches, nothing changes.

Why the other options are wrong

  • A. The provider does not ignore a second run; Terraform compares state and plans only the real differences.
  • B. Terraform has no daily limit; it runs as often as you call it.
  • C. Deleting and recreating everything each time would be destructive; Terraform does not do that by design.

Tip: Remember: Terraform compares current state with desired state before making any changes.

Question 7Medium

Why do teams store Terraform state in a remote backend with locking?

  1. Remote state makes every plan run faster because the provider skips its API refresh calls
  2. Local state cannot hold more than ten resources
  3. So team members share one state and cannot apply concurrently
  4. Locking encrypts the state file

Answer: C. So team members share one state and cannot apply concurrently

Explanation

Shared state avoids divergent copies, and locks stop two applies from corrupting it.

Step-by-step solution

  1. Remote state lets the team share one source of truth.
  2. Locking blocks two applies at once.
  3. That avoids corrupted state.

Why the other options are wrong

  • A. It does not speed up plans.
  • B. Local state has no such limit.
  • D. Locking does not encrypt.

Tip: Remote + lock = shared, safe.

Question 8Medium

Immutable infrastructure means:

  1. Infrastructure cannot be deleted once created
  2. Servers are never updated at all after launch, so they keep the original packages for their lifetime
  3. Configuration files cannot be edited
  4. Servers are replaced with new ones instead of patched in place

Answer: D. Servers are replaced with new ones instead of patched in place

Explanation

New images are built and rolled out, avoiding snowflake servers that drift over time.

Step-by-step solution

  1. Immutable infrastructure means changes are made by replacing servers, not updating them.
  2. New servers are created with updated configurations instead of modifying old ones.
  3. This avoids inconsistencies caused by partial updates or configuration drift.
  4. Servers are replaced entirely, ensuring uniformity and reliability.
  5. Final answer: Servers are replaced with new ones instead of patched in place.

Why the other options are wrong

  • A. Immutable does not mean undeletable; old servers are deleted once their replacements are live.
  • B. Servers are not kept unchanged for life; an updated server is built as a new image and replaces the old one.
  • C. Config files can still be edited; immutable means the servers are replaced, not that files are frozen.

Tip: Think replacement over modification for immutable setups.

Question 9Hard

A plan shows a resource will be 'destroyed and then created' (replaced). Why?

  1. Terraform always replaces resources on every apply
  2. The provider is out of date
  3. A changed argument cannot be updated in place
  4. The state lock expired

Answer: C. A changed argument cannot be updated in place

Explanation

Some attributes (e.g. a disk's zone) are immutable; changing them forces replacement, which may cause downtime.

Step-by-step solution

  1. Some arguments, such as a disk's zone, cannot be changed on an existing resource.
  2. When such an argument changes, Terraform must destroy the resource and create it again.
  3. The correct answer is option C, a changed argument cannot be updated in place.

Why the other options are wrong

  • A. Terraform does not replace resources on every apply; only when a change requires it.
  • B. An outdated provider does not by itself force a replacement.
  • D. A state lock controls concurrent runs; it does not cause a replacement.

Tip: Check the plan for replacements before applying, since they can cause downtime.

Question 10Hard

An existing database was created manually. How do you bring it under Terraform without recreating it?

  1. Run terraform plan with -refresh=false
  2. Copy its ID into a variable, and Terraform will adopt the database automatically on the next plan
  3. Write its resource block and import it into state
  4. Delete it and run terraform apply

Answer: C. Write its resource block and import it into state

Explanation

Importing links the real resource to the configuration so future changes are managed.

Step-by-step solution

  1. Write the resource block for the database in Terraform.
  2. Use the import command to link the existing database to the configuration.
  3. Terraform will now manage the database without recreating it.
  4. This ensures changes are tracked and applied correctly.
  5. Final answer: Write its resource block and import it into state.

Why the other options are wrong

  • A. A plan with -refresh=false does not bring a resource into state; it only skips reading current state.
  • B. Terraform does not adopt a resource from a variable value; an import is needed to link it to state.
  • D. Deleting the database would destroy the data it holds, which is what the question wants to avoid.

Tip: Use import to adopt existing resources without losing data.

Practise all 24 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all DevOps & Kubernetes topics · All topics