Day 05 Mastery

CI/CD with Terraform

Run Terraform in GitHub Actions, use Terraform Cloud for remote state, and implement plan-on-PR, apply-on-merge.

~1 hour Hands-on Precision AI Academy

Today's Objective

Learn the core concepts of CI/CD with Terraform and apply them in practical exercises.

01

GitHub Actions Workflow

.github/workflows/terraform.yml
.github/workflows/terraform.yml
name: Terraform

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  terraform:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4

    - uses: hashicorp/setup-terraform@v3
      with:
        terraform_version: 1.7.0
        cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

    - name: Terraform Init
      run: terraform init

    - name: Terraform Plan
      run: terraform plan -no-color
      # On PRs, plan output is posted as a comment

    - name: Terraform Apply
      if: github.ref == 'refs/heads/main' && github.event_name == 'push'
      run: terraform apply -auto-approve
Terraform Cloud Setup
Terraform Cloud Setup
# 1. Create account at app.terraform.io
# 2. Create organization and workspace
# 3. Add backend config:
terraform {
  cloud {
    organization = "your-org"
    workspaces {
      name = "production"
    }
  }
}
# 4. Store AWS credentials as workspace variables
# 5. Generate API token → store as GitHub secret TF_API_TOKEN
ℹ️
The pull-request → plan, merge → apply workflow is the gold standard for infrastructure changes. Every change is reviewed before it lands. The plan output on the PR shows exactly what will change. This prevents accidental destroys.

Supporting References & Reading

Go deeper with these external resources.

Terraform Docs
CI/CD with Terraform Official HashiCorp Terraform documentation.
YouTube
CI/CD with Terraform Terraform tutorials on YouTube
MDN
MDN Web Docs Comprehensive web technology reference

Day 5 Checkpoint

Before moving on, confirm understanding of these key concepts:

Course Complete
Return to Course Overview