Prompt
CI/CD for infrastructure changes
Latest observation
Here’s a practical CI/CD approach for infrastructure changes (IaC), whether you use Terraform, Pulumi, CloudFormation, Bicep, or Ansible.
Core idea
Treat infrastructure like application code:
- store it in Git
- validate on every PR
- require approval before production
- apply changes automatically only after checks pass
Typical CI/CD flow
1) Developer makes a change
Example: updates a Terraform module or environment config in a feature branch.
2) Pull request triggers CI
Run checks such as:
- format/lint
- static validation
- security scan
- unit tests for modules/policies
plan/ dry-run
3) Review plan
Post the plan output to the PR so reviewers can see:
- what will be created
- what will be changed
- what will be destroyed
4) Merge to main
After approval, merge to main triggers deployment workflow.
5) CD applies to target environment
Use:
- dev automatically
- staging automatically or with approval
- prod with manual approval / change window
Recommended pipeline stages
Build/Validate
terraform fmt -checkterraform validatetflintcheckovortfsec- policy checks via OPA/Conftest if needed
Plan
terraform plan -out=tfplan- upload artifact
- comment summary on PR
Approval gate
- required reviewer
- environment approval in GitHub/GitLab/Azure DevOps
- optional change management ticket
Apply
terraform apply tfplan- or equivalent IaC deploy command
Post-deploy verification
- smoke tests
- drift checks
- health checks / resource probes
Best practices
Separate environments
Use separate state and accounts/subscriptions/projects for:
- dev
- staging
- prod
Keep state remote and locked
Examples:
- Terraform Cloud
- S3 + DynamoDB lock
- Azure Storage backend
- GCS backend
Use immutable reviewable plans
Never apply unreviewed infrastructure changes directly.
Prefer least privilege
CI/CD runners should have only the permissions needed for the target environment.
Don’t store secrets in code
Use:
- Vault
- cloud secret managers
- CI secret stores
Make destructive changes explicit
Require manual approval if a plan includes:
- resource deletion
- replacement of critical resources
- network/security group changes
Use modules and versioning
Version infrastructure components so changes are predictable and reusable.
Example GitHub Actions flow for Terraform
name: terraform-ci
on:
pull_request:
paths:
- "infra/**"
push:
branches: ["main"]
paths:
- "infra/**"
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
- run: terraform fmt -check
working-directory: infra
- run: terraform init -backend=false
working-directory: infra
- run: terraform validate
working-directory: infra
plan:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
- run: terraform init
working-directory: infra
- run: terraform plan
working-directory: infra
apply:
if: github.event_name == 'push'
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
- run: terraform init
working-directory: infra
- run: terraform apply -auto-approve
working-directory: infra
Common mistakes to avoid
- applying directly from a laptop
- using one state file for everything
- allowing prod deploys from feature branches
- not reviewing the plan
- mixing app and infra deploy logic without boundaries
- ignoring drift
Good operational model
A strong pattern is:
- PR = validation + plan
- main = deploy to dev/staging
- manual approval = prod
- scheduled drift detection
- alerts on failed applies
If you want, I can also give you:
- a GitHub Actions, GitLab CI, or Azure DevOps example
- a Terraform-only pipeline design
- a production-grade branching and approval model