Introduction to Cloud Custodian
Relevant source files
The following files were used as context for generating this wiki page:
Cloud Custodian (c7n) is an open-source rules engine for managing public cloud accounts and resources. It uses YAML policies to enable well-managed cloud infrastructure that is secure, compliant, and cost-optimized. The tool consolidates ad-hoc scripts into a lightweight engine with unified metrics and reporting.
Custodian manages AWS, Azure, and GCP environments by ensuring real-time compliance to security policies, tag requirements, and cost management through garbage collection. It integrates with cloud-native serverless capabilities for real-time enforcement or runs as a cron job against existing resource fleets.
Sources: README.md:14-25
Core Architecture and Policy Structure
Cloud Custodian operates by executing policies against resource types. A policy consists of a resource type, filters to narrow the target set, actions to perform on matched resources, and an execution mode.
Policy Components
| Component | Description |
|---|---|
| Resource | Specifies the cloud resource type (e.g., aws.s3, azure.vm, gcp.instance). |
| Filters | Provides a vocabulary to select specific resources based on attributes, tags, or boolean conditions. |
| Actions | Defines the operations to perform on filtered resources (e.g., terminate, tag, stop). |
| Mode | Determines how the policy executes (e.g., cloudtrail for event-based or serverless triggers). |
Sources: README.md:37-43, README.md:120-140
Policy Execution Logic
The following diagram illustrates the flow from policy definition to cloud provider execution.
This diagram shows the sequence where a YAML policy is validated and loaded into the Resource Manager, which applies filters and actions via the Cloud Provider APIs.
Sources: tools/c7n_sphinxext/c7n_sphinxext/docgen.py:30-100
Multi-Cloud and Resource Providers
Cloud Custodian supports multiple cloud providers through a plugin architecture. Each provider implements a specific set of resources, filters, and actions.
- AWS Provider: Comprehensive support for Amazon Web Services resources.
- Azure Provider: Plugin-based support for Microsoft Azure (e.g.,
azure.vm). - GCP Provider: Support for Google Cloud Platform using Google Cloud SDK and API clients.
- Others: Includes OpenStack, Tencent Cloud, Oracle Cloud (OCI), and Kubernetes.
Sources: GOVERNANCE.md:7-15, tools/c7n_azure/readme.md:1-5, tools/c7n_gcp/pyproject.toml:10-18
Shift-Left and Infrastructure as Code (IaC)
Cloud Custodian supports "shifting left" by evaluating policies against Infrastructure as Code assets before deployment. This prevents non-compliant resources from reaching the production environment.
c7n-left
The c7n-left tool evaluates policies directly against IaC source assets like Terraform. It provides a CLI optimized for developer workstations and CI pipelines.
- Taggable Filter: Automatically identifies resources that support tagging across AWS, Azure, GCP, OCI, and Tencent Cloud.
- Traverse Filter: Enables multi-hop graph traversal to check related resources (e.g., checking an EC2 instance's Security Group attributes).
- Data Resource Support: Validates Terraform data resources (prefixed with
data.).
Sources: tools/c7n_left/README.md:1-15, tools/c7n_left/README.md:130-145, tools/c7n_left/README.md:160-170
Policy Evaluation in Pipelines
This diagram depicts the automated evaluation process within a CI/CD pipeline using c7n-left.
Sources: tools/c7n_left/README.md:50-85, tools/ops/azure/container-host/devops-flow/README.md:60-75
Development and Testing Patterns
Flight Data Recording (GCP Example)
The GCP provider uses a "flight data" pattern for testing. This allows tests to run without active cloud credentials by replaying recorded HTTP request/response cycles.
- Recording Mode: Real API calls are made and stored as JSON files when
C7N_FUNCTIONAL=yesis set. - Playback Mode: Tests use recorded files located in
tools/c7n_gcp/tests/data/flights/. - Terraform Integration: The
@terraformdecorator provisions infrastructure for recording sessions and destroys it afterward.
Sources: tools/c7n_gcp/development.md:120-160
Automated Documentation
The project includes tools to automate policy documentation:
- c7n-autodoc: A sandbox utility that crawls policy directories, groups them by resource or category (e.g., Security vs. Cost), and generates HTML reports or uploads them to S3.
- c7n-sphinxext: A Sphinx extension that generates documentation directly from the Custodian schema, ensuring technical docs stay synchronized with the code.
Sources: tools/sandbox/c7n_autodoc/c7n-autodoc.py:15-50, tools/c7n_sphinxext/c7n_sphinxext/docgen.py:200-220
Project Governance
Cloud Custodian is a CNCF Incubating project managed by a community of contributors.
- Core Maintainers: Oversee the entire project, coordinate releases, and manage security.
- Area Maintainers: Own specific areas like the AWS Provider or Multi-cloud Management (
c7n-org). - Decision-Making: Follows a consensus-based model among Area Maintainers, with escalation to Core Maintainers or the CNCF TOC if necessary.
Sources: GOVERNANCE.md:20-45, README.md:27-29
Summary
Cloud Custodian provides a unified, YAML-based vocabulary for cloud governance. By abstracting cloud provider complexities into a set of filters and actions, it allows organizations to manage security and costs consistently across AWS, Azure, and GCP. Its ability to run both as a reactive engine in the cloud and as a preventive tool in IaC pipelines makes it a comprehensive solution for modern cloud infrastructure management.