返回更新列表
新发布Sep 5, 2026

cloud-custodian v0.9.52.0

用于云安全、成本优化和治理的规则引擎,使用YAML DSL编写策略,以查询、过滤并对资源采取行动。

分享

Cloud Custodian (c7n)

Cloud Custodian Logo


slack CI CII Best Practices

Cloud Custodian,也称为 c7n,是一个用于管理公有云账户和资源的规则引擎。它允许用户定义策略,以实现既安全又成本优化的良好管理的云基础设施。它将组织拥有的许多临时脚本整合为一个轻量且灵活的工具,并具有统一的指标和报告功能。

Custodian 可用于管理 AWS、Azure 和 GCP 环境,通过确保对安全策略(如加密和访问要求)、标签策略的实时合规,以及通过清理未使用资源和非工作时间资源管理来实现成本管理。

Custodian 还支持对基础设施即代码资产运行策略,从而直接在开发人员工作站或 CI 流水线中提供反馈。

Custodian 策略以简单的 YAML 配置文件编写,使用户能够针对资源类型(EC2、ASG、Redshift、CosmosDB、PubSub Topic)指定策略,并由过滤器和操作的词汇表构建而成。

它与各提供商的原生无服务器能力集成,通过内置的预置功能实现策略的实时执行。或者,它也可以作为服务器上的简单 cron 作业运行,针对现有的大型资源群执行。

Cloud Custodian 是一个 CNCF 孵化项目,由数百名贡献者组成的社区领导。

功能

  • 全面支持公有云服务和资源,并拥有丰富的操作和过滤器库,可用于构建策略。
  • 对基础设施即代码(terraform 等)资产运行策略。
  • 支持对资源进行任意过滤,并支持嵌套的布尔条件。
  • 对任何策略进行试运行,以查看其效果。
  • 自动预置无服务器函数和事件源(AWS CloudWatchEvents、AWS Config Rules、Azure EventGrid、GCP AuditLog 和 Pub/Sub 等)
  • 在匹配策略的资源上输出云提供商原生指标
  • 将匹配策略的资源以结构化输出写入云原生对象存储。
  • 智能缓存使用,以最小化 API 调用。
  • 支持多账户/多订阅/多项目使用。
  • 久经实战考验——已在一些非常大型的云环境中投入生产使用。

链接

快速安装

Custodian 以带有 c7n 前缀的一系列包的形式发布在 pypi 上,同时也提供 docker 镜像。

$ python3 -m venv custodian
$ source custodian/bin/activate
(custodian) $ pip install c7n

用法

使用 Cloud Custodian (c7n) 的第一步是编写一个 YAML 文件,其中包含你想要运行的策略。每个策略指定该策略将运行的资源类型、一组控制哪些资源将受此策略影响的过滤器、策略将对匹配资源执行的操作,以及控制策略如何执行的模式。

最好的入门指南是各云服务商专属的教程。

作为快速导览,以下是一些针对 AWS 资源的示例策略。

  1. 将强制要求没有任何 S3 存储桶启用跨账户访问。
  2. 将终止任何新启动的、没有加密 EBS 卷的 EC2 实例。
  3. 将为任何不具有以下标签 "Environment"、"AppId",以及 "OwnerContact" 或 "DeptID" 之一的 EC2 实例打上标签, 使其在四天后被停止。
policies:
 - name: s3-cross-account
   description: |
     Checks S3 for buckets with cross-account access and
     removes the cross-account access.
   resource: aws.s3
   region: us-east-1
   filters:
     - type: cross-account
   actions:
     - type: remove-statements
       statement_ids: matched

 - name: ec2-require-non-public-and-encrypted-volumes
   resource: aws.ec2
   description: |
    Provision a lambda and cloud watch event target
    that looks at all new instances and terminates those with
    unencrypted volumes.
   mode:
    type: cloudtrail
    role: CloudCustodian-QuickStart
    events:
      - RunInstances
   filters:
    - type: ebs
      key: Encrypted
      value: false
   actions:
    - terminate

 - name: tag-compliance
   resource: aws.ec2
   description: |
     Schedule a resource that does not meet tag compliance policies to be stopped in four days. Note a separate policy using the`marked-for-op` filter is required to actually stop the instances after four days.
   filters:
    - State.Name: running
    - "tag:Environment": absent
    - "tag:AppId": absent
    - or:
      - "tag:OwnerContact": absent
      - "tag:DeptID": absent
   actions:
    - type: mark-for-op
      op: stop
      days: 4

你可以使用以下命令,通过示例策略来验证、测试并运行 Cloud Custodian:

# Validate the configuration (note this happens by default on run)
$ custodian validate policy.yml

# Dryrun on the policies (no actions executed) to see what resources
# match each policy.
$ custodian run --dryrun -s out policy.yml

# Run the policy
$ custodian run -s out policy.yml

你可以通过 Docker 运行 Cloud Custodian:

# Download the image
$ docker pull cloudcustodian/c7n
$ mkdir output

# Run the policy
#
# This will run the policy using only the environment variables for authentication
$ docker run -it \
  -v $(pwd)/output:/home/custodian/output \
  -v $(pwd)/policy.yml:/home/custodian/policy.yml \
  --env-file <(env | grep "^AWS\|^AZURE\|^GOOGLE") \
  cloudcustodian/c7n run -v -s /home/custodian/output /home/custodian/policy.yml

# Run the policy (using AWS's generated credentials from STS)
#
# NOTE: We mount the ``.aws/credentials`` and ``.aws/config`` directories to
# the docker container to support authentication to AWS using the same credentials
# credentials that are available to the local user if authenticating with STS.

$ docker run -it \
  -v $(pwd)/output:/home/custodian/output \
  -v $(pwd)/policy.yml:/home/custodian/policy.yml \
  -v $(cd ~ && pwd)/.aws/credentials:/home/custodian/.aws/credentials \
  -v $(cd ~ && pwd)/.aws/config:/home/custodian/.aws/config \
  --env-file <(env | grep "^AWS") \
  cloudcustodian/c7n run -v -s /home/custodian/output /home/custodian/policy.yml

custodian cask 工具是一个 Go 二进制程序,它为 docker 提供了一个透明的前端,其行为与常规的 custodian CLI 一致,但会自动处理卷挂载。

请查阅文档以获取更多信息,或在 gitter 上联系我们。

云服务商专属帮助

有关 AWS、Azure 和 GCP 的具体说明,请访问相应的入门页面。

参与其中

  • GitHub -(本页面)
  • Slack - 实时聊天,如果你需要帮助或有兴趣为 Custodian 做贡献!
    • Gitter -(较旧的实时聊天,我们可能正在迁移离开此平台)
  • Linen.dev - 在 Linen 上关注我们的讨论
  • 邮件列表 - 我们的项目邮件列表,在此订阅以获取重要的项目公告,欢迎提问
  • Reddit - 我们的 subreddit
  • StackOverflow - 面向开发者的问答网站,我们会关注 cloudcustodian 标签
  • YouTube 频道 - 我们正在添加教程和其他有用信息,以及会议视频

社区资源

我们定期举行社区会议,对所有技能水平的用户和开发者开放。 加入邮件列表将自动向你发送会议邀请。 有关加入会议的更多技术信息,请参阅下面的说明。

其他工具

Custodian 项目还在此处开发和维护了一套附加工具 https://github.com/cloud-custodian/cloud-custodian/tree/main/tools:

  • Org: 多账户策略执行。

  • ShiftLeft: 左移 ~ 针对基础设施即代码资产(如 terraform)运行策略。

  • PolicyStream: 将 Git 历史作为逻辑策略变更流。

  • Salactus: 横向扩展 s3 扫描。

分类