Why Automate Terraform?
Manual terraform apply works for learning. In production, you need:
- Automated planning on pull requests
- Approval gates before applying
- Consistent environments
- Audit trails
GitHub Actions Workflow
name: Terraform
on:
push:
branches: [main]
pull_request:
branches: [main]
permissions:
contents: read
pull-requests: write
jobs:
terraform:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.7.0
- name: Terraform Init
run: terraform init
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Terraform Format Check
run: terraform fmt -check
- name: Terraform Validate
run: terraform validate
- name: Terraform Plan
id: plan
run: terraform plan -no-color -out=tfplan
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Comment PR with Plan
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const plan = `${{ steps.plan.outputs.stdout }}`;
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `## Terraform Plan\n\`\`\`\n${plan}\n\`\`\``
});
- name: Terraform Apply
if: github.ref == 'refs/heads/main'
run: terraform apply -auto-approve tfplan
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}Master this topic with hands-on labs
Go beyond reading — build real projects in sandboxed environments with expert video guidance.
Browse Courses →Safety Checks
- Format check — enforces consistent style
- Validate — catches syntax errors
- Plan on PR — reviewers see what will change
- Apply only on main — no accidental deployments from branches
- Plan output as PR comment — visibility for reviewers
Environment Separation
jobs:
deploy-staging:
environment: staging
steps:
- run: terraform workspace select staging
deploy-prod:
needs: deploy-staging
environment: production
steps:
- run: terraform workspace select prodGet weekly IT automation tips
Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.
Subscribe Free →Best Practices
- Never auto-apply without review for production
- Use OIDC instead of long-lived AWS keys
- Lock state to prevent concurrent applies
- Pin Terraform version across all environments
- Store plan artifacts for audit trails
Learn More
Build production CI/CD workflows for Terraform in our Terraform for Beginners course.
---
Ready to go deeper? Check out our hands-on course: Terraform for Beginners — practical exercises you can follow along on your own machine.
Ready to learn by doing?
Stop reading tutorials — start building. Expert video courses with hands-on labs in real sandboxed environments.
Related Articles
GitHub Actions CI/CD for Terraform
Automate Terraform with GitHub Actions. Plan on PR, apply on merge, remote state locking, and secure secrets for IaC pipelines.
CI/CD Pipeline Tutorial from Scratch
Build a complete CI/CD pipeline from scratch with GitHub Actions. Lint, test, build, and deploy your application — fully automated on every push to your.
GitHub Actions CI/CD Advanced Guide
Advanced GitHub Actions for CI/CD. Matrix builds, reusable workflows, dependency caching, artifacts, environments, and self-hosted runners.
Terraform Course: What You Learn
What a Terraform course teaches you. Skills, exercises, AWS provisioning, and career benefits of learning Terraform for IaC.
Terraform Expert Tips and Patterns
Level up Terraform skills with expert patterns. Dynamic blocks, for_each, moved blocks, validation, and production workflows.
Terraform HCL Syntax: A Practical Guide
Master HashiCorp Configuration Language (HCL) — the syntax behind every Terraform configuration. Blocks, arguments, expressions, and functions.
Explore topics
Browse more articles on the topics covered here.