CI/CD Pipeline
In our Lego city analogy, the CI/CD pipeline is like the automated conveyor belt that delivers new building blocks to the construction site. Instead of engineers manually carrying bricks (running terraform apply), the pipeline ensures every change is tested, reviewed, and deployed automatically.
This chapter shows how to integrate Terraform with CI/CD tools (GitHub Actions, Jenkins, GitLab CI, or Terraform Cloud) to achieve automation, consistency, and speed.
Key Concepts
1. Why CI/CD for Terraform?
- Consistency: Every change follows the same workflow.
- Automation: No manual intervention needed.
- Collaboration: Teams can review plans before apply.
- Speed: Faster deployments with fewer errors.
2. Typical Pipeline Stages
- Validate: Run
terraform fmtandterraform validate. - Plan: Generate execution plan (
terraform plan). - Policy Check: Apply Sentinel or custom policies.
- Approval: Manual approval step for production.
- Apply: Run
terraform applyto deploy changes.
3. GitHub Actions Example
name: Terraform CI/CD
on:
push:
branches: [ "main" ]
jobs:
terraform:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
- name: Terraform Init
run: terraform init
- name: Terraform Validate
run: terraform validate
- name: Terraform Plan
run: terraform plan -out=tfplan
- name: Terraform Apply
if: github.ref == 'refs/heads/main'
run: terraform apply tfplan
4. Terraform Cloud Integration
- Connect repo to Terraform Cloud workspace.
- Pipeline triggers
planandapplyautomatically. - Sentinel policies enforce compliance.
- Notifications sent to Slack/email.
Hands‑On Lab / Demo
Lab: CI/CD with GitHub Actions
- Create
.github/workflows/terraform.yml. - Add stages: init, validate, plan, apply.
- Push code → GitHub Actions runs pipeline.
- Review plan output before apply.
- Observe automated deployment in Terraform Cloud.
Pro Tips & Best Practices
- Always run
terraform validatebefore plan. - Use separate pipelines for dev, staging, and prod.
- Require manual approval for production applies.
- Store secrets securely (GitHub Secrets, Vault).
- Integrate cost estimation to avoid surprises.
Summary & Cheatsheet
- CI/CD = Automated conveyor belt for deployments.
- Stages: Validate → Plan → Policy → Approval → Apply.
- Tools: GitHub Actions, Jenkins, GitLab CI, Terraform Cloud.
- Best practice: Automate everything, but approve production manually.
Quick mnemonic: Validate → Plan → Apply → Automate
The Hackers Notebook
The CI/CD pipeline is the automation engine of your 3‑tier infrastructure. By integrating Terraform with GitHub Actions, Jenkins, or Terraform Cloud, you ensure every change is tested, reviewed, and deployed seamlessly.

Updated on Dec 31, 2025