Pulumi Policy Python SDK

1.20.0 · active · verified Thu Apr 16

Pulumi's Policy Python SDK defines and manages policies for cloud resources deployed through Pulumi. Policy rules run during `pulumi preview` and `pulumi up`, asserting that cloud resource definitions comply with policies immediately before they are created or updated. It is currently at version 1.20.0 and follows a regular release cadence as part of the broader Pulumi ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This example defines a Pulumi Policy Pack in Python that includes a single policy. The `s3-no-public-read` policy ensures that no AWS S3 bucket can be created or updated with a `public-read` or `public-read-write` ACL. If such a bucket is detected during `pulumi preview` or `pulumi up`, the deployment will be halted due to the `MANDATORY` enforcement level. This code would typically reside in `__main__.py` within a policy pack directory created by `pulumi policy new aws-python`.

import pulumi
from pulumi_policy import PolicyPack, ResourceValidationPolicy, EnforcementLevel, validateResourceOfType
import pulumi_aws as aws

def s3_bucket_no_public_read_policy(args: aws.s3.Bucket, report_violation):
    if args.acl == 'public-read' or args.acl == 'public-read-write':
        report_violation(f"S3 Bucket '{args.id}' should not be publicly readable.")

PolicyPack(
    name="aws-s3-security",
    policies=[
        ResourceValidationPolicy(
            name="s3-no-public-read",
            description="Prohibits setting the publicRead or publicReadWrite permission on AWS S3 buckets.",
            enforcement_level=EnforcementLevel.MANDATORY,
            validate=validateResourceOfType(aws.s3.Bucket, s3_bucket_no_public_read_policy)
        )
    ]
)

view raw JSON →