CI/CD: Terraform, dbt, and Schema Evolution
Master Snowflake CI/CD with Terraform for infrastructure, dbt for transformations, and schema evolution strategies.
20+ years shipping high-throughput database systems. Notes here come from systems that actually shipped.
- ✓Basic knowledge of SQL and Snowflake
- ✓Familiarity with Git and CI/CD concepts
- ✓Terraform installed (v1.0+)
- ✓dbt installed (v1.0+)
- ✓Snowflake account with ACCOUNTADMIN access
- Use Terraform to manage Snowflake warehouses, databases, and roles as code.
- Use dbt for version-controlled SQL transformations and testing.
- Handle schema changes with backward-compatible migrations and zero-downtime deploys.
- Automate CI/CD pipelines with GitHub Actions or similar tools.
- Monitor and rollback using dbt artifacts and Snowflake time travel.
Think of Snowflake CI/CD like a construction crew building a skyscraper. Terraform is the blueprint for the building's structure (warehouses, databases). dbt is the team that installs the interior (tables, views, transformations). Schema evolution is like renovating a floor while the building is still occupied—you need to avoid breaking things and ensure everyone can still work. CI/CD automates the process so changes are tested and deployed safely.
In modern data engineering, Snowflake has become a leading cloud data warehouse, but managing its infrastructure and data transformations manually is error-prone and unscalable. Enter CI/CD—Continuous Integration and Continuous Deployment—which brings software engineering best practices to data pipelines. This tutorial focuses on three key tools: Terraform for infrastructure-as-code, dbt (data build tool) for SQL transformations, and strategies for schema evolution. You'll learn how to define Snowflake resources (warehouses, databases, roles) in Terraform, version-control your dbt models, and handle schema changes without breaking production. We'll walk through a real-world production incident where a schema change caused a downstream outage, and how to debug it. By the end, you'll have a production-ready CI/CD pipeline that ensures your Snowflake environment is reproducible, testable, and resilient.
1. Setting Up Terraform for Snowflake
Terraform allows you to define Snowflake resources as code. Start by installing the Snowflake provider. Create a main.tf file with provider configuration. Use variables for sensitive info like account and private key. Define a virtual warehouse, database, and schema. Example: resource 'snowflake_warehouse' 'my_wh' { name = 'my_warehouse' warehouse_size = 'X-SMALL' auto_suspend = 60 }. Then run terraform init, plan, apply. Best practice: use remote state (e.g., S3) and separate environments (dev, prod) with workspaces.
2. Managing dbt Models and Tests
dbt transforms data in your warehouse using SQL SELECT statements. Initialize a dbt project with 'dbt init'. Configure profiles.yml with Snowflake connection details. Create models in the models/ folder. Use Jinja templating for dynamic SQL. Example: a model that aggregates sales by month. Add tests using schema.yml: define unique, not_null, and custom tests. Run 'dbt run' to build models and 'dbt test' to validate. Use 'dbt docs generate' for documentation.
3. Schema Evolution Strategies
Schema evolution is inevitable. Use backward-compatible changes: add columns as NULLable, avoid renaming/dropping without deprecation. In dbt, use 'on_schema_change' to automatically add new columns. For breaking changes, create new models and deprecate old ones. Use Snowflake's Time Travel to rollback if needed. Example: adding a column to a source table. Update dbt model to include the new column. Test with 'dbt test' before deploying.
4. Building a CI/CD Pipeline with GitHub Actions
Automate testing and deployment. Create a .github/workflows/ci.yml file. Trigger on pull requests to main. Steps: checkout code, set up Terraform, run 'terraform plan' for dev. Set up dbt, run 'dbt deps', 'dbt run', 'dbt test'. If all pass, merge. Then a deploy workflow runs 'terraform apply' and 'dbt run' on prod. Use secrets for Snowflake credentials. Example workflow snippet.
5. Zero-Downtime Deployments with Blue-Green Strategy
For critical pipelines, use blue-green deployment. Maintain two environments (blue and green). Deploy changes to the inactive environment, test, then switch traffic. In Snowflake, this can be achieved by using separate schemas or databases. Use dbt's 'target' configuration to point to different schemas. Example: blue schema 'prod_v1', green schema 'prod_v2'. After validation, update the view or alias to point to the new schema. Use Terraform to manage the switch.
6. Monitoring and Rollback Strategies
Even with CI/CD, issues can slip through. Use dbt's 'source freshness' to detect stale data. Set up alerts on dbt test failures. For rollbacks, Snowflake Time Travel allows you to restore tables to a previous state (up to 90 days). Use 'UNDROP' for dropped objects. In dbt, use 'dbt run --full-refresh' to rebuild models from scratch. For Terraform, use 'terraform state rm' to remove resources and re-import. Example: rolling back a dbt model.
The Silent Column Drop That Broke Dashboards
- Never use SELECT * in production dbt models; always list columns explicitly.
- Always check downstream dependencies before altering schemas.
- Use dbt's 'ref' and 'source' freshness to detect breaking changes.
- Implement a CI step that runs dbt test before deploying schema changes.
- Have a rollback plan (e.g., Time Travel, versioned deployments).
ALTER TABLE my_table UNDROP COLUMN my_column;dbt run --select my_model| File | Command / Code | Purpose |
|---|---|---|
| main.tf | provider "snowflake" { | 1. Setting Up Terraform for Snowflake |
| models | SELECT | 2. Managing dbt Models and Tests |
| schema.yml | version: 2 | 3. Schema Evolution Strategies |
| .github | name: CI | 4. Building a CI/CD Pipeline with GitHub Actions |
| dbt_profiles.yml | snowflake: | 5. Zero-Downtime Deployments with Blue-Green Strategy |
| rollback.sql | ALTER TABLE my_table UNDROP; | 6. Monitoring and Rollback Strategies |
Key takeaways
Common mistakes to avoid
3 patternsUsing SELECT * in dbt models
Running Terraform apply directly on production without plan
Not testing schema changes in CI
Interview Questions on This Topic
How would you implement a CI/CD pipeline for Snowflake using Terraform and dbt?
Frequently Asked Questions
20+ years shipping high-throughput database systems. Notes here come from systems that actually shipped.
That's Snowflake. Mark it forged?
3 min read · try the examples if you haven't