Cloud Custodian
Cloud Custodian (c7n) is an open-source, cloud-native rules engine for managing public cloud accounts and resources. It enables users to define policies in simple YAML files to ensure well-managed, secure, and cost-optimized cloud infrastructure. It supports major cloud providers like AWS, Azure, and GCP, and can execute policies in real-time (via serverless functions) or periodically (via scheduled jobs). The current version is 0.9.50, and it maintains an active release cadence with frequent updates and feature additions.
Warnings
- gotcha Forgetting to install cloud-specific packages (e.g., `c7n_azure`, `c7n_gcp`) will lead to errors when trying to run policies for those providers, as the necessary resource modules will be missing.
- gotcha Always use the `--dryrun` flag when developing or testing new policies, especially those with `actions`. This prevents unintended modifications or deletions of cloud resources by showing what actions *would* be taken.
- gotcha Cloud Custodian policies are written in YAML. Incorrect YAML syntax (e.g., indentation errors, missing `policies:` root key) is a common cause of `PolicyValidationError` or `YAMLError` during execution.
- breaking Cloud Custodian currently requires Python >=3.10.2 and <4.0.0. Using older Python versions will result in installation failures or runtime errors due to dropped support.
- gotcha Proper cloud provider credentials must be configured in the execution environment (e.g., AWS CLI configuration, environment variables like `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_REGION` for AWS). Without them, policies cannot interact with cloud APIs.
- gotcha For managing policies across multiple cloud accounts, subscriptions, or projects in parallel, the `c7n-org` tool is necessary. Running `custodian` directly will only target the configured account/region.
- gotcha When deploying real-time policies using 'mode' (e.g., `cloudtrail`, `periodic`), Cloud Custodian automatically provisions serverless functions (like AWS Lambda). Incorrect or insufficient IAM permissions for the Custodian execution role can lead to deployment failures or policy execution errors within the serverless environment.
Install
-
pip install c7n -
pip install c7n_azure -
pip install c7n_gcp -
pip install c7n_oci
Imports
- Policy
from c7n.policy import Policy
Quickstart
# policy.yml
policies:
- name: find-unencrypted-s3-buckets
resource: aws.s3
filters:
- type: unencrypted
actions:
# Remove or comment out 'actions' for a pure dry-run without notification setup
- type: notify
violation_messages:
- "S3 bucket {resource_id} in {account_id} is not encrypted!"
to:
- "{{ resource_owner_email | default(owner@example.com) }}"
transport:
type: sqs # Requires c7n-mailer and an SQS queue named 'cloud-custodian-notifications'
queue: cloud-custodian-notifications
# To run the policy (ensure AWS credentials are configured):
custodian run --dryrun -s . policy.yml