Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 153/ 160
08. Data Quality, Governance, Security & AI Infrastructure

Infrastructure as Code (IaC) for Data Platforms with Terraform

Provision warehouses, buckets, IAM, and clusters reproducibly with Terraform.

IntermediateTerraform · IaC · IAM3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Pinned providers, modules, variables, and policies. Next, OIDC credential, remote state, diff, and policy check. Finally, detect changes, import, key/role lifecycle.

Think of it like…

Terraform is a construction blueprint with a state ledger and change order; the blueprint should never contain the vault key, and demolition needs a separate safety interlock.

Where you’ve seen it

lifecycle { prevent_destroy = true } protects a data bucket, a GCS/S3 backend holds encrypted state with versioning, and GitHub OIDC assumes a narrow Terraform role before terraform plan -out=review.plan gates apply.

Words you’ll hear

Terraform
Infrastructure-as-code tool using HCL configuration, providers, state, and plan/apply workflows.
IaC
Declarative infrastructure treated as reviewable/versioned source rather than manually configured consoles.
IAM
Identity and policy determining whether Terraform or data workloads may create buckets, warehouses, roles, and keys.
Operational boundary
Storing secrets or unencrypted state can leak credentials, a broad destroy in the same plan can erase data buckets, and provider authentication that is too powerful defeats least privilege in every apply. Use remote encrypted/locked state, plan/apply through reviewed environments, prevent deletion on stateful resources, pin providers/modules, use separate narrow deployment roles, and import/drift-detect existing data infrastructure.

Visual diagram

Loading interactive diagram

Theory & deep dive

Terraform: the working mechanism

Terraform declares cloud data resources-buckets, warehouses, IAM, KMS, networks, clusters-and records state so CI can review plans, apply controlled diffs, and recreate infrastructure consistently.

Pinned providers, modules, variables, and policies. OIDC credential, remote state, diff, and policy check, then approval, locking, stateful resource guard, and evidence; this exposes the state governed by IAM.

Infrastructure as Code for Data Platforms with Terraform in one concrete design

lifecycle { prevent_destroy = true } protects a data bucket, a GCS/S3 backend holds encrypted state with versioning, and GitHub OIDC assumes a narrow Terraform role before terraform plan -out=review.plan gates apply.

Key takeaway: Terraform declares cloud data resources-buckets, warehouses, IAM, KMS, networks, clusters-and records state so CI can review plans, apply controlled diffs, and recreate infrastructure consistently

Infrastructure as Code for Data Platforms with Terraform: failure mode and production response

Storing secrets or unencrypted state can leak credentials, a broad destroy in the same plan can erase data buckets, and provider authentication that is too powerful defeats least privilege in every apply.

Use remote encrypted/locked state, plan/apply through reviewed environments, prevent deletion on stateful resources, pin providers/modules, use separate narrow deployment roles, and import/drift-detect existing data infrastructure. Track State lock as "one apply for an environment at a time", and make Provider pin visible to the dataset owner.

Key takeaway: Use remote encrypted/locked state, plan/apply through reviewed environments, prevent deletion on stateful resources, pin providers/modules, use separate narrow deployment roles, and import/drift-detect existing data infrastructure.

How it works, step by step

Step 1 of 4
Terraform

1. Declare

Pinned providers, modules, variables, and policies. Terraform declares cloud data resources-buckets, warehouses, IAM, KMS, networks, clusters-and records state so CI can review plans, apply controlled diffs, and recreate infrastructure consistently.

pinned providers, modules, variables, and policies
Key numbers at this step
State lock
one apply for an environment at a time
IaC
Declarative infrastructure treated as reviewable/versioned source rather than manually configured consoles.

When to use it & trade-offs

What it is

Terraform declares cloud data resources-buckets, warehouses, IAM, KMS, networks, clusters-and records state so CI can review plans, apply controlled diffs, and recreate infrastructure consistently. lifecycle { prevent_destroy = true } protects a data bucket, a GCS/S3 backend holds encrypted state with versioning, and GitHub OIDC assumes a narrow Terraform role before terraform plan -out=review.plan gates apply.

Why it matters: Storing secrets or unencrypted state can leak credentials, a broad destroy in the same plan can erase data buckets, and provider authentication that is too powerful defeats least privilege in every apply. Use remote encrypted/locked state, plan/apply through reviewed environments, prevent deletion on stateful resources, pin providers/modules, use separate narrow deployment roles, and import/drift-detect existing data infrastructure.

When to reach for it

  • Provision warehouses, buckets, IAM, and clusters reproducibly with Terraform.
  • Choose Terraform because it provides this capability: Infrastructure-as-code tool using HCL configuration, providers, state, and plan/apply workflows.
  • Keep evidence for State lock at "one apply for an environment at a time" so the last production stage-Drift/rotate - detect changes, import, key/role lifecycle-remains testable.

When to avoid or be careful

  • Storing secrets or unencrypted state can leak credentials, a broad destroy in the same plan can erase data buckets, and provider authentication that is too powerful defeats least privilege in every apply.
  • Do not conflate IaC with Terraform. The second concept contributes different behavior: Declarative infrastructure treated as reviewable/versioned source rather than manually configured consoles.
  • Release only after this guardrail is in place: Use remote encrypted/locked state, plan/apply through reviewed environments, prevent deletion on stateful resources, pin providers/modules, use separate narrow deployment roles, and import/drift-detect existing data infrastructure.

Terraform / IaC / IAM - roles, limits, and operating cost

Terraform
Best for: Infrastructure-as-code tool using HCL configuration, providers, state, and plan/apply workflows.

Strengths: Infrastructure-as-code tool using HCL configuration, providers, state, and plan/apply workflows. Terraform declares cloud data resources-buckets, warehouses, IAM, KMS, networks, clusters-and records state so CI can review plans, apply controlled diffs, and recreate infrastructure consistently.

Watch out: Storing secrets or unencrypted state can leak credentials, a broad destroy in the same plan can erase data buckets, and provider authentication that is too powerful defeats least privilege in every apply.

IaC
Best for: Declarative infrastructure treated as reviewable/versioned source rather than manually configured consoles.

Strengths: Declarative infrastructure treated as reviewable/versioned source rather than manually configured consoles. In this workflow it supports: Apply - approval, locking, stateful resource guard, and evidence.

Watch out: The operating cost is real: versions and checksums committed in lock file.

IAM
Best for: Identity and policy determining whether Terraform or data workloads may create buckets, warehouses, roles, and keys.

Strengths: Identity and policy determining whether Terraform or data workloads may create buckets, warehouses, roles, and keys. Its output can be checked with Destructive plan.

Watch out: Operating guardrail: Use remote encrypted/locked state, plan/apply through reviewed environments, prevent deletion on stateful resources, pin providers/modules, use separate narrow deployment roles, and import/drift-detect existing data infrastructure.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for Terraform, and how would State lock reveal it?”
  • “Why is IaC distinct from this behavior: identity and policy determining whether Terraform or data workloads may create buckets, warehouses, roles, and keys?”
  • Production clue: Storing secrets or unencrypted state can leak credentials, a broad destroy in the same plan can erase data buckets, and provider authentication that is too powerful defeats least privilege in every apply.
  • Design probe: Use remote encrypted/locked state, plan/apply through reviewed environments, prevent deletion on stateful resources, pin providers/modules, use separate narrow deployment roles, and import/drift-detect existing data infrastructure.

Benchmarks & rules of thumb

State lock -
one apply for an environment at a time
Provider pin -
versions and checksums committed in lock file
Destructive plan -
zero unreviewed delete/replace on stateful data resources

Common mistakes & follow-ups

Common interview mistakes

  • Storing secrets or unencrypted state can leak credentials, a broad destroy in the same plan can erase data buckets, and provider authentication that is too powerful defeats least privilege in every apply.
  • Reporting success from a job exit code without checking State lock and Provider pin.
  • Treating this example as complete without replay, rollback, or access boundaries: lifecycle { prevent_destroy = true } protects a data bucket, a GCS/S3 backend holds encrypted state with versioning, and GitHub OIDC assumes a narrow Terraform role before terraform plan -out=review.plan gates apply.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: Infrastructure-as-code tool using HCL configuration, providers, state, and plan/apply workflows.
  • What does the recovery plan do when this occurs? Storing secrets or unencrypted state can leak credentials, a broad destroy in the same plan can erase data buckets, and provider authentication that is too powerful defeats least privilege in every apply.
  • When is Terraform insufficient for Provider pin, and what does adding IaC cost in State lock?
Interview tip: Lead with the data path and failure boundary: Terraform declares cloud data resources-buckets, warehouses, IAM, KMS, networks, clusters-and records state so CI can review plans, apply controlled diffs, and recreate infrastructure consistently. Then make the answer operational: Use remote encrypted/locked state, plan/apply through reviewed environments, prevent deletion on stateful resources, pin providers/modules, use separate narrow deployment roles, and import/drift-detect existing data infrastructure.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Infrastructure as Code (IaC) for Data Platforms with Terraform with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Privacy Regulations Compliance (GDPR, DPDP Act, HIPAA, Right to be Forgotten)
Next topic
Monitoring & Alerting for Data Infrastructure (Prometheus, Grafana, OpenTelemetry)