无法通过 CDK 部署配置规则

Can't deploy a config rule through the CDK

我正在通过利用 Python CDK for AWS 在我的组织中应用配置规则。在我的设置中,我有一个来自 here 的托管配置规则列表。此列表位于将通过堆栈集进一步部署的堆栈中。我对一致性包中的一些配置规则有疑问。出于某种原因,cloudformation 不接受 SourceIdentifier: AWS_CONFIG_PROCESS_CHECK

from aws_cdk import (
    core as cdk,
    aws_config as config,
)
....
class TestConfigRulesStack(cdk.Stack):
    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        self.rule = config.ManagedRule(
          self,
          'ManagedRule-test',
          config_rule_name='account-contact-details-configured',
          identifier='AWS_CONFIG_PROCESS_CHECK',
        )
....

错误:

The sourceIdentifier AWS_CONFIG_PROCESS_CHECK is invalid. Please refer to the documentation for a list of valid sourceIdentifiers that can be used when AWS is the Owner. (Service: AmazonConfig; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: <request_id>; Proxy: null)

CDK 输出:

...
        "JSIIMetaManagedRuleaccountcontactdetailsconfigured9BA14D66": {
            "Type": "AWS::Config::ConfigRule",
            "Properties": {
                "Source": {
                    "Owner": "AWS",
                    "SourceIdentifier": "AWS_CONFIG_PROCESS_CHECK"
                },
                "ConfigRuleName": "account-contact-details-configured",
                "Description": "Ensure the contact email and telephone number for AWS accounts are current and map to more than one individual in your organization. Within the My Account section of the console ensure correct information is specified in the Contact Information section."
            },
            "Metadata": {
                "aws:cdk:path": "<path>"
            }
        },
    ...

这是来自官方模板的配置规则:

  AccountContactDetailsConfigured:
    Properties:
      ConfigRuleName: account-contact-details-configured
      Description: Ensure the contact email and telephone number for AWS accounts are current and map to more than one individual in your organization. Within the My Account section of the console ensure correct information is specified in the Contact Information section.
      Source:
        Owner: AWS
        SourceIdentifier: AWS_CONFIG_PROCESS_CHECK
    Type: AWS::Config::ConfigRule

我错过了什么吗?我不明白为什么它不起作用。

您正在使用与现有 AWS 拥有的 ManagedRule 相同的标识符创建新规则。

要导入现有规则而不是创建新规则,请使用 ManagedRule.fromConfigRuleName:

rule = config.ManagedRule.from_config_rule_name(
    self,
    'ManagedRule-test',
    config_rule_name='account-contact-details-configured'
)

要使用默认的 AWS 托管规则,您只需使用位于 aws_cdk.aws_config.ManagedRuleIdentifiers 中的常量即可附加该规则。您不需要使用 from_config 或任何其他功能导入规则。

只要您使用规则作为 iRule 一部分的结构,您就可以使用 aws_cdk.aws_config.ManagedRuleIdentifiers.THE_NAME_OF_THE_RULE

或者,基于您的进口

config.ManagedRuleIdentifiers.THE_NAME_OF_THE_RULE

您可以在此处获取 AWS 托管规则列表及其常量值:

https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_config/ManagedRuleIdentifiers.html

如果它不是 AWS 托管规则,并且您已经通过另一种方法(另一个堆栈,手动,另一个 cloudformation 模板)创建了它,那么您需要使用 from_ 功能将它导入到这个堆栈中。

如果它是在另一个 CDK 堆栈中创建的,您可以从该堆栈中公开它并将其用作您需要它的任何其他堆栈中的参数,但我建议您将它们全部设为 nestedStacks 并将它们放在一个公共目录下应用程序,因此您没有部署依赖性问题