← Back to Engineering Blog
πŸ—“οΈ Jul 22, 2024⏱️ 5 min read

Policy as Code: Blocking Non-Compliant Terraform PRs at the CI/CD Pipeline Gate

Why post-provisioning security audits cost $50,000 in incident response, and how embedding Checkov static analysis in GitHub Actions blocks 100% of non-compliant Terraform PRs.

πŸŽ™οΈ Listen to ArticleREADY
AI Audio Synthesis Narrator
Share Post:

β€œDiscovering an unencrypted storage account or open RDP port in production costs $50,000 in incident response overhead and legal audits. Catching it in a GitHub Actions Pull Request costs zero dollars and takes 45 seconds.”

In July 2024, in my current role as Associate Director, Cloud Architecture & AI, I led platform security governance for an enterprise Azure Enterprise Landing Zone factory.

Our platform team managed infrastructure-as-code (IaC) repositories used by 15 application development teams across the enterprise.

Every day, developers submitted dozens of GitHub Pull Requests containing Terraform modules defining virtual networks, Key Vaults, storage accounts, and Kubernetes clusters.

Development velocity was fast. Security compliance was a minefield.


The Post-Provisioning Security Trap

Under the client’s legacy security model, infrastructure security was evaluated after resources were provisioned in production.

A Cloud Security Posture Management (CSPM) tool scanned active Azure subscriptions every 24 hours.

If a developer accidentally merged a Terraform PR containing a public storage account (public_network_access_enabled = true), the CSPM tool flagged the violation the next morning.

By then, the damage was already done:

  1. Forensic Audit Overhead: The Security Operations Center (SOC) had to declare a P1 security incident, pull access logs, isolate the resource, and verify whether sensitive data had been exfiltrated.
  2. High Incident Cost: Each post-provisioning incident investigation cost an average of $50,000 USD in engineering hours, legal compliance reviews, and emergency patch deployments.
  3. Destructive Remediation: Fixing a misconfigured production database disk often required destroying the storage account, causing unplanned application downtime.

The security lead attempted to solve the problem by requiring a manual code review by a Senior Security Architect for every single pull request.

The result was operational paralysis. A queue of 25 PRs sat waiting for review every morning, and developers waited an average of four business days for a 10-line Terraform code change to be approved.


The Mess: The 3:00 AM Public Storage Exfiltration Panic

In June 2024, before automated pipeline gates were installed, a developer working on a data analytics module needed to test file uploads from a external vendor.

He temporarily added public_network_access_enabled = true and enable_https_traffic_only = false to a shared storage module.

The manual security reviewer missed the 2-line configuration change in a 1,200-line PR diff and approved the pull request.

At 3:00 AM on Sunday, an automated internet port scanner discovered the publicly accessible storage account and downloaded 40 Gigabytes of unencrypted customer telemetry logs.

# Security Operations Incident Log
ALERT: Public Blob Access Enabled on storage account 'stprodanalytics01'
DATA_EXFILTRATION_DETECTED: 42,190 MB transferred to unverified external IP 185.220.101.5

The company spent $65,000 in forensic investigation costs and was forced to notify regulatory bodies.

Relying on manual code reviews for infrastructure security was a proven failure.


The Solution: Shift-Left Policy as Code with Checkov in GitHub Actions

We removed manual security code reviews and embedded Checkov static policy-as-code analysis directly into our GitHub Actions CI/CD pipelines.

Checkov parses Terraform HCL code before terraform plan or terraform apply ever runsβ€”scanning configurations against 1,000+ built-in security policies.

# GitHub Actions DevSecOps Pipeline Workflow
name: 'Checkov Policy-as-Code PR Gate'

on:
  pull_request:
    branches: [main]

jobs:
  checkov-static-scan:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Run Checkov Static Security Scan
        uses: bridgecrewio/checkov-action@master
        with:
          framework: terraform
          output_format: cli
          soft_fail: false # Hard fail PR build on HIGH/CRITICAL security violations
          check: CKV_AZURE_3,CKV_AZURE_33,CKV_AZURE_35,CKV_AZURE_109

Custom Policy-as-Code (Enterprise Tagging & Compliance)

In addition to standard CIS benchmarks, we wrote custom Checkov policies in YAML to enforce enterprise-specific governanceβ€”such as mandatory cost-center tagging and region restrictions:

# Custom Checkov Policy: Enforce Mandatory Tags on Azure Resources
metadata:
  id: 'CUSTOM_AZURE_001'
  name: 'Ensure all Azure resources have mandatory CostCenter and Owner tags'
  category: 'CONVENTIONS'
definition:
  and:
    - attribute: 'tags.CostCenter'
      operator: 'exists'
    - attribute: 'tags.Owner'
      operator: 'exists'

The Automated 45-Second Pull-Request Feedback Loop

When a developer submits a pull request:

  1. GitHub Actions triggers Checkov automatically.
  2. In 45 seconds, Checkov scans every .tf file.
  3. If a developer attempts to expose port 22 to 0.0.0.0/0 or disable storage encryption, Checkov sets a FAILURE status on the GitHub PR and posts an automated inline comment pointing directly to the exact file and line number:
🚨 **Checkov Security Gate Failure:**

- **Policy Failed:** `CKV_AZURE_3` - Ensure Storage Account public access is disabled
- **File:** `modules/storage/main.tf` (Line 42)
- **Violation:** `public_network_access_enabled` is set to `true`.
- **Remediation:** Set `public_network_access_enabled = false` and use private endpoints.

The pull request merge button is locked automatically. No non-compliant code can ever reach production.


The Impact

  • Zero Production Misconfigurations: Blocked 140+ non-compliant Terraform PRs before code was merged over a 6-month period.
  • Sub-Minute Feedback: Reduced security review latency from 4 business days (manual review) to 45 seconds (Checkov pipeline gate).
  • $0 Incident Overhead: Eliminated post-provisioning incident response costs for IaC security misconfigurations.

Key Takeaway

Embed Checkov Static Security Scanning as a Mandatory CI/CD Pipeline Gate.

Do not rely on slow manual code reviews or post-provisioning CSPM audits for cloud security governance. Embed Checkov Policy as Code directly into your GitHub Actions pipelines to scan Terraform HCL files on every pull request, enforce custom enterprise tagging rules, and hard-block non-compliant merges before resources are ever provisioned in the cloud.


Architecture and decisions: mine. Debugging sessions at odd hours: mine. AI assistance: structure, syntax, first draft. β€” Sachin

SKS

Sachin Kumar Sharma

Associate Director (Infrastructure & Cloud Architecture Strategy) | 20+ Yrs Exp

Architecting resilient multi-cloud enterprise landing zones, SDN overlay fabrics, DevSecFinOps automation pipelines, and autonomous Agentic AI platforms.

πŸ“¬

πŸ“¬ Stay Updated on Tech Releases

Sign up to get notified when I publish new production war stories, agentic AI architecture blueprints, or open-source infrastructure tools.

⚑ Theme Adaptive Shift
Switching layouts matching domain reading affinity...