与 AWS SDK或PutConfigRule一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或PutConfigRule一起使用 CLI

以下代码示例演示如何使用 PutConfigRule

CLI
AWS CLI

添加 AWS 托管 Config 规则

以下命令提供了用于添加 AWS 托管 Config 规则的JSON代码:

aws configservice put-config-rule --config-rule file://RequiredTagsForEC2Instances.json

RequiredTagsForEC2Instances.json是一个包含规则配置的JSON文件:

{ "ConfigRuleName": "RequiredTagsForEC2Instances", "Description": "Checks whether the CostCenter and Owner tags are applied to EC2 instances.", "Scope": { "ComplianceResourceTypes": [ "AWS::EC2::Instance" ] }, "Source": { "Owner": "AWS", "SourceIdentifier": "REQUIRED_TAGS" }, "InputParameters": "{\"tag1Key\":\"CostCenter\",\"tag2Key\":\"Owner\"}" }

对于该ComplianceResourceTypes属性,此JSON代码将范围限制为该AWS::EC2::Instance类型的资源,因此 AWS Config 将仅根据规则评估EC2实例。由于该规则是托管规则,因此 Owner 属性设置为 AWSSourceIdentifier 属性设置为规则标识符 REQUIRED_TAGS。对于 InputParameters 属性,指定了规则所需的标签键 CostCenterOwner

如果命令成功, AWS Config 将不返回任何输出。要验证规则配置,请运行 describe-config-rules命令并指定规则名称。

添加客户托管的 Config 规则

以下命令提供了用于添加客户托管的 Config 规则的JSON代码:

aws configservice put-config-rule --config-rule file://InstanceTypesAreT2micro.json

InstanceTypesAreT2micro.json是一个包含规则配置的JSON文件:

{ "ConfigRuleName": "InstanceTypesAreT2micro", "Description": "Evaluates whether EC2 instances are the t2.micro type.", "Scope": { "ComplianceResourceTypes": [ "AWS::EC2::Instance" ] }, "Source": { "Owner": "CUSTOM_LAMBDA", "SourceIdentifier": "arn:aws:lambda:us-east-1:123456789012:function:InstanceTypeCheck", "SourceDetails": [ { "EventSource": "aws.config", "MessageType": "ConfigurationItemChangeNotification" } ] }, "InputParameters": "{\"desiredInstanceType\":\"t2.micro\"}" }

对于该ComplianceResourceTypes属性,此JSON代码将范围限制为该AWS::EC2::Instance类型的资源,因此 AWS Config 将仅根据规则评估EC2实例。由于此规则是客户托管规则,因此该Owner属性设置为CUSTOM_LAMBDASourceIdentifier属性设置为 AWS Lambda 函数的。ARNSourceDetails 对象为必填项。当 Confi AWS g 调用 AWS Lambda 函数来根据规则评估资源时,为该InputParameters属性指定的参数将传递给 Lambda 函数。

如果命令成功, AWS Config 将不返回任何输出。要验证规则配置,请运行 describe-config-rules命令并指定规则名称。

Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class ConfigWrapper: """ Encapsulates AWS Config functions. """ def __init__(self, config_client): """ :param config_client: A Boto3 AWS Config client. """ self.config_client = config_client def put_config_rule(self, rule_name): """ Sets a configuration rule that prohibits making Amazon S3 buckets publicly readable. :param rule_name: The name to give the rule. """ try: self.config_client.put_config_rule( ConfigRule={ "ConfigRuleName": rule_name, "Description": "S3 Public Read Prohibited Bucket Rule", "Scope": { "ComplianceResourceTypes": [ "AWS::S3::Bucket", ], }, "Source": { "Owner": "AWS", "SourceIdentifier": "S3_BUCKET_PUBLIC_READ_PROHIBITED", }, "InputParameters": "{}", "ConfigRuleState": "ACTIVE", } ) logger.info("Created configuration rule %s.", rule_name) except ClientError: logger.exception("Couldn't create configuration rule %s.", rule_name) raise
  • 有关API详细信息,请参阅PutConfigRule中的 AWS SDKPython (Boto3) API 参考。