Infrastructure as Code with Terraform
Define buckets, datasets, warehouses, roles and clusters as code with Terraform so data infrastructure is reviewable, reproducible and easy to tear down.
Intermediate · about 5 hours · difficulty 3/5
Learn first: Cloud Fundamentals for Data Engineers
Clicking through a cloud console is fine for learning, but production infrastructure should be defined in code, reviewed in pull requests and recreated reliably. Terraform is the most common tool for this, and the DE Zoomcamp teaches it early.
Core concepts
- Providers: plugins for AWS, Google Cloud, Snowflake, Databricks and more.
- Resources: the things you create (an S3 bucket, a BigQuery dataset, an IAM role).
- Variables and outputs: parameterise environments (dev, prod) and expose values.
- State: Terraform's record of what exists. Store it remotely (S3 or GCS backend) with locking for teams; never commit it to Git, as it can contain secrets.
- Modules: reusable bundles (a "data lake bucket with lifecycle rules and encryption" module).
The workflow
terraform init, terraform plan (review changes), terraform apply, and terraform destroy to clean up after practice. In teams, CI runs plan on pull requests and apply after merge.
What data teams manage with it
Buckets and lifecycle rules, warehouse databases and roles (Snowflake and BigQuery providers), service accounts and IAM, Kafka topics, Composer or MWAA environments, and Databricks workspaces and jobs.
Alternatives
Pulumi (infrastructure in Python or TypeScript), AWS CloudFormation and CDK, and OpenTofu (an open source Terraform fork).
Pitfalls
- Manual console changes that drift from the code.
- Applying without reading the plan and destroying a production dataset.
- Local state files lost or committed with secrets.
You are ready to move on when you can create your project's bucket, dataset or warehouse, and service account with Terraform, review a plan, apply it, and destroy it cleanly.
Key concepts
- Providers and resources
- Variables and outputs
- Remote state and locking
- plan, apply, destroy
- Modules
- Terraform in CI
- Pulumi and CDK
Mini project
Write Terraform for your portfolio project's cloud resources (bucket with lifecycle rule, BigQuery dataset or Redshift Serverless namespace, a least-privilege service account). Use remote state, run plan in GitHub Actions, and destroy everything after testing.
Check yourself
Why should Terraform state never be committed to a Git repository, and where should it live instead?
Free resources
- Terraform documentation (HashiCorp)
- Terraform tutorials (HashiCorp)
- Data Engineering Zoomcamp (Terraform module) (DataTalksClub)