

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 更新 AWS Config 規則
<a name="evaluate-config_update-rules"></a>

您可以使用 AWS Config 主控台或 AWS SDKs來更新您的規則。

**Topics**
+ [使用主控台](#evaluate-config_update-rules-console)
+ [使用 AWS SDKs](#evaluate-config_update-rules-cli)

## 更新規則 （主控台）
<a name="evaluate-config_update-rules-console"></a>

**規則** 頁面會在表格中顯示您的規則和其目前的合規結果。每個規則的結果都是**評估...**，直到 AWS Config 完成針對規則評估您的資源為止。您可以使用重新整理按鈕來更新結果。評估 AWS Config 完成後，您可以看到合規或不合規的規則和資源類型。如需詳細資訊，請參閱[使用 檢視 AWS 資源的合規資訊和評估結果 AWS Config](evaluate-config_view-compliance.md)。

### 更新規則
<a name="update-rules-console"></a>

**更新規則**

1. 登入 AWS 管理主控台 ，並在 https：//[https://console.aws.amazon.com/config/home](https://console.aws.amazon.com/config/home) 開啟 AWS Config 主控台。

1. 在 AWS 管理主控台 選單中，確認區域選擇器已設定為支援 AWS Config 規則的區域。如需支援區域的清單，請參閱《*Amazon Web Services 一般參考*》中的 [AWS Config 區域與端點](https://docs.aws.amazon.com/general/latest/gr/rande.html#awsconfig_region)。

1. 在左側導覽中，選擇 **規則**。

1. 依序為要更新的規則選擇一項規則及 **編輯規則**。

1. 修改 **編輯規則** 頁面上的設定，以視需要變更規則。

1. 選擇**儲存**。

## 更新規則AWS SDKs)
<a name="evaluate-config_update-rules-cli"></a>

### 更新規則
<a name="update-rules-cli"></a>

若您要更新先前新增的規則，您可以在您用於此請求的 `ConfigRule` 資料類型中，透過 `ConfigRuleName`、`ConfigRuleId` 或 `ConfigRuleArn` 來指定規則。您可以使用與新增規則時相同的`PutConfigRule`命令。

下列程式碼範例示範如何使用 `PutConfigRule`。

------
#### [ CLI ]

**AWS CLI**  
**新增 AWS 受管 Config 規則**  
下列命令提供 JSON 程式碼來新增 AWS 受管 Config 規則：  

```
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` 屬性會設為 `AWS`，而 `SourceIdentifier` 屬性則設為規則識別碼 `REQUIRED_TAGS`。針對 `InputParameters` 屬性，會指定規則需要的標籤索引鍵 `CostCenter` 和 `Owner`。  
如果命令成功， AWS Config 不會傳回任何輸出。若要驗證規則組態，請執行 describe-config-rules 命令，並指定規則名稱。  
**新增客戶管理的 Config 規則**  
下列命令提供 JSON 程式碼，以新增客戶管理的 Config 規則：  

```
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_LAMBDA`，而`SourceIdentifier`屬性設定為 AWS Lambda 函數的 ARN。`SourceDetails` 物件為必要項目。當 Config 叫用屬性來根據規則評估資源時，為 `InputParameters` 屬性指定的參數會傳遞至 AWS Lambda AWS 函數。  
如果命令成功， AWS Config 不會傳回任何輸出。若要驗證規則組態，請執行 describe-config-rules 命令，並指定規則名稱。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [PutConfigRule](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/configservice/put-config-rule.html)。

------
#### [ Python ]

**適用於 Python 的 SDK (Boto3)**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/example_code/config#code-examples)中設定和執行。

```
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 詳細資訊，請參閱《*適用於 Python (Boto3) 的AWS 開發套件 API 參考*》中的 [PutConfigRule](https://docs.aws.amazon.com/goto/boto3/config-2014-11-12/PutConfigRule)。

------
#### [ SAP ABAP ]

**適用於 SAP ABAP 的開發套件**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/sap-abap/services/cfs#code-examples)中設定和執行。

```
    " Create a config rule for S3 bucket public read prohibition
    lo_cfs->putconfigrule(
      io_configrule = NEW /aws1/cl_cfsconfigrule(
        iv_configrulename = iv_rule_name
        iv_description = |S3 Public Read Prohibited Bucket Rule|
        io_scope = NEW /aws1/cl_cfsscope(
          it_complianceresourcetypes = VALUE /aws1/cl_cfscplncresrctypes_w=>tt_complianceresourcetypes(
            ( NEW /aws1/cl_cfscplncresrctypes_w( |AWS::S3::Bucket| ) )
          )
        )
        io_source = NEW /aws1/cl_cfssource(
          iv_owner = |AWS|
          iv_sourceidentifier = |S3_BUCKET_PUBLIC_READ_PROHIBITED|
        )
        iv_inputparameters = '{}'
        iv_configrulestate = |ACTIVE|
      )
    ).
    MESSAGE 'Created AWS Config rule.' TYPE 'I'.
```
+  如需 API 詳細資訊，請參閱《適用於 *AWS SAP ABAP 的 SDK API 參考*》中的 [PutConfigRule](https://docs.aws.amazon.com/sdk-for-sap-abap/v1/api/latest/index.html)。

------