Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
创建 Amazon Config 自定义策略规则
您可以通过、Amazon Web Services 管理控制台Amazon CLI 或 Amazon Config API 创建 Amazon Config 自定义策略规则。
添加 Amazon Config 自定义策略规则
- Using the console
-
登录到,Amazon Web Services 管理控制台然后通过以下网址打开 Amazon Config 控制台:https://console.aws.amazon.com/config/home。
-
在 Amazon Web Services 管理控制台 菜单上,验证区域选择器是否设置为支持 Amazon Config 规则的 Amazon 区域。有关支持的区域的列表,请参阅《Amazon Web Services 一般参考》中的 Amazon Config 区域和终端节点。
-
在左侧导航窗格中,选择 Rules。
-
在 Rules 页面,选择 Add rule。
-
在指定规则类型页面上,选择使用 Guard 创建自定义规则。
-
在配置规则页面上,通过完成以下步骤创建规则:
-
对于规则名称,键入一个唯一的规则名称。
-
对于描述,键入规则的描述。
-
对于 Guard 运行时版本,为您的 Amazon Config 自定义策略规则选择运行时系统。
-
对于规则内容,可以为其填充规则的 Guard 自定义策略。
-
对于评估模式,请选择在资源创建和管理过程中 Amazon Config 何时评估您的资源。根据此规则,Amazon Config 可以在预置资源之前、预置资源之后或同时在这两个阶段评估资源配置。
-
选择开启主动评估,以允许您在部署资源之前对资源的配置设置进行评估。
开启主动评估后,您可以使用 StartResourceEvaluation API 和 GetResourceEvaluationSummary API 来检查您在这些命令中指定的资源是否会被您所在区域的账户中的主动规则标记为 NON_COMPLIANT。
有关使用此命令的更多信息,请参阅使用 Amazon Config 规则评估您的资源。有关支持主动评估的托管规则列表,请参阅按评估模式列出的 Amazon Config 托管规则列表。
-
选择开启侦查评估以评估您的现有资源的配置设置。
对于侦查评估,Amazon Config 自定义策略规则由配置更改启动。将预先选择以下选项。
-
资源 – 在创建、更改或删除与指定资源类型(或类型和标识符)匹配的资源时。
-
标签 – 在创建、更改或删除包含指定标签的资源时。
-
所有更改 – 在创建、更改或删除 Amazon Config 记录的资源时。
Amazon Config 在检测到与规则的范围匹配的资源发生更改时运行评估。您可以使用范围来限制哪些资源启动评估。否则,当配置后的资源发生变化时,就会启动评估。
-
对于参数,如果您的规则包含参数,可以自定义所提供的键的值。参数是资源在被视为符合规则之前必须遵守的属性。
-
在审核和创建页面上,查看您的所有选择,然后再将规则添加到您的。Amazon Web Services 账户
-
查看完规则后,选择添加规则。
- Using the Amazon CLI
使用 put-config-rule 命令。
Owner 字段应为。CUSTOM_POLICYAmazon Config 自定义策略规则需要以下附加字段:
-
Runtime:Amazon Config 自定义策略规则的运行时系统。
-
PolicyText:包含 Amazon Config 自定义策略规则逻辑的策略定义。
-
EnableDebugLogDelivery:为 Amazon Config 自定义策略规则启用调试日志记录的布尔表达式。默认值为。false
- Using the API Reference
-
使用 PutConfigRule 操作。
Owner 字段应为。CUSTOM_POLICYAmazon Config 自定义策略规则需要以下附加字段:
-
Runtime:Amazon Config 自定义策略规则的运行时系统。
-
PolicyText:包含 Amazon Config 自定义策略规则逻辑的策略定义。
-
EnableDebugLogDelivery:为 Amazon Config 自定义策略规则启用调试日志记录的布尔表达式。默认值为。false
为 Amazon Config 自定义策略规则编写规则内容
通过 Amazon Config 自定义策略规则,您可以使用 Amazon CloudFormation Guard 的域特定语言(DSL)来评估资源配置。本主题提供了编写自定义策略规则的模式和最佳实践。
有关如何使用 Guard 编写规则的更多信息,请参阅《Amazon CloudFormation Guard 用户指南》中的编写 Guard 规则,以及 Guard GitHub 资源库中的 Amazon CloudFormation Guard 2.0's Modes of Operation。
基本规则结构
使用以下基本格式创建规则:
# Basic rule format
rule <rule_name> when
resourceType == "<AWS::Service::Resource>" {
# Evaluation clauses
}
# Example with filtering
let resources_of_type = Resources.*[ Type == 'AWS::Service::Resource' ]
rule check_resources when %resources_of_type !empty {
%resources_of_type.configuration.property == expected_value
}
关键组件
- configuration
-
包含资源配置的内容。
- supplementaryConfiguration
-
包含资源配置的其他内容。Amazon Config 针对某些资源类型返回此字段,用于补充针对配置字段返回的信息。
- resourceType
-
正在评估的 Amazon 资源类型。
- resourceId
-
资源的 ID(例如,sg-xxxxxx)。
- accountId
-
与资源关联的 12 位数字 Amazon Web Services 账户 ID。
常见模式
- Status checks
let allowed_status = ['ACTIVE', 'RUNNING']
rule check_resource_status when
resourceType == "AWS::Service::Resource" {
configuration.status IN %allowed_status
}
- Required properties
rule check_required_properties when
resourceType == "AWS::Service::Resource" {
configuration.propertyName exists
configuration.propertyName is_string # or is_list, is_struct
}
- Query blocks
configuration.Properties {
property1 exists
property2 is_string
property3 IN [allowed_value1, allowed_value2]
}
- Conditional evaluation
when configuration.feature_enabled == true {
configuration.feature_settings exists
configuration.feature_settings is_struct
}
- Custom messages
rule check_compliance when
resourceType == "AWS::Service::Resource" {
configuration.property == expected_value <<Custom error message explaining the requirement>>
}}
高级功能
- Range checks
rule check_numeric_limits {
# Inclusive range (lower_limit <= value <= upper_limit)
configuration.value IN r[minimum_value, maximum_value]
# Exclusive range (lower_limit < value < upper_limit)
configuration.value IN r(exclusive_min, exclusive_max)
# Left inclusive, right exclusive (lower_limit <= value < upper_limit)
configuration.value IN r[minimum_value, exclusive_max)
# Left exclusive, right inclusive (lower_limit < value <= upper_limit)
configuration.value IN r(exclusive_min, maximum_value]
}
- Combining conditions
# AND conditions (implicit through new lines)
condition_1
condition_2
# OR conditions (explicit)
condition_3 OR
condition_4
- Chaining rules
rule check_prerequisites {
configuration.required_setting exists
}
rule check_details when check_prerequisites {
configuration.required_setting == expected_value
}
最佳实践
示例:dynamodb-pitr-enabled
以下示例显示了 Amazon Config 托管规则 的 Amazon Config 自定义策略规则版本的策略定义。此规则检查 DynamoDB 表是否启用了时间点恢复。
# Check if DynamoDB tables have Point-in-Time Recovery enabled
let status = ['ACTIVE']
rule tableisactive when
resourceType == "AWS::DynamoDB::Table" {
configuration.tableStatus == %status
}
rule checkcompliance when
resourceType == "AWS::DynamoDB::Table"
tableisactive {
let pitr = supplementaryConfiguration.ContinuousBackupsDescription.pointInTimeRecoveryDescription.pointInTimeRecoveryStatus
%pitr == "ENABLED" <<DynamoDB tables must have Point-in-Time Recovery enabled>>
}