如何使用 AWS CDK 将元数据添加到 IAM 策略?

Lui*_*mes 9 amazon-web-services aws-cloudformation amazon-iam aws-cdk

我需要将一些元数据添加到 Cloudformation 中以获取 IAM 策略。我怎样才能用 CDK 做到这一点?

我正在使用 CDK 合成 cloudformation,并且需要包含元数据来抑制 cfn-nag ( https://github.com/stelligent/cfn_nag ) 警告。

我使用以下语句进行了策略生成:

const cfnCustomPolicy = new iam.CfnPolicy(scope,
    'cfnCustomPolicy', 
    {
        policyName: "CustomPolicy",                
        policyDocument: {
            Version: "2012-10-17",
            Statement: [
                {
                    Effect: "Allow",
                    Action: "apigateway:GET",
                    Resource: [
                        "arn:aws:apigateway:us-east-1::/restapis/*/stages/*/exports/*"
                    ]
                }
            ]
        }
    }
);

cfnCustomPolicy.cfnOptions.metadata = {
    cfn_nag: {
        rules_to_suppress: [
            {
                id: "W12",
                reason: "The lambda need access to export documents from any API"
            }
        ]
    }            
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法使用 CDK 来做到这一点,而不使用 L1 接口?

Yan*_*iao 9

是的,根据文档,这是唯一的方法

https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html

但是,这并不意味着您只能使用 CfnXXX 创建构造,您可以使用 CDK 构造来执行此操作

cfn_policy = self.policy.node.default_child
cfn_policy.cfn_options.metadata = {
        "cfn_nag": {
            "rules_to_suppress": [
                {"id": "W9"},
                {"id": "W2"}
            ]
        }
    }
Run Code Online (Sandbox Code Playgroud)

我尝试过 node.add_metadata 但显然它只添加内部 cdk 元数据