1. Docs
  2. Pulumi IDP
  3. Best Practices
  4. Patterns
  5. Policies as tests

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:

    // policies/security-policies.ts
    import { PolicyPack, validateResourceOfType } from "@pulumi/policy";
    import { 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:

    # 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.

      IDP Builder Course. Register Now.