Skip to content

IDP Pattern: Policies as tests ​

Description ​

This pattern involves writing Pulumi policies that enforce organizational standards, security requirements, and compliance rules by running them as automated tests during deployment. Policies act as guardrails that prevent non-compliant infrastructure from being deployed.

When to use this pattern ​

  • Compliance requirements: When you need to enforce regulatory or organizational standards
  • Security governance: When you want to prevent security misconfigurations automatically
  • Standardization: When you need consistent infrastructure patterns across teams
  • Automated enforcement: When manual reviews are too slow or error-prone

When NOT to use this pattern ​

  • Rapid prototyping: When you need to quickly test ideas without governance overhead
  • Unique requirements: When applications legitimately need to deviate from standard policies
  • Small teams: When policy maintenance overhead exceeds the benefits

How to use this pattern ​

Policies are written as code and can be integrated into CI/CD pipelines to automatically validate infrastructure before deployment.

Example ​

Organization creates policies for common security requirements:

typescript
// policies/security-policies.ts
import { PolicyPack, validateResourceOfType } from "@pulumi/policy";
import * as aws from "@pulumi/aws";

new PolicyPack("security-policies", {
  policies: [
    {
      name: "s3-bucket-encryption",
      description: "S3 buckets must have encryption enabled",
      enforcementLevel: "mandatory",
      validateResource: validateResourceOfType(aws.s3.Bucket, (bucket, args, reportViolation) => {
        if (!bucket.serverSideEncryptionConfiguration) {
          reportViolation("S3 bucket must have encryption enabled");
        }
      }),
    },
    {
      name: "rds-backup-retention",
      description: "RDS instances must have backup retention >= 7 days",
      enforcementLevel: "mandatory",
      validateResource: validateResourceOfType(aws.rds.Instance, (instance, args, reportViolation) => {
        if (!instance.backupRetentionPeriod || instance.backupRetentionPeriod < 7) {
          reportViolation("RDS instance must have backup retention >= 7 days");
        }
      }),
    },
  ],
});

Teams run policies as part of their deployment process:

bash
# In CI/CD pipeline
pulumi preview --policy-pack ./policies/security-policies
pulumi up --policy-pack ./policies/security-policies

This ensures that all infrastructure deployments automatically comply with organizational security and governance standards.

Content from the official Pulumi documentation (github.com/pulumi/docs).