Cloud Custodian - 并行执行
项目描述
c7n-org: 多账户Custodian执行
% [注释]: # (!!!重要!!!) % [注释]: # (此文件在文档生成过程中已移动。) % [注释]: # (只编辑 ./tools/c7n_org/README.md 中的原始文档)
c7n-org 是一个用于并行运行Custodian针对多个AWS账户、Azure订阅、GCP项目或OCI租户的工具。
安装
pip install c7n-org
c7n-org 有3种运行模式
Usage: c7n-org [OPTIONS] COMMAND [ARGS]...
custodian organization multi-account runner.
Options:
--help Show this message and exit.
Commands:
report report on an AWS cross account policy execution
run run a custodian policy across accounts (AWS, Azure, GCP, OCI)
run-script run a script across AWS accounts
为了运行c7n-org针对多个账户,首先需要创建一个包含账户相关信息的配置文件。
示例AWS配置文件
accounts:
- account_id: '123123123123'
name: account-1
regions:
- us-east-1
- us-west-2
role: arn:aws:iam::123123123123:role/CloudCustodian
vars:
charge_code: xyz
tags:
- type:prod
- division:some division
- partition:us
- scope:pci
...
示例Azure配置文件
subscriptions:
- name: Subscription-1
subscription_id: a1b2c3d4-e5f6-g7h8i9...
- name: Subscription-2
subscription_id: 1z2y3x4w-5v6u-7t8s9r...
示例GCP配置文件
projects:
- name: app-dev
project_id: app-203501
tags:
- label:env:dev
- name: app-prod
project_id: app-1291
tags:
- label:env:dev
示例OCI配置文件
tenancies:
- name: dev-tenancy
profile: DEVTENANCY
regions:
- us-ashburn-1
- us-phoenix-1
vars:
environment: dev
tags:
- type:test
...
配置文件生成
我们还在 脚本
文件夹 中提供生成所需配置文件的脚本。
注意: 目前这些脚本仅通过git分发。根据 https://github.com/cloud-custodian/cloud-custodian/issues/2420,我们计划将它们集成到新的c7n-org子命令中。
- 对于 AWS,脚本
orgaccounts.py
从AWS组织API生成配置文件。
python orgaccounts.py -f accounts.yml
-
对于 Azure,脚本
azuresubs.py
从Azure资源管理API生成配置文件。- 请参阅 附加Azure说明 以获取初始设置和其他重要信息。
python azuresubs.py -f subscriptions.yml
- 对于 GCP,脚本
gcpprojects.py
从GCP资源管理API生成配置文件。
python gcpprojects.py -f projects.yml
-
对于 OCI,脚本
ocitenancies.py
使用OCI配置文件和OCI组织API生成配置文件。- 请参阅 附加OCI说明 以获取更多信息。
python ocitenancies.py -f tenancies.yml
使用c7n-org运行策略
要运行策略,必须传递以下参数
-c | accounts|projects|subscriptions|tenancies config file
-s | output directory
-u | policy
例如
c7n-org run -c accounts.yml -s output -u test.yml --dryrun
运行上述命令后,将创建以下文件夹结构
output
|_ account-1
|_ us-east-1
|_ policy-name
|_ resources.json
|_ custodian-run.log
|_ us-west-2
|_ policy-name
|_ resources.json
|_ custodian-run.log
|- account-2
...
使用 c7n-org report
从输出目录生成csv报告。
选择要执行的账户、区域和策略
您可以通过通过 -a
标志传递账户名称或id来筛选要运行的账户,该标志可以指定多次,或者也可以用逗号分隔的值。
也可以通过指定 -t
标签过滤器来选择执行账户组。账户标签在配置文件中指定。给定上述账户配置文件,您可以使用 -t type:prod
指定所有prod账户。您可以使用 -t
标志多次或使用逗号分隔的列表。
您可以通过指定 -p
或通过标签选择策略组来指定要使用的策略 -l
。两个选项都支持指定多次或使用逗号分隔的值。
默认情况下,在AWS中,c7n-org将在区域之间并行执行。可以多次指定 -r
标志,默认为 (us-east-1, us-west-2)
。特殊值 all
将在所有区域执行。
有关更多信息,请参阅 c7n-org run --help
。
定义和使用变量
配置文件中每个账户/订阅/项目配置也可以定义一个变量部分 vars
,该部分可用于策略的定义,并在执行时进行插值。这些是除了custodian提供的默认运行时变量(如 account_id
、now
和 region
)之外的变量。
c7n-org配置文件中定义示例
accounts:
- account_id: '123123123123'
name: account-1
role: arn:aws:iam::123123123123:role/CloudCustodian
vars:
charge_code: xyz
策略文件中使用示例
policies:
- name: ec2-check-tag
resource: aws.ec2
filters:
- "tag:CostCenter": "{charge_code}"
为 c7n-org run-script
增强的另一项功能是支持在脚本arg中支持几个变量。可用的变量是 account
、account_id
、region
和 output_dir
。
c7n-org run-script -s . -c my-projects.yml gcp_check_{region}.sh
# or
c7n-org run-script -s . -c my-projects.yml use_another_policy_result.sh {output_dir}
注意: 变量插值对正确的引号和间隔敏感,即 { charge_code }
由于额外的空白字符而无效。此外,yaml解析可以转换如 {charge_code}
的值到null,除非它像上述示例那样在字符串中引用。不需要引号将插值值放入其他内容中,即 "my_{charge_code}"。
其他命令
c7n-org 还支持通过运行脚本命令对账户执行任意脚本。对于AWS,在执行之前,将标准AWS SDK凭据信息导出到进程环境。对于Azure和GCP,仅导出环境变量 AZURE_SUBSCRIPTION_ID
和 PROJECT_ID
(除系统环境变量外)。
c7n-org 还支持通过 c7n-org report
子命令生成针对特定策略执行跨账户的报告。默认情况下,account_id 不会暴露在输出中,但您可以在 cli 中使用 --field AccountID=account_id
附加它。
额外的 Azure 指令
如果您使用 Azure Service Principal 来执行 c7n-org,您需要确保该主体有权访问多个订阅。
有关创建服务主体和在订阅之间授予访问权限的说明,请访问 Azure 认证文档页面。
额外的 OCI 指令
脚本 ocitenancies.py
接受一个可选参数 --add-child-tenancies
,该参数将所有与 DEFAULT
配置文件中十租户关联的子租户添加到生成的 c7n-org 配置文件中。如果子租户配置文件在 OCI 配置文件中不可用,则用户可以将子租户配置文件添加到 OCI 配置文件中,并用相应的配置文件名称替换 c7n-org 配置中的 <ADD_PROFILE>
条目,或者用户可以从 c7n-org 配置文件中删除子租户条目。有关配置文件的更多信息,请参阅此 页面。
如果用户想在 c7n-org 中查询特定存储区中的资源,则可以在 vars
部分 oci_compartments
中传递存储区的 OCID,如下所示。如果 oci_comparments
未在 vars
下传递,则将从十租户级别获取资源。
tenancies:
- name: dev-tenancy
profile: DEVTENANCY
regions:
- us-ashburn-1
- us-phoenix-1
vars:
oci_compartments: ocid1.test.oc1..<unique_ID>EXAMPLE-compartmentId-2-Value,ocid1.test.oc1..<unique_ID>EXAMPLE-compartmentId-3-Value
environment: dev
- name: test-tenancy
profile: TESTTENANCY
regions:
- us-ashburn-1
vars:
environment: test
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。