Pulumi Policy Python SDK
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
-
409 conflict: Another update is currently in progress
cause Another Pulumi update is already running on the stack, or a previous update crashed, leaving a stale lease.fixConfirm no other users are running updates. If not, use `pulumi cancel` in the stack directory to clear the stale lease. -
Policy violations not being reported during pulumi preview/up
cause The policy pack is not correctly referenced (locally) or enabled (in Pulumi Cloud).fixIf testing locally, ensure you are running `pulumi preview --policy-pack <path-to-your-policy-pack-directory>`. If expecting cloud enforcement, verify the policy pack is published and associated with a Policy Group in your Pulumi Cloud organization. -
Error: after mutation of snapshot
cause This error message indicates an internal bug within the Pulumi engine related to its data structure self-check.fixThis is a rare error and usually points to a Pulumi bug. It is recommended to open a GitHub issue with Pulumi, providing detailed steps to reproduce the issue and diagnostic logs.
Warnings
- breaking Some Pulumi-authored policy packs (e.g., `pulumi-awsguard`) were released as 'Preview' and explicitly stated they were subject to breaking changes. While the core `pulumi-policy` SDK is more stable, be cautious with specific policy libraries and always check their release notes.
- gotcha Policies with `EnforcementLevel.MANDATORY` will halt `pulumi up` operations if a violation is detected. This prevents non-compliant resources from being provisioned or updated.
- gotcha Policy Packs are only evaluated locally when the `--policy-pack <path-to-policy-pack-directory>` flag is explicitly passed to `pulumi preview` or `pulumi up`. For central enforcement across an organization, policy packs must be published to Pulumi Cloud and associated with a Policy Group.
- gotcha During policy evaluation, any secrets defined in your Pulumi stack configuration or resources are decrypted and accessible in plaintext to the policy code. Treat your policy code with the same security considerations as your infrastructure code.
- gotcha `pulumi stack import` and `pulumi refresh` commands do not trigger policy evaluations. Policies are primarily designed to run during `pulumi preview` and `pulumi up` to validate changes before or during deployment.
Install
-
pip install pulumi-policy -
pulumi policy new aws-python
Imports
- PolicyPack
from pulumi_policy import PolicyPack
- ResourceValidationPolicy
from pulumi_policy import ResourceValidationPolicy
- StackValidationPolicy
from pulumi_policy import StackValidationPolicy
- EnforcementLevel
from pulumi_policy import EnforcementLevel
- validateResourceOfType
from pulumi_policy import validateResourceOfType
Quickstart
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)
)
]
)