Policy as code

Terraform Security Toolkit

Production-ready, battle-tested policy-as-code guardrails for Google Cloud, AWS and Azure. Prevent critical misconfigurations in code before infrastructure is provisioned.

Downloadable repository · Terraform 1.5+
REPOSITORY: /toolkit-repo/ VERSION: v1.4.0 STANDARDS: CIS v3.0 · NCSC CLOUD LICENCE: MIT
View Documentation (.md) ⬇
Roll out safely without breaking CI/CD:Always test organisation policies, SCPs and Azure Policies in a non-production project or audit/dry-run mode first, verify service-account dependencies, and document exception owners before enforcing across production.
gcp_org_policies.tfDownload .tf ⬇
01 / GOOGLE CLOUD ORGANISATION GUARDRAILS

Prevent credential leakage & public exposure.

# 1. Prevent creation of static, long-lived service account JSON keys
resource "google_org_policy_policy" "disable_sa_keys" {
  name   = "projects/${var.project_id}/policies/iam.disableServiceAccountKeyCreation"
  parent = "projects/${var.project_id}"

  spec {
    rules {
      enforce = "TRUE"
    }
  }
}

# 2. Enforce Public Access Prevention across all GCS Buckets
resource "google_org_policy_policy" "no_public_buckets" {
  name   = "projects/${var.project_id}/policies/storage.publicAccessPrevention"
  parent = "projects/${var.project_id}"

  spec {
    rules {
      enforce = "TRUE"
    }
  }
}
aws_scp_guardrails.tfDownload .tf ⬇
02 / AWS SERVICE CONTROL POLICIES

Deny root usage & mandate IMDSv2 tokens.

# 1. Deny root account API actions & mandate IMDSv2
resource "aws_organizations_policy" "baseline_guardrails" {
  name        = "sachan-partners-aws-baseline-scp"
  description = "Denies root account actions and enforces IMDSv2"
  type        = "SERVICE_CONTROL_POLICY"

  content = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Sid       = "DenyRootUserActions"
        Effect    = "Deny"
        Action    = "*"
        Resource  = "*"
        Condition = {
          StringLike = { "aws:PrincipalArn" = "arn:aws:iam::*:root" }
        }
      },
      {
        Sid       = "RequireIMDSv2"
        Effect    = "Deny"
        Action    = "ec2:RunInstances"
        Resource  = "arn:aws:ec2:*:*:instance/*"
        Condition = {
          StringNotEquals = { "ec2:MetadataHttpTokens" = "required" }
        }
      }
    ]
  })
}
azure_security_policies.tfDownload .tf ⬇
03 / AZURE CUSTOM POLICY BASELINE

Block public IP attachments on NICs.

# 1. Deny direct Public IP attachments to VM NICs
resource "azurerm_policy_definition" "deny_vm_public_ip" {
  name         = "deny-nic-public-ip"
  policy_type  = "Custom"
  mode         = "Indexed"
  display_name = "Deny Public IP on Network Interfaces"

  policy_rule = jsonencode({
    if = {
      allOf = [
        { field = "type", equals = "Microsoft.Network/networkInterfaces" },
        { not = { field = "Microsoft.Network/networkInterfaces/ipConfigurations[*].publicIpAddress.id", exists = false } }
      ]
    }
    then = {
      effect = "deny"
    }
  })
}
04 / CI/CD AUTOMATION CHECKS

Scan Terraform before merge.

name: "IaC Security Guardrails"
on: [pull_request]

jobs:
  trivy-scan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Scan IaC with Trivy
        uses: aquasecurity/trivy-action@master
        with:
          scan-type: 'config'
          exit-code: '1'
          severity: 'CRITICAL,HIGH'
05 / REVIEW BEFORE APPLY

Separate detection from enforcement.

Start with audit or deny-with-exemption modes, identify real dependencies, and record owners for every exception before a guardrail blocks delivery.

  • Test in a representative non-production project or subscription.
  • Review existing pipelines, service identities and break-glass paths.
  • Promote only after the owner can explain the expected failure mode.
06 / OPERATING MODEL

Make the control durable.

Policy-as-code is useful when it is versioned, reviewed, deployed through an approved pipeline and checked again for drift. Pair every control with evidence, an exception route and a clear operational owner.

Read the guardrails guide ↗

A clear next step

Deploy cloud security guardrails across your estate.

Protect developer speed without compromising multi-cloud security baselines.

Request a guardrails engagement