GitHub Actions CI/CD Security Terraform DevSecOps

Checking infrastructure changes with GitHub Actions

CO
CloudOn
|
Read as Markdown JSON
In this article
  1. The Challenge
  2. Solution Overview
  3. Key Components
  4. Implementation Details
  5. Best Practices
  6. Getting Started
  7. Conclusion

Infrastructure changes need more than a syntax check. A useful pull request workflow also checks for security issues, estimates costs, and gives reviewers enough context to assess a change.

This guide brings those checks together in a GitHub Actions pipeline for Terraform.

The Challenge

Modern infrastructure deployments face several critical challenges:

  • Preventing security vulnerabilities and leaked credentials
  • Managing and forecasting cloud costs
  • Maintaining code quality and consistency
  • Ensuring compliance with best practices

Solution Overview

Our pipeline addresses these challenges through a multi-faceted approach implemented as a GitHub Actions workflow. The pipeline automatically triggers on:

  • Pull requests that modify infrastructure files
  • Push events to the main/master branch
  • Changes to workflow configurations or pre-commit hooks

Key Components

1. Security Scanning

The pipeline implements two layers of security scanning:

GitGuardian Integration

GitGuardian performs deep security scanning of the entire git history to detect:

  • Exposed secrets and credentials
  • API keys and tokens
  • Other sensitive information

The scan requires a GitGuardian API key and analyzes the complete commit history, ensuring no sensitive data slips through even in historical commits.

TFSec Analysis

TFSec provides specialized security scanning for Terraform configurations:

  • Identifies security issues and best practice violations
  • Generates detailed JSON reports
  • Operates in soft-fail mode to avoid blocking deployments
  • Includes comprehensive scan statistics

2. Cost Management with Infracost

The pipeline incorporates sophisticated cost management through Infracost integration:

Pull Request Analysis

  • Generates differential cost estimates between base and PR branches
  • Posts detailed cost breakdowns directly on pull requests
  • Provides clear visibility into the financial impact of changes

Continuous Cost Tracking

  • Updates cost data on main branch changes
  • Maintains historical cost trends in Infracost Cloud
  • Tracks PR status changes (merged/closed) for accurate cost history

3. Infrastructure Validation Suite

A comprehensive validation suite runs in a Docker container to ensure code quality:

Pre-commit Checks

  • Enforces code formatting standards
  • Runs customizable quality checks
  • Ensures consistency across the codebase

Terraform-specific Validation

  • TFLint for Terraform best practices
  • Terraform init and validate operations
  • Verification of required provider configurations

Implementation Details

Workflow Triggers

on:
  pull_request:
    paths:
      - '**.tf'
      - '**.tfvars'
      - '**.hcl'
      - 'Dockerfile'
      - '.github/workflows/**'
      - '.pre-commit-config.yaml'

Concurrency Control

concurrency:
  group: infra-checks-${{ github.ref }}
  cancel-in-progress: true

Performance Optimizations

  • Docker layer caching using GitHub Actions cache
  • Concurrent job execution
  • Automatic cancellation of redundant runs

Best Practices

Security

  • Store sensitive API keys as GitHub secrets
  • Use soft-fail mode for security scans to avoid blocking critical updates
  • Implement comprehensive git history scanning

Cost Management

  • Review cost impacts before merging PRs
  • Track historical cost trends
  • Maintain visibility of infrastructure expenses

Code Quality

  • Enforce provider configuration requirements
  • Implement pre-commit hooks
  • Use consistent formatting and validation

Getting Started

  1. Clone the GitHub repo with the pipeline sample
  2. Configure Required Secrets:
    • GITGUARDIAN_API_KEY
    • INFRACOST_API_KEY
  3. Set Up Prerequisites:
    • Define required_providers blocks in Terraform files
    • Configure pre-commit hooks
    • Ensure Docker availability in the GitHub Actions environment
  4. Enable the Pipeline:
    • Add the workflow file to .github/workflows/
    • Configure any custom pre-commit hooks
    • Set up branch protection rules

Conclusion

The workflow combines validation, security scanning, and cost estimates in one review process. Adjust the required checks and failure thresholds to fit your repositories.

The pipeline’s automated nature ensures consistent validation without manual intervention, while the comprehensive reporting helps teams make informed decisions about their infrastructure changes.

Remember that this pipeline is just the beginning — you can extend it with additional checks, custom validations, and integration with other tools to meet your specific needs.

CO

CloudOn engineering

Practical notes on AWS, Google Cloud, Kubernetes, and infrastructure code from the CloudOn engineering blog.