{
  "schema_version": 1,
  "kind": "article",
  "title": "Checking infrastructure changes with GitHub Actions",
  "description": "Add Terraform validation, security scanning, and cost estimates to a GitHub Actions workflow before changes reach production.",
  "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions",
  "markdown_url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions.md",
  "json_url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions.json",
  "language": "en",
  "author": {
    "type": "Organization",
    "name": "CloudOn",
    "url": "https://cloudon-one.com/"
  },
  "rights_url": "https://cloudon-one.com/terms",
  "indexable": true,
  "headings": [
    {
      "level": 2,
      "text": "The Challenge",
      "id": "the-challenge",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#the-challenge"
    },
    {
      "level": 2,
      "text": "Solution Overview",
      "id": "solution-overview",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#solution-overview"
    },
    {
      "level": 2,
      "text": "Key Components",
      "id": "key-components",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#key-components"
    },
    {
      "level": 3,
      "text": "1. Security Scanning",
      "id": "1-security-scanning",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#1-security-scanning"
    },
    {
      "level": 3,
      "text": "2. Cost Management with Infracost",
      "id": "2-cost-management-with-infracost",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#2-cost-management-with-infracost"
    },
    {
      "level": 3,
      "text": "3. Infrastructure Validation Suite",
      "id": "3-infrastructure-validation-suite",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#3-infrastructure-validation-suite"
    },
    {
      "level": 2,
      "text": "Implementation Details",
      "id": "implementation-details",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#implementation-details"
    },
    {
      "level": 3,
      "text": "Workflow Triggers",
      "id": "workflow-triggers",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#workflow-triggers"
    },
    {
      "level": 3,
      "text": "Concurrency Control",
      "id": "concurrency-control",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#concurrency-control"
    },
    {
      "level": 3,
      "text": "Performance Optimizations",
      "id": "performance-optimizations",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#performance-optimizations"
    },
    {
      "level": 2,
      "text": "Best Practices",
      "id": "best-practices",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#best-practices"
    },
    {
      "level": 3,
      "text": "Security",
      "id": "security",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#security"
    },
    {
      "level": 3,
      "text": "Cost Management",
      "id": "cost-management",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#cost-management"
    },
    {
      "level": 3,
      "text": "Code Quality",
      "id": "code-quality",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#code-quality"
    },
    {
      "level": 2,
      "text": "Getting Started",
      "id": "getting-started",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#getting-started"
    },
    {
      "level": 2,
      "text": "Conclusion",
      "id": "conclusion",
      "url": "https://cloudon-one.com/blog/infra-validation-pipeline-github-actions#conclusion"
    }
  ],
  "date_published": "2025-01-06T00:00:00.000Z",
  "tags": [
    "GitHub Actions",
    "CI/CD",
    "Security",
    "Terraform",
    "DevSecOps"
  ],
  "content_markdown": "---\ntitle: Checking infrastructure changes with GitHub Actions\ndescription: Add Terraform validation, security scanning, and cost estimates to a GitHub Actions workflow before changes reach production.\nurl: https://cloudon-one.com/blog/infra-validation-pipeline-github-actions\nauthor: CloudOn\nlanguage: en\ndate_published: '2025-01-06T00:00:00.000Z'\ntags:\n  - GitHub Actions\n  - CI/CD\n  - Security\n  - Terraform\n  - DevSecOps\nrights_url: https://cloudon-one.com/terms\n---\n\n# Checking infrastructure changes with GitHub Actions\n\nInfrastructure 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.\n\nThis guide brings those checks together in a GitHub Actions pipeline for Terraform.\n\n## The Challenge\n\nModern infrastructure deployments face several critical challenges:\n\n- Preventing security vulnerabilities and leaked credentials\n- Managing and forecasting cloud costs\n- Maintaining code quality and consistency\n- Ensuring compliance with best practices\n\n## Solution Overview\n\nOur pipeline addresses these challenges through a multi-faceted approach implemented as a GitHub Actions workflow. The pipeline automatically triggers on:\n\n- Pull requests that modify infrastructure files\n- Push events to the main/master branch\n- Changes to workflow configurations or pre-commit hooks\n\n## Key Components\n\n### 1. Security Scanning\n\nThe pipeline implements two layers of security scanning:\n\n**GitGuardian Integration**\n\nGitGuardian performs deep security scanning of the entire git history to detect:\n\n- Exposed secrets and credentials\n- API keys and tokens\n- Other sensitive information\n\nThe scan requires a GitGuardian API key and analyzes the complete commit history, ensuring no sensitive data slips through even in historical commits.\n\n**TFSec Analysis**\n\nTFSec provides specialized security scanning for Terraform configurations:\n\n- Identifies security issues and best practice violations\n- Generates detailed JSON reports\n- Operates in soft-fail mode to avoid blocking deployments\n- Includes comprehensive scan statistics\n\n### 2. Cost Management with Infracost\n\nThe pipeline incorporates sophisticated cost management through Infracost integration:\n\n**Pull Request Analysis**\n- Generates differential cost estimates between base and PR branches\n- Posts detailed cost breakdowns directly on pull requests\n- Provides clear visibility into the financial impact of changes\n\n**Continuous Cost Tracking**\n- Updates cost data on main branch changes\n- Maintains historical cost trends in Infracost Cloud\n- Tracks PR status changes (merged/closed) for accurate cost history\n\n### 3. Infrastructure Validation Suite\n\nA comprehensive validation suite runs in a Docker container to ensure code quality:\n\n**Pre-commit Checks**\n- Enforces code formatting standards\n- Runs customizable quality checks\n- Ensures consistency across the codebase\n\n**Terraform-specific Validation**\n- TFLint for Terraform best practices\n- Terraform init and validate operations\n- Verification of required provider configurations\n\n## Implementation Details\n\n### Workflow Triggers\n\n```yaml\non:\n  pull_request:\n    paths:\n      - '**.tf'\n      - '**.tfvars'\n      - '**.hcl'\n      - 'Dockerfile'\n      - '.github/workflows/**'\n      - '.pre-commit-config.yaml'\n```\n\n### Concurrency Control\n\n```yaml\nconcurrency:\n  group: infra-checks-${{ github.ref }}\n  cancel-in-progress: true\n```\n\n### Performance Optimizations\n\n- Docker layer caching using GitHub Actions cache\n- Concurrent job execution\n- Automatic cancellation of redundant runs\n\n## Best Practices\n\n### Security\n- Store sensitive API keys as GitHub secrets\n- Use soft-fail mode for security scans to avoid blocking critical updates\n- Implement comprehensive git history scanning\n\n### Cost Management\n- Review cost impacts before merging PRs\n- Track historical cost trends\n- Maintain visibility of infrastructure expenses\n\n### Code Quality\n- Enforce provider configuration requirements\n- Implement pre-commit hooks\n- Use consistent formatting and validation\n\n## Getting Started\n\n1. Clone the [GitHub repo](https://github.com/cloudon-one) with the pipeline sample\n2. Configure Required Secrets:\n   - `GITGUARDIAN_API_KEY`\n   - `INFRACOST_API_KEY`\n3. Set Up Prerequisites:\n   - Define `required_providers` blocks in Terraform files\n   - Configure pre-commit hooks\n   - Ensure Docker availability in the GitHub Actions environment\n4. Enable the Pipeline:\n   - Add the workflow file to `.github/workflows/`\n   - Configure any custom pre-commit hooks\n   - Set up branch protection rules\n\n## Conclusion\n\nThe workflow combines validation, security scanning, and cost estimates in one review process. Adjust the required checks and failure thresholds to fit your repositories.\n\nThe pipeline's automated nature ensures consistent validation without manual intervention, while the comprehensive reporting helps teams make informed decisions about their infrastructure changes.\n\nRemember 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.\n",
  "content_sha256": "7917d09423e795f424f20fecab40df217716740d78f481d07e9c434ff75a8047"
}
