

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# AWS Config カスタムルール
<a name="evaluate-config_develop-rules"></a>

AWS Config カスタムルールは、ゼロから作成するルールです。 AWS Config カスタムルールを作成するには、Lambda 関数 ([AWS Lambda デベロッパーガイド](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-concepts.html#gettingstarted-concepts-function)) とpolicy-as-code 言語である Guard ([Guard GitHub リポジトリ](https://github.com/aws-cloudformation/cloudformation-guard)) の 2 つの方法があります。

AWS Config Lambda で作成されたカスタムルールは*AWS Config カスタム Lambda ルール*と呼ばれ、Guard で作成された AWS Config カスタムルールは*AWS Config カスタムポリシールール*と呼ばれます。

カスタムルールを使用する前に、「[考慮事項](evaluate-config.md#evaluate-config-considerations)」を参照してください。

## AWS Config カスタムポリシールール
<a name="evaluate-config_develop-rules-policy"></a>

Guard を使用して記述されたルールは、 AWS Config コンソールまたは AWS Config ルール APIs を使用して作成できます。 AWS Config カスタムポリシールールを使用すると、Java または Python を使用して Lambda 関数を開発し、カスタム AWS Config ルールを管理することなく、カスタムルールを作成できます。 AWS Config カスタムポリシールールは、設定の変更によって開始されます。詳細については、「[Guard GitHub Repository](https://github.com/aws-cloudformation/cloudformation-guard)」を参照してください。

## AWS Config カスタム Lambda ルール
<a name="evaluate-config_develop-rules-lambda"></a>

カスタム Lambda ルールには、Java または Python を使用して AWS Config カスタムルールの Lambda 関数を作成するオプションが用意されています。* Lambda 関数*は、アップロード先のカスタムコードであり AWS Lambda、イベントソースによって発行されるイベントによって呼び出されます。Lambda 関数が AWS Config ルールに関連付けられている場合、 AWS Config はルールの開始時にその関数を呼び出します。次に、Lambda 関数は から送信された設定情報を評価し AWS Config、評価結果を返します。Lambda 関数の詳細については、「*AWS Lambda デベロッパーガイド*」の「[関数とイベントソース](https://docs.aws.amazon.com/lambda/latest/dg/intro-core-components.html)」を参照してください。

## AWS Config カスタムルールの形式の違い
<a name="evaluate-config_develop-schema"></a>

次の表は、[ConfigurationItem](https://docs.aws.amazon.com/config/latest/APIReference/API_ConfigurationItem.html) データ型のフィールドと AWS Config カスタムルールのフィールドの形式の違いを示しています。


| ConfigurationItem | AWS Config カスタムルール | 
| --- | --- | 
| version | configurationItemVersion | 
| accountId | awsAccountId | 
| arn | ARN | 
| configurationItemMD5Hash | configurationStateMd5Hash | 

**Topics**
+ [カスタムポリシールール](#evaluate-config_develop-rules-policy)
+ [カスタム Lambda ルール](#evaluate-config_develop-rules-lambda)
+ [

## AWS Config カスタムルールの形式の違い
](#evaluate-config_develop-schema)
+ [カスタムポリシールールの作成](evaluate-config_develop-rules_cfn-guard.md)
+ [カスタム Lambda ルールの作成](evaluate-config_develop-rules_lambda-functions.md)
+ [カスタム Lambda ルールにより削除されたリソースを管理する](evaluate-config_develop-rules-delete.md)

# AWS Config カスタムポリシールールの作成
<a name="evaluate-config_develop-rules_cfn-guard"></a>

 AWS Config カスタムポリシールールは AWS マネジメントコンソール、、 AWS CLI、または AWS Config API から作成できます。

## AWS Config カスタムポリシールールの追加
<a name="config-custom-policy-rules-add"></a>

------
#### [ Using the console ]

1. にサインイン AWS マネジメントコンソール し、[https://console.aws.amazon.com/config/home](https://console.aws.amazon.com/config/home) で AWS Config コンソールを開きます。

1.  AWS マネジメントコンソール メニューで、リージョンセレクタが AWS Config ルールをサポートする AWS リージョンに設定されていることを確認します。サポートされているリージョンのリストについては、「*Amazon Web Services 全般のリファレンス*」の「[AWS Config リージョンとエンドポイント](https://docs.aws.amazon.com/general/latest/gr/awsconfig.html)」を参照してください。

1. 左のナビゲーションで、**[ルール]** を選択します。

1. **[ルール]** ページで、**[ルールの追加]** を選択します。

1. [**Specify rule type**] (ルールタイプの指定) ページで [**Create custom rule using Guard**] (Guard を使用したカスタムルールの作成) を選択します。

1. [**ルールの設定**] ページで、以下のステップを完了してルールを作成します。

   1. [**Rule name**] (ルール名) に、ルールの一意の名前を入力します。

   1. [**説明**] に、ルールの説明を入力します。

   1. **Guard ランタイムバージョン**では、 AWS Config カスタムポリシールールのランタイムシステムを選択します。

   1. [**Rule Content**] (ルールコンテンツ) は、ルールの Guard カスタムポリシーで入力できます。

   1. **評価モードでは**、リソースの作成および管理プロセスでリソース AWS Config を評価するタイミングを選択します。ルールに応じて、 はリソースのプロビジョニング前、リソースのプロビジョニング後、またはその両方でリソース設定を評価 AWS Config できます。

      1. **[プロアクティブ評価をオンにする]** を選択すると、リソースをデプロイする前に、その設定内容に対して評価を実行できます。

         プロアクティブ評価をオンにした後、[StartResourceEvaluation](https://docs.aws.amazon.com/config/latest/APIReference/API_StartResourceEvaluation.html) API と [GetResourceEvaluationSummary](https://docs.aws.amazon.com/config/latest/APIReference/API_GetResourceEvaluationSummary.html) API を使用して、これらのコマンドで指定したリソースが、リソースのアカウントのプロアクティブルールに基づいて NON\$1COMPLIANT としてフラグが設定されるかどうかを確認できます。

          このコマンドの使用の詳細については、[AWS Config 「ルールを使用したリソースの評価](https://docs.aws.amazon.com/config/latest/developerguide/evaluating-your-resources.html#evaluating-your-resources-proactive)」を参照してください。プロアクティブ評価をサポートするマネージドルールのリストについては、[「評価モード別の AWS Config マネージドルールのリスト](https://docs.aws.amazon.com/config/latest/developerguide/managed-rules-by-evaluation-mode.html)」を参照してください。

      1. 既存のリソースの設定内容を評価するには、**[検出評価を有効にする]** を選択します。

         検出評価の場合、 AWS Config カスタムポリシールールは**設定の変更**によって開始されます。このオプションは事前に選択されます。
         +  **[リソース]** – 指定したリソースタイプや、リソースタイプおよび識別子と一致するリソースが作成、変更、または削除された場合。
         +  **[Tags]** (タグ) – 指定したタグを含むリソースが作成、変更、または削除された場合。
         +  **すべての変更** — によって記録されたリソース AWS Config が作成、変更、または削除された場合。

         AWS Config は、ルールのスコープに一致するリソースへの変更を検出すると、評価を実行します。スコープを使用して評価を開始するリソースを制限できます。それ以外の場合は、プロビジョニング後のリソースに変更があったときに評価が開始されます。

   1. **[Parameters]** (パラメータ) では、ルールにパラメータが含まれる場合、提供されたキーの値をカスタマイズできます。パラメータは、ルールに準拠しているとみなされるためにリソースが従う必要がある属性です。

1. **[レビューして作成]** ページで、 AWS アカウントアカウントにルールを追加する前に、編集中のすべての選択項目を確認します。

1. ルールの確認が完了したら、[**ルールの追加**] を選択します。

------
#### [ Using the AWS CLI ]

[https://docs.aws.amazon.com/cli/latest/reference/configservice/put-config-rule.html](https://docs.aws.amazon.com/cli/latest/reference/configservice/put-config-rule.html) コマンドを使用します。

`Owner` フィールドは `CUSTOM_POLICY` になっている必要があります。 AWS Config カスタムポリシールールには、次の追加フィールドが必要です。
+ `Runtime`: AWS Config カスタムポリシールールのランタイムシステム。
+ `PolicyText`: AWS Config カスタムポリシールールのロジックを含むポリシー定義。
+ `EnableDebugLogDelivery`: AWS Config カスタムポリシールールのデバッグログ記録を有効にするためのブール式。デフォルト値は `false` です。

------
#### [ Using the API Reference ]

[PutConfigRule](https://docs.aws.amazon.com/config/latest/APIReference/API_PutConfigRule.html) アクションを使用します。

`Owner` フィールドは `CUSTOM_POLICY` になっている必要があります。 AWS Config カスタムポリシールールには、次の追加フィールドが必要です。
+ `Runtime`: AWS Config カスタムポリシールールのランタイムシステム。
+ `PolicyText`: AWS Config カスタムポリシールールのロジックを含むポリシー定義。
+ `EnableDebugLogDelivery`: AWS Config カスタムポリシールールのデバッグログ記録を有効にするためのブール式。デフォルト値は `false` です。

------

## AWS Config カスタムポリシールールのルールコンテンツの記述
<a name="config-custom-policy-rules"></a>

 AWS Config カスタムポリシールールを使用すると、Guard AWS CloudFormation のドメイン固有の言語 (DSL) を使用してリソース設定を評価できます。このトピックでは、カスタムポリシールールを記述するためのパターンとベストプラクティスについて説明します。

Guard でルールを記述する方法の詳細については、[「Guard ユーザーガイド」の「Writing Guard rules](https://docs.aws.amazon.com/cfn-guard/latest/ug/writing-rules.html)」および「Guard GitHub リポジトリ[AWS CloudFormation 」の「Guard 2.0 の運用モード](https://github.com/aws-cloudformation/cloudformation-guard/tree/main/guard)」を参照してください。 AWS CloudFormation 

### 基本的なルール構造
<a name="config-custom-policy-rules-structure"></a>

ルールを作成するには、次の基本的な形式を使用します。

```
# 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
}
```

### 主要コンポーネント
<a name="config-custom-policy-rules-components"></a>

設定  
リソース設定の内容が含まれます。

supplementaryConfiguration  
リソース設定の追加コンテンツが含まれています。 は、特定のリソースタイプに対してこのフィールドを AWS Config 返し、設定フィールドに対して返される情報を補足します。

resourceType  
AWS 評価中の リソースタイプ。

resourceId  
リソースの ID (例: `sg-xxxxxx`)。

accountId  
リソースに関連付けられている 12 桁の AWS アカウント ID。

### 一般的なパターン
<a name="config-custom-policy-rules-patterns"></a>

------
#### [ 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>>
}}
```

------

### 高度な機能
<a name="config-custom-policy-rules-advanced"></a>

------
#### [ 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
}
```

------

### ベストプラクティス
<a name="config-custom-policy-rules-best-practices"></a>
+ 読みやすさを向上させるには、`let` ステートメントで変数を使用します。
+ 名前付きルールブロックを使用して関連するチェックをグループ化します。
+ わかりやすいコメントを含めます。
+ 適切な演算子 (`exists`、`is_string`、`is_list`) を使用します。
+ 大文字と小文字を区別しないマッチングで正規表現パターンを使用します。

### 例: dynamodb-pitr-enabled
<a name="config-custom-policy-rules-example"></a>

次の例は、 AWS Config マネージドルール の AWS Config カスタムポリシールールバージョンのポリシー定義を示しています[dynamodb-pitr-enabled](dynamodb-pitr-enabled.md)。このルールは、ポイントインタイムリカバリが 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>>
}
```

# AWS Config カスタム Lambda ルールの作成
<a name="evaluate-config_develop-rules_lambda-functions"></a>

カスタムルールを開発し、 AWS Lambda 関数 AWS Config を使用して に追加できます。

各カスタムルールを Lambda 関数に関連付けます。これには、 AWS リソースがルールに準拠しているかどうかを評価するロジックが含まれます。この関数をルールに関連付け、設定変更に応じて、または定期的にルールから関数を呼び出します。次に、関数はリソースがルールに準拠しているかどうかを評価し、その評価結果を に送信します AWS Config。

## AWS ルール開発キット (RDK)
<a name="rdk"></a>

 AWS Rule Development Kit (RDK) は、直感的で生産的なCompliance-as-Code」ワークフローをサポートするように設計されています。カスタム Lambda 関数によってバックアップされた AWS Config ルールのデプロイに関連する未分化の重労働の多くを抽象化し、効率的なdevelop-deploy-monitorの反復プロセスを提供します。

詳細な手順については、「[AWS ルール開発キット (RDK) ドキュメント](https://aws-config-rdk.readthedocs.io/en/master)」を参照してください。

## AWS Config ルールの AWS Lambda 関数の例 (Node.js)
<a name="evaluate-config_develop-rules_nodejs-sample"></a>

AWS Lambda は、 AWS サービスによって発行されたイベントに応答して関数を実行します。 AWS Config Custom Lambda ルールの 関数は、 によって発行されるイベントを受け取り AWS Config、その後、イベントから受け取ったデータと AWS Config API から取得したデータを使用してルールのコンプライアンスを評価します。Config ルールの関数のオペレーションは、設定変更にトリガーされて評価するか、定期的にトリガーされて評価するかによって異なります。

 AWS Lambda 関数内の一般的なパターンについては、「 *AWS Lambda デベロッパーガイド*」の[「プログラミングモデル](https://docs.aws.amazon.com/lambda/latest/dg/programming-model-v2.html)」を参照してください。

------
#### [ Example Function for Evaluations Triggered by Configuration Changes ]

AWS Config は、カスタムルールの範囲内にあるリソースの設定変更を検出すると、次の例のような関数を呼び出します。

 AWS Config コンソールを使用して、この例のような関数に関連付けられたルールを作成する場合は、トリガータイプとして**設定の変更**を選択します。 AWS Config API または を使用してルール AWS CLI を作成する場合は、 `MessageType` 属性を `ConfigurationItemChangeNotification`および に設定します`OversizedConfigurationItemChangeNotification`。これらの設定により、リソースの変更の結果として が設定項目またはオーバーサイズの設定項目 AWS Config を生成するたびに、ルールをトリガーできます。

この例では、リソースを評価し、インスタンスがリソースタイプ `AWS::EC2::Instance` と一致するかどうかを確認します。 AWS Config で設定項目またはサイズが大きすぎる設定項目が生成されると、ルールがトリガーされます。

```
'use strict';

import { ConfigServiceClient, GetResourceConfigHistoryCommand, PutEvaluationsCommand } from "@aws-sdk/client-config-service";

const configClient = new ConfigServiceClient({});

// Helper function used to validate input
function checkDefined(reference, referenceName) {
    if (!reference) {
        throw new Error(`Error: ${referenceName} is not defined`);
    }
    return reference;
}

// Check whether the message type is OversizedConfigurationItemChangeNotification,
function isOverSizedChangeNotification(messageType) {
    checkDefined(messageType, 'messageType');
    return messageType === 'OversizedConfigurationItemChangeNotification';
}

// Get the configurationItem for the resource using the getResourceConfigHistory API.
async function getConfiguration(resourceType, resourceId, configurationCaptureTime, callback) {
    const input = { resourceType, resourceId, laterTime: new Date(configurationCaptureTime), limit: 1 };
    const command = new GetResourceConfigHistoryCommand(input);
    await configClient.send(command).then(
        (data) => {
            callback(null, data.configurationItems[0]);
        },
        (error) => {
            callback(error, null);
        }
    );

}

// Convert the oversized configuration item from the API model to the original invocation model.
function convertApiConfiguration(apiConfiguration) {
    apiConfiguration.awsAccountId = apiConfiguration.accountId;
    apiConfiguration.ARN = apiConfiguration.arn;
    apiConfiguration.configurationStateMd5Hash = apiConfiguration.configurationItemMD5Hash;
    apiConfiguration.configurationItemVersion = apiConfiguration.version;
    apiConfiguration.configuration = JSON.parse(apiConfiguration.configuration);
    if ({}.hasOwnProperty.call(apiConfiguration, 'relationships')) {
        for (let i = 0; i < apiConfiguration.relationships.length; i++) {
            apiConfiguration.relationships[i].name = apiConfiguration.relationships[i].relationshipName;
        }
    }
    return apiConfiguration;
}

// Based on the message type, get the configuration item either from the configurationItem object in the invoking event or with the getResourceConfigHistory API in the getConfiguration function.
async function getConfigurationItem(invokingEvent, callback) {
    checkDefined(invokingEvent, 'invokingEvent');
    if (isOverSizedChangeNotification(invokingEvent.messageType)) {
        const configurationItemSummary = checkDefined(invokingEvent.configurationItemSummary, 'configurationItemSummary');
        await getConfiguration(configurationItemSummary.resourceType, configurationItemSummary.resourceId, configurationItemSummary.configurationItemCaptureTime, (err, apiConfigurationItem) => {
            if (err) {
                callback(err);
            }
            const configurationItem = convertApiConfiguration(apiConfigurationItem);
            callback(null, configurationItem);
        });
    } else {
        checkDefined(invokingEvent.configurationItem, 'configurationItem');
        callback(null, invokingEvent.configurationItem);
    }
}

// Check whether the resource has been deleted. If the resource was deleted, then the evaluation returns not applicable.
function isApplicable(configurationItem, event) {
    checkDefined(configurationItem, 'configurationItem');
    checkDefined(event, 'event');
    const status = configurationItem.configurationItemStatus;
    const eventLeftScope = event.eventLeftScope;
    return (status === 'OK' || status === 'ResourceDiscovered') && eventLeftScope === false;
}

// In this example, the resource is compliant if it is an instance and its type matches the type specified as the desired type.
// If the resource is not an instance, then this resource is not applicable.
function evaluateChangeNotificationCompliance(configurationItem, ruleParameters) {
    checkDefined(configurationItem, 'configurationItem');
    checkDefined(configurationItem.configuration, 'configurationItem.configuration');
    checkDefined(ruleParameters, 'ruleParameters');

    if (configurationItem.resourceType !== 'AWS::EC2::Instance') {
        return 'NOT_APPLICABLE';
    } else if (ruleParameters.desiredInstanceType === configurationItem.configuration.instanceType) {
        return 'COMPLIANT';
    }
    return 'NON_COMPLIANT';
}

// Receives the event and context from AWS Lambda.
export const handler = async (event, context) => {
    checkDefined(event, 'event');
    const invokingEvent = JSON.parse(event.invokingEvent);
    const ruleParameters = JSON.parse(event.ruleParameters);
    await getConfigurationItem(invokingEvent, async (err, configurationItem) => {

        let compliance = 'NOT_APPLICABLE';
        let annotation = '';
        const putEvaluationsRequest = {};
        if (isApplicable(configurationItem, event)) {
            // Invoke the compliance checking function.
            compliance = evaluateChangeNotificationCompliance(configurationItem, ruleParameters);
            if (compliance === "NON_COMPLIANT") {
                annotation = "This is an annotation describing why the resource is not compliant.";
            }
        }
        // Initializes the request that contains the evaluation results.
        if (annotation) {
            putEvaluationsRequest.Evaluations = [
                {
                    ComplianceResourceType: configurationItem.resourceType,
                    ComplianceResourceId: configurationItem.resourceId,
                    ComplianceType: compliance,
                    OrderingTimestamp: new Date(configurationItem.configurationItemCaptureTime),
                    Annotation: annotation
                },
            ];
        } else {
            putEvaluationsRequest.Evaluations = [
                {
                    ComplianceResourceType: configurationItem.resourceType,
                    ComplianceResourceId: configurationItem.resourceId,
                    ComplianceType: compliance,
                    OrderingTimestamp: new Date(configurationItem.configurationItemCaptureTime),
                },
            ];
        }
        putEvaluationsRequest.ResultToken = event.resultToken;

        // Sends the evaluation results to AWS Config.
        await configClient.send(new PutEvaluationsCommand(putEvaluationsRequest));
    });
};
```

**関数のオペレーション**

関数はランタイムに以下のオペレーションを実行します。

1. 関数は、 が `event` オブジェクトを`handler`関数に AWS Lambda 渡すときに実行されます。この例では、 関数は、呼び出し元に情報を返すために使用するオプションの `callback`パラメータを受け入れます。 は、関数の実行中に使用できる情報とメソッドを含む `context` オブジェクト AWS Lambda も渡します。Lambda の新しいバージョンでは、コンテキストは使用されなくなりました。

1. 関数は、イベントの `messageType` が設定項目であるかサイズが大きすぎる設定項目であるかを確認し、その設定項目を返します。

1. ハンドラーは、`isApplicable` 関数を呼び出してリソースが削除されたかどうかを確認します。
**注記**  
削除されたリソースをレポートするルールは、不必要なルール評価を避けるために、`NOT_APPLICABLE` の評価結果を返す必要があります。

1. ハンドラーは `evaluateChangeNotificationCompliance`関数を呼び出し、 イベントで AWS Config 発行した `configurationItem`および `ruleParameters` オブジェクトを渡します。

   関数は最初にリソースが EC2 インスタンスであるかどうかを評価します。リソースが EC2 インスタンスではない場合、関数はコンプライアンス値として `NOT_APPLICABLE` を返します。

   次に、関数は設定項目の `instanceType` 属性が `desiredInstanceType` パラメータ値と等しいかどうかを評価します。値が等しい場合、関数は `COMPLIANT` を返します。値が等しくない場合、関数は `NON_COMPLIANT` を返します。

1. ハンドラーは、 `putEvaluationsRequest` オブジェクトを初期化 AWS Config して評価結果を に送信する準備をします。このオブジェクトに含まれている `Evaluations` パラメータは、評価対象のリソースのコンプライアンス結果、リソースタイプ、および ID を識別します。`putEvaluationsRequest` オブジェクトには、ルールとイベントを識別するイベントの結果トークンも含まれます AWS Config。

1. ハンドラーは、 オブジェクトを`config`クライアントの `putEvaluations`メソッドに渡 AWS Config して、評価結果を に送信します。

------
#### [ Example Function for Periodic Evaluations ]

AWS Config は、定期的な評価のために次の例のような関数を呼び出します。定期的な評価は、 AWS Configでのルールの定義時に指定した間隔で発生します。

 AWS Config コンソールを使用して、この例のような関数に関連付けられたルールを作成する場合は、トリガータイプとして **Periodic** を選択します。 AWS Config API または を使用してルール AWS CLI を作成する場合は、 `MessageType` 属性を に設定します`ScheduledNotification`。

この例では、指定したリソースの合計数が指定した最大値を超えているかどうかを確認します。

```
'use strict';
import { ConfigServiceClient, ListDiscoveredResourcesCommand, PutEvaluationsCommand } from "@aws-sdk/client-config-service";

const configClient = new ConfigServiceClient({});

// Receives the event and context from AWS Lambda.
export const handler = async (event, context, callback) => {
    // Parses the invokingEvent and ruleParameters values, which contain JSON objects passed as strings.
    var invokingEvent = JSON.parse(event.invokingEvent),
        ruleParameters = JSON.parse(event.ruleParameters),
        numberOfResources = 0;

    if (isScheduledNotification(invokingEvent) && hasValidRuleParameters(ruleParameters, callback)) {
        await countResourceTypes(ruleParameters.applicableResourceType, "", numberOfResources, async function (err, count) {
            if (err === null) {
                var putEvaluationsRequest;
                const compliance = evaluateCompliance(ruleParameters.maxCount, count);
                var annotation = '';
                if (compliance === "NON_COMPLIANT") {
                    annotation = "Description of why the resource is not compliant.";
                }
                // Initializes the request that contains the evaluation results.
                if (annotation) {
                    putEvaluationsRequest = {
                        Evaluations: [{
                            // Applies the evaluation result to the AWS account published in the event.
                            ComplianceResourceType: 'AWS::::Account',
                            ComplianceResourceId: event.accountId,
                            ComplianceType: compliance,
                            OrderingTimestamp: new Date(),
                            Annotation: annotation
                        }],
                        ResultToken: event.resultToken
                    };
                } else {
                    putEvaluationsRequest = {
                        Evaluations: [{
                            // Applies the evaluation result to the AWS account published in the event.
                            ComplianceResourceType: 'AWS::::Account',
                            ComplianceResourceId: event.accountId,
                            ComplianceType: compliance,
                            OrderingTimestamp: new Date()
                        }],
                        ResultToken: event.resultToken
                    };
                }

                // Sends the evaluation results to AWS Config.
                try {
                    await configClient.send(new PutEvaluationsCommand(putEvaluationsRequest));
                }
                catch (e) {
                    callback(e, null);
                }
            } else {
                callback(err, null);
            }
        });
    } else {
        console.log("Invoked for a notification other than Scheduled Notification... Ignoring.");
    }
};

// Checks whether the invoking event is ScheduledNotification.
function isScheduledNotification(invokingEvent) {
    return (invokingEvent.messageType === 'ScheduledNotification');
}

// Checks the rule parameters to see if they are valid
function hasValidRuleParameters(ruleParameters, callback) {
    // Regular express to verify that applicable resource given is a resource type
    const awsResourcePattern = /^AWS::(\w*)::(\w*)$/;
    const isApplicableResourceType = awsResourcePattern.test(ruleParameters.applicableResourceType);
    // Check to make sure the maxCount in the parameters is an integer
    const maxCountIsInt = !isNaN(ruleParameters.maxCount) && parseInt(Number(ruleParameters.maxCount)) == ruleParameters.maxCount && !isNaN(parseInt(ruleParameters.maxCount, 10));
    if (!isApplicableResourceType) {
        callback("The applicableResourceType parameter is not a valid resource type.", null);
    }
    if (!maxCountIsInt) {
        callback("The maxCount parameter is not a valid integer.", null);
    }
    return isApplicableResourceType && maxCountIsInt;
}

// Checks whether the compliance conditions for the rule are violated.
function evaluateCompliance(maxCount, actualCount) {
    if (actualCount > maxCount) {
        return "NON_COMPLIANT";
    } else {
        return "COMPLIANT";
    }
}

// Counts the applicable resources that belong to the AWS account.
async function countResourceTypes(applicableResourceType, nextToken, count, callback) {
    const input = { resourceType: applicableResourceType, nextToken: nextToken };
    const command = new ListDiscoveredResourcesCommand(input);
    try {
        const response = await configClient.send(command);
        count = count + response.resourceIdentifiers.length;
        if (response.nextToken !== undefined && response.nextToken != null) {
            countResourceTypes(applicableResourceType, response.nextToken, count, callback);
        }
        callback(null, count);
    } catch (e) {
        callback(e, null);
    }
    return count;
}
```

**関数のオペレーション**

関数はランタイムに以下のオペレーションを実行します。

1. 関数は、 が `event` オブジェクトを`handler`関数に AWS Lambda 渡すときに実行されます。この例では、 関数は、呼び出し元に情報を返すために使用するオプションの `callback`パラメータを受け入れます。 は、関数の実行中に使用できる情報とメソッドを含む `context` オブジェクト AWS Lambda も渡します。Lambda の新しいバージョンでは、コンテキストは使用されなくなりました。

1. 指定したタイプのリソースをカウントする場合、ハンドラーは `countResourceTypes` 関数を呼び出し、イベントから受け取った `applicableResourceType` パラメータを渡します。`countResourceTypes` 関数は、`config` クライアントの `listDiscoveredResources` メソッドを呼び出します。クライアントは、該当するリソースの ID のリストを返します。関数は、このリストの長さに基づいて該当するリソースの数を判断し、この数をハンドラーに返します。

1. ハンドラーは、 `putEvaluationsRequest` オブジェクトを初期化 AWS Config して評価結果を に送信する準備をします。このオブジェクトには、コンプライアンス結果を識別する `Evaluations`パラメータと、イベントで AWS アカウント 発行された が含まれます。`Evaluations` パラメータでは、 AWS Configでサポートされている任意のリソースタイプに結果を適用できます。`putEvaluationsRequest` オブジェクトには、ルールとイベントを識別するイベントの結果トークンも含まれます AWS Config。

1. `putEvaluationsRequest` オブジェクト内で、ハンドラーは `evaluateCompliance` 関数を呼び出します。この関数は、該当するリソースの数が、イベントから提供された `maxCount` パラメータに割り当てた最大値を超えているかどうかをテストします。リソース数が最大値を超えている場合、関数は `NON_COMPLIANT` を返します。リソース数が最大値を超えていない場合、関数は `COMPLIANT` を返します。

1. ハンドラーは、 オブジェクトを`config`クライアントの `putEvaluations`メソッドに渡 AWS Config して、評価結果を に送信します。

------

## AWS Config ルールの AWS Lambda 関数の例 (Python)
<a name="evaluate-config_develop-rules_python-sample"></a>

AWS Lambda は、 AWS サービスによって発行されたイベントに応答して関数を実行します。 AWS Config Custom Lambda ルールの 関数は、 によって発行されるイベントを受け取り AWS Config、その後、イベントから受け取ったデータと AWS Config API から取得したデータを使用してルールのコンプライアンスを評価します。Config ルールの関数のオペレーションは、設定変更にトリガーされて評価するか、定期的にトリガーされて評価するかによって異なります。

 AWS Lambda 関数内の一般的なパターンについては、「 *AWS Lambda デベロッパーガイド*」の[「プログラミングモデル](https://docs.aws.amazon.com/lambda/latest/dg/programming-model-v2.html)」を参照してください。

------
#### [ Example Function for Evaluations Triggered by Configuration Changes ]

AWS Config は、カスタムルールの範囲内にあるリソースの設定変更を検出すると、次の例のような関数を呼び出します。

 AWS Config コンソールを使用して、この例のような関数に関連付けられたルールを作成する場合は、トリガータイプとして**設定の変更**を選択します。 AWS Config API または を使用してルール AWS CLI を作成する場合は、 `MessageType` 属性を `ConfigurationItemChangeNotification`および に設定します`OversizedConfigurationItemChangeNotification`。これらの設定により、リソースの変更の結果として が設定項目またはオーバーサイズの設定項目 AWS Config を生成するたびに、ルールをトリガーできます。

```
import botocore 
import boto3
import json
import datetime

# Set to True to get the lambda to assume the Role attached on the Config Service (useful for cross-account).
ASSUME_ROLE_MODE = False

# This gets the client after assuming the Config service role
# either in the same AWS account or cross-account.
def get_client(service, event):
    """Return the service boto client. It should be used instead of directly calling the client.
    Keyword arguments:
    service -- the service name used for calling the boto.client()
    event -- the event variable given in the lambda handler
    """
    if not ASSUME_ROLE_MODE:
        return boto3.client(service)
    credentials = get_assume_role_credentials(event["executionRoleArn"])
    return boto3.client(service, aws_access_key_id=credentials['AccessKeyId'],
                        aws_secret_access_key=credentials['SecretAccessKey'],
                        aws_session_token=credentials['SessionToken']
                       )

# Helper function used to validate input
def check_defined(reference, reference_name):
    if not reference:
        raise Exception('Error: ', reference_name, 'is not defined')
    return reference

# Check whether the message is OversizedConfigurationItemChangeNotification or not
def is_oversized_changed_notification(message_type):
    check_defined(message_type, 'messageType')
    return message_type == 'OversizedConfigurationItemChangeNotification'

# Get configurationItem using getResourceConfigHistory API
# in case of OversizedConfigurationItemChangeNotification
def get_configuration(resource_type, resource_id, configuration_capture_time):
    result = AWS_CONFIG_CLIENT.get_resource_config_history(
        resourceType=resource_type,
        resourceId=resource_id,
        laterTime=configuration_capture_time,
        limit=1)
    configurationItem = result['configurationItems'][0]
    return convert_api_configuration(configurationItem)

# Convert from the API model to the original invocation model
def convert_api_configuration(configurationItem):
    for k, v in configurationItem.items():
        if isinstance(v, datetime.datetime):
            configurationItem[k] = str(v)
    configurationItem['awsAccountId'] = configurationItem['accountId']
    configurationItem['ARN'] = configurationItem['arn']
    configurationItem['configurationStateMd5Hash'] = configurationItem['configurationItemMD5Hash']
    configurationItem['configurationItemVersion'] = configurationItem['version']
    configurationItem['configuration'] = json.loads(configurationItem['configuration'])
    if 'relationships' in configurationItem:
        for i in range(len(configurationItem['relationships'])):
            configurationItem['relationships'][i]['name'] = configurationItem['relationships'][i]['relationshipName']
    return configurationItem

# Based on the type of message get the configuration item
# either from configurationItem in the invoking event
# or using the getResourceConfigHistory API in getConfiguration function.
def get_configuration_item(invokingEvent):
    check_defined(invokingEvent, 'invokingEvent')
    if is_oversized_changed_notification(invokingEvent['messageType']):
        configurationItemSummary = check_defined(invokingEvent['configurationItemSummary'], 'configurationItemSummary')
        return get_configuration(configurationItemSummary['resourceType'], configurationItemSummary['resourceId'], configurationItemSummary['configurationItemCaptureTime'])
    return check_defined(invokingEvent['configurationItem'], 'configurationItem')

# Check whether the resource has been deleted. If it has, then the evaluation is unnecessary.
def is_applicable(configurationItem, event):
    try:
        check_defined(configurationItem, 'configurationItem')
        check_defined(event, 'event')
    except:
        return True
    status = configurationItem['configurationItemStatus']
    eventLeftScope = event['eventLeftScope']
    if status == 'ResourceDeleted':
        print("Resource Deleted, setting Compliance Status to NOT_APPLICABLE.")
    return (status == 'OK' or status == 'ResourceDiscovered') and not eventLeftScope

def get_assume_role_credentials(role_arn):
    sts_client = boto3.client('sts')
    try:
        assume_role_response = sts_client.assume_role(RoleArn=role_arn, RoleSessionName="configLambdaExecution")
        return assume_role_response['Credentials']
    except botocore.exceptions.ClientError as ex:
        # Scrub error message for any internal account info leaks
        if 'AccessDenied' in ex.response['Error']['Code']:
            ex.response['Error']['Message'] = "AWS Config does not have permission to assume the IAM role."
        else:
            ex.response['Error']['Message'] = "InternalError"
            ex.response['Error']['Code'] = "InternalError"
        raise ex

def evaluate_change_notification_compliance(configuration_item, rule_parameters):
    check_defined(configuration_item, 'configuration_item')
    check_defined(configuration_item['configuration'], 'configuration_item[\'configuration\']')
    if rule_parameters:
        check_defined(rule_parameters, 'rule_parameters')

    if (configuration_item['resourceType'] != 'AWS::EC2::Instance'):
        return 'NOT_APPLICABLE'

    elif rule_parameters.get('desiredInstanceType'):
        if (configuration_item['configuration']['instanceType'] in rule_parameters['desiredInstanceType']):
            return 'COMPLIANT'
    return 'NON_COMPLIANT'

def lambda_handler(event, context):

    global AWS_CONFIG_CLIENT

    check_defined(event, 'event')
    invoking_event = json.loads(event['invokingEvent'])
    rule_parameters = {}
    if 'ruleParameters' in event:
        rule_parameters = json.loads(event['ruleParameters'])

    compliance_value = 'NOT_APPLICABLE'

    AWS_CONFIG_CLIENT = get_client('config', event)
    configuration_item = get_configuration_item(invoking_event)
    if is_applicable(configuration_item, event):
        compliance_value = evaluate_change_notification_compliance(
                configuration_item, rule_parameters)

    response = AWS_CONFIG_CLIENT.put_evaluations(
       Evaluations=[
           {
               'ComplianceResourceType': invoking_event['configurationItem']['resourceType'],
               'ComplianceResourceId': invoking_event['configurationItem']['resourceId'],
               'ComplianceType': compliance_value,
               'OrderingTimestamp': invoking_event['configurationItem']['configurationItemCaptureTime']
           },
       ],
       ResultToken=event['resultToken'])
```

**関数のオペレーション**

関数はランタイムに以下のオペレーションを実行します。

1. 関数は、 が `event` オブジェクトを`handler`関数に AWS Lambda 渡すときに実行されます。この例では、 関数は、呼び出し元に情報を返すために使用するオプションの `callback`パラメータを受け入れます。 は、関数の実行中に使用できる情報とメソッドを含む `context` オブジェクト AWS Lambda も渡します。Lambda の新しいバージョンでは、コンテキストは使用されなくなりました。

1. 関数は、イベントの `messageType` が設定項目であるかサイズが大きすぎる設定項目であるかを確認し、その設定項目を返します。

1. ハンドラーは、`isApplicable` 関数を呼び出してリソースが削除されたかどうかを確認します。
**注記**  
削除されたリソースをレポートするルールは、不必要なルール評価を避けるために、`NOT_APPLICABLE` の評価結果を返す必要があります。

1. ハンドラーは `evaluateChangeNotificationCompliance`関数を呼び出し、 イベントで AWS Config 発行した `configurationItem`および `ruleParameters` オブジェクトを渡します。

   関数は最初にリソースが EC2 インスタンスであるかどうかを評価します。リソースが EC2 インスタンスではない場合、関数はコンプライアンス値として `NOT_APPLICABLE` を返します。

   次に、関数は設定項目の `instanceType` 属性が `desiredInstanceType` パラメータ値と等しいかどうかを評価します。値が等しい場合、関数は `COMPLIANT` を返します。値が等しくない場合、関数は `NON_COMPLIANT` を返します。

1. ハンドラーは、 `putEvaluationsRequest` オブジェクトを初期化 AWS Config して評価結果を に送信する準備をします。このオブジェクトに含まれている `Evaluations` パラメータは、評価対象のリソースのコンプライアンス結果、リソースタイプ、および ID を識別します。`putEvaluationsRequest` オブジェクトには、ルールとイベントを識別するイベントの結果トークンも含まれます AWS Config。

1. ハンドラーは、 オブジェクトを`config`クライアントの `putEvaluations`メソッドに渡 AWS Config して、評価結果を に送信します。

------
#### [ Example Function for Periodic Evaluations ]

AWS Config は、定期的な評価のために次の例のような関数を呼び出します。定期的な評価は、 AWS Configでのルールの定義時に指定した間隔で発生します。

 AWS Config コンソールを使用して、この例のような関数に関連付けられているルールを作成する場合は、トリガータイプとして **Periodic** を選択します。 AWS Config API または を使用してルール AWS CLI を作成する場合は、 `MessageType` 属性を に設定します`ScheduledNotification`。

```
import botocore 
import boto3
import json
import datetime

# Set to True to get the lambda to assume the Role attached on the Config Service (useful for cross-account).
ASSUME_ROLE_MODE = False
DEFAULT_RESOURCE_TYPE = 'AWS::::Account'

# This gets the client after assuming the Config service role
# either in the same AWS account or cross-account.
def get_client(service, event):
    """Return the service boto client. It should be used instead of directly calling the client.
    Keyword arguments:
    service -- the service name used for calling the boto.client()
    event -- the event variable given in the lambda handler
    """
    if not ASSUME_ROLE_MODE:
        return boto3.client(service)
    credentials = get_assume_role_credentials(event["executionRoleArn"])
    return boto3.client(service, aws_access_key_id=credentials['AccessKeyId'],
                        aws_secret_access_key=credentials['SecretAccessKey'],
                        aws_session_token=credentials['SessionToken']
                       )

def get_assume_role_credentials(role_arn):
    sts_client = boto3.client('sts')
    try:
        assume_role_response = sts_client.assume_role(RoleArn=role_arn, RoleSessionName="configLambdaExecution")
        return assume_role_response['Credentials']
    except botocore.exceptions.ClientError as ex:
        # Scrub error message for any internal account info leaks
        if 'AccessDenied' in ex.response['Error']['Code']:
            ex.response['Error']['Message'] = "AWS Config does not have permission to assume the IAM role."
        else:
            ex.response['Error']['Message'] = "InternalError"
            ex.response['Error']['Code'] = "InternalError"
        raise ex

# Check whether the message is a ScheduledNotification or not.
def is_scheduled_notification(message_type):
    return message_type == 'ScheduledNotification'

def count_resource_types(applicable_resource_type, next_token, count):
    resource_identifier = AWS_CONFIG_CLIENT.list_discovered_resources(resourceType=applicable_resource_type, nextToken=next_token)
    updated = count + len(resource_identifier['resourceIdentifiers']);
    return updated

# Evaluates the configuration items in the snapshot and returns the compliance value to the handler.
def evaluate_compliance(max_count, actual_count):
    return 'NON_COMPLIANT' if int(actual_count) > int(max_count) else 'COMPLIANT'

def evaluate_parameters(rule_parameters):
    if 'applicableResourceType' not in rule_parameters:
        raise ValueError('The parameter with "applicableResourceType" as key must be defined.')
    if not rule_parameters['applicableResourceType']:
        raise ValueError('The parameter "applicableResourceType" must have a defined value.')
    return rule_parameters

# This generate an evaluation for config
def build_evaluation(resource_id, compliance_type, event, resource_type=DEFAULT_RESOURCE_TYPE, annotation=None):
    """Form an evaluation as a dictionary. Usually suited to report on scheduled rules.
    Keyword arguments:
    resource_id -- the unique id of the resource to report
    compliance_type -- either COMPLIANT, NON_COMPLIANT or NOT_APPLICABLE
    event -- the event variable given in the lambda handler
    resource_type -- the CloudFormation resource type (or AWS::::Account) to report on the rule (default DEFAULT_RESOURCE_TYPE)
    annotation -- an annotation to be added to the evaluation (default None)
    """
    eval_cc = {}
    if annotation:
        eval_cc['Annotation'] = annotation
    eval_cc['ComplianceResourceType'] = resource_type
    eval_cc['ComplianceResourceId'] = resource_id
    eval_cc['ComplianceType'] = compliance_type
    eval_cc['OrderingTimestamp'] = str(json.loads(event['invokingEvent'])['notificationCreationTime'])
    return eval_cc

def lambda_handler(event, context):

    global AWS_CONFIG_CLIENT

    evaluations = []
    rule_parameters = {}
    resource_count = 0
    max_count = 0

    invoking_event = json.loads(event['invokingEvent'])
    if 'ruleParameters' in event:
        rule_parameters = json.loads(event['ruleParameters'])
        valid_rule_parameters = evaluate_parameters(rule_parameters)

    compliance_value = 'NOT_APPLICABLE'

    AWS_CONFIG_CLIENT = get_client('config', event)
    if is_scheduled_notification(invoking_event['messageType']):
        result_resource_count = count_resource_types(valid_rule_parameters['applicableResourceType'], '', resource_count)

    if valid_rule_parameters.get('maxCount'):
        max_count = valid_rule_parameters['maxCount']

    compliance_value = evaluate_compliance(max_count, result_resource_count)
    evaluations.append(build_evaluation(event['accountId'], compliance_value, event, resource_type=DEFAULT_RESOURCE_TYPE))
    response = AWS_CONFIG_CLIENT.put_evaluations(Evaluations=evaluations, ResultToken=event['resultToken'])
```

**関数のオペレーション**

関数はランタイムに以下のオペレーションを実行します。

1. 関数は、 が `event` オブジェクトを`handler`関数に AWS Lambda 渡すときに実行されます。この例では、 関数は、呼び出し元に情報を返すために使用するオプションの `callback`パラメータを受け入れます。 は、関数の実行中に使用できる情報とメソッドを含む `context` オブジェクト AWS Lambda も渡します。Lambda の新しいバージョンでは、コンテキストは使用されなくなりました。

1. 指定したタイプのリソースをカウントする場合、ハンドラーは `countResourceTypes` 関数を呼び出し、イベントから受け取った `applicableResourceType` パラメータを渡します。`countResourceTypes` 関数は、`config` クライアントの `listDiscoveredResources` メソッドを呼び出します。クライアントは、該当するリソースの ID のリストを返します。関数は、このリストの長さに基づいて該当するリソースの数を判断し、この数をハンドラーに返します。

1. ハンドラーは、 `putEvaluationsRequest` オブジェクトを初期化 AWS Config して評価結果を に送信する準備をします。このオブジェクトには、コンプライアンス結果を識別する `Evaluations`パラメータと、イベントで AWS アカウント 発行された が含まれます。`Evaluations` パラメータでは、 AWS Configでサポートされている任意のリソースタイプに結果を適用できます。`putEvaluationsRequest` オブジェクトには、ルールとイベントを識別するイベントの結果トークンも含まれます AWS Config。

1. `putEvaluationsRequest` オブジェクト内で、ハンドラーは `evaluateCompliance` 関数を呼び出します。この関数は、該当するリソースの数が、イベントから提供された `maxCount` パラメータに割り当てた最大値を超えているかどうかをテストします。リソース数が最大値を超えている場合、関数は `NON_COMPLIANT` を返します。リソース数が最大値を超えていない場合、関数は `COMPLIANT` を返します。

1. ハンドラーは、 オブジェクトを`config`クライアントの `putEvaluations`メソッドに渡 AWS Config して、評価結果を に送信します。

------

## AWS Config ルールのイベント例
<a name="evaluate-config_develop-rules_example-events"></a>

ルールのトリガーが発生すると、 はイベントを発行してルールの AWS Lambda 関数を AWS Config 呼び出します。次に、 はイベントを関数のハンドラに渡して関数 AWS Lambda を実行します。

------
#### [ Example Event for Evaluations Triggered by Configuration Changes ]

AWS Config は、ルールの範囲内にあるリソースの設定変更を検出すると、イベントを発行します。次のイベント例は、EC2 インスタンスの設定変更でルールがトリガーされたことを示しています。

```
{ 
    "invokingEvent": "{\"configurationItem\":{\"configurationItemCaptureTime\":\"2016-02-17T01:36:34.043Z\",\"awsAccountId\":\"123456789012\",\"configurationItemStatus\":\"OK\",\"resourceId\":\"i-00000000\",\"ARN\":\"arn:aws:ec2:us-east-2:123456789012:instance/i-00000000\",\"awsRegion\":\"us-east-2\",\"availabilityZone\":\"us-east-2a\",\"resourceType\":\"AWS::EC2::Instance\",\"tags\":{\"Foo\":\"Bar\"},\"relationships\":[{\"resourceId\":\"eipalloc-00000000\",\"resourceType\":\"AWS::EC2::EIP\",\"name\":\"Is attached to ElasticIp\"}],\"configuration\":{\"foo\":\"bar\"}},\"messageType\":\"ConfigurationItemChangeNotification\"}",
    "ruleParameters": "{\"myParameterKey\":\"myParameterValue\"}",
    "resultToken": "myResultToken",
    "eventLeftScope": false,
    "executionRoleArn": "arn:aws:iam::123456789012:role/config-role",
    "configRuleArn": "arn:aws:config:us-east-2:123456789012:config-rule/config-rule-0123456",
    "configRuleName": "change-triggered-config-rule",
    "configRuleId": "config-rule-0123456",
    "accountId": "123456789012",
    "version": "1.0"
}
```

------
#### [ Example Event for Evaluations Triggered by Oversized Configuration Changes ]

一部のリソースの変更では、サイズが大きすぎる設定項目が生成されます。次のイベント例は、EC2 インスタンスのサイズが大きすぎる設定変更でルールがトリガーされたことを示しています。

```
{
        "invokingEvent": "{\"configurationItemSummary\": {\"changeType\": \"UPDATE\",\"configurationItemVersion\": \"1.2\",\"configurationItemCaptureTime\":\"2016-10-06T16:46:16.261Z\",\"configurationStateId\": 0,\"awsAccountId\":\"123456789012\",\"configurationItemStatus\": \"OK\",\"resourceType\": \"AWS::EC2::Instance\",\"resourceId\":\"i-00000000\",\"resourceName\":null,\"ARN\":\"arn:aws:ec2:us-west-2:123456789012:instance/i-00000000\",\"awsRegion\": \"us-west-2\",\"availabilityZone\":\"us-west-2a\",\"configurationStateMd5Hash\":\"8f1ee69b287895a0f8bc5753eca68e96\",\"resourceCreationTime\":\"2016-10-06T16:46:10.489Z\"},\"messageType\":\"OversizedConfigurationItemChangeNotification\"}",
        "ruleParameters": "{\"myParameterKey\":\"myParameterValue\"}",
        "resultToken": "myResultToken",
        "eventLeftScope": false,
        "executionRoleArn": "arn:aws:iam::123456789012:role/config-role",
        "configRuleArn": "arn:aws:config:us-east-2:123456789012:config-rule/config-rule-ec2-managed-instance-inventory",
        "configRuleName": "change-triggered-config-rule",
        "configRuleId": "config-rule-0123456",
        "accountId": "123456789012",
        "version": "1.0"
    }
```

------
#### [ Example Event for Evaluations Triggered by Periodic Frequency ]

AWS Config は、指定した頻度 (24 時間ごとなど) でリソースを評価するときにイベントを発行します。次のイベント例は、定期的な間隔でルールがトリガーされたことを示しています。

```
{
    "invokingEvent": "{\"awsAccountId\":\"123456789012\",\"notificationCreationTime\":\"2016-07-13T21:50:00.373Z\",\"messageType\":\"ScheduledNotification\",\"recordVersion\":\"1.0\"}",
    "ruleParameters": "{\"myParameterKey\":\"myParameterValue\"}",
    "resultToken": "myResultToken",
    "eventLeftScope": false,
    "executionRoleArn": "arn:aws:iam::123456789012:role/config-role",
    "configRuleArn": "arn:aws:config:us-east-2:123456789012:config-rule/config-rule-0123456",
    "configRuleName": "periodic-config-rule",
    "configRuleId": "config-rule-6543210",
    "accountId": "123456789012",
    "version": "1.0"
}
```

------

### イベントの属性
<a name="w2aac20c19c20c13b1b7"></a>

 AWS Config イベントの JSON オブジェクトには、次の属性が含まれます。

`invokingEvent`  
ルールの評価をトリガーするイベント。リソースの設定変更に応じてイベントが発行される場合、この属性の値は JSON `configurationItem` または `configurationItemSummary` (サイズが大きすぎる設定項目の場合) を含む文字列です。設定項目は、変更 AWS Config を検出した時点のリソースの状態を表します。設定項目の例については、 の `get-resource-config-history` AWS CLI コマンドによって生成される出力を参照してください[設定履歴の表示](view-manage-resource-console.md#get-config-history-cli)。  
定期的な評価のためにイベントが発行される場合、値は JSON オブジェクトを含む文字列です。オブジェクトには、トリガーされた評価に関する情報が含まれています。  
それぞれのタイプのイベントで、次の Node.js 例に示すように、関数は JSON パーサーで文字列を解析して文字列の内容を評価する必要があります。  

```
var invokingEvent = JSON.parse(event.invokingEvent);
```

`ruleParameters`  
評価ロジックの一部として関数が処理するキー/値ペア。 AWS Config コンソールを使用してカスタム Lambda ルールを作成するときにパラメータを定義します。`PutConfigRule` AWS Config API リクエストまたは `put-config-rule` AWS CLI コマンドの `InputParameters` 属性を使用してパラメータを定義することもできます。  
パラメータの JSON コードが文字列に含まれているため、次の Node.js 例で示すように、関数は JSON パーサーで文字列を解析して文字列の内容を評価する必要があります。  

```
var ruleParameters = JSON.parse(event.ruleParameters);
```

`resultToken`  
関数が`PutEvaluations`呼び出し AWS Config で渡す必要があるトークン。

`eventLeftScope`  
評価する AWS リソースがルールのスコープから削除されたかどうかを示すブール値。値が `true` の場合、関数は、`PutEvaluations` 呼び出しの `ComplianceType` 属性の値として `NOT_APPLICABLE` を渡すことで、評価を無視できることを示します。

`executionRoleArn`  
が割り当てられている IAM ロールの ARN AWS Config。

`configRuleArn`  
がルールに AWS Config 割り当てた ARN。

`configRuleName`  
がイベントを発行し、 関数 AWS Config を呼び出す原因となったルールに割り当てた名前。

`configRuleId`  
がルールに AWS Config 割り当てた ID。

`accountId`  
ルールを所有 AWS アカウント する の ID。

`version`  
によって割り当てられたバージョン番号 AWS。が AWS Config イベントに属性を追加すると、 AWS バージョンは増加します。関数に必要な属性が特定のバージョン以上のイベントにのみある場合、その関数はこの属性の値を確認できます。  
 AWS Config イベントの現在のバージョンは 1.0 です。

# AWS Config カスタム Lambda ルールの削除済みリソースの管理
<a name="evaluate-config_develop-rules-delete"></a>

削除されたリソースをレポートするルールは、不必要なルール評価を避けるために、`NOT_APPLICABLE` の評価結果を返す必要があります。

リソースを削除すると、 は `ResourceDeleted`の `configurationItem`を使用して AWS Config を作成します`configurationItemStatus`。このメタデータを使用して、ルールによって削除されたリソースについてレポートされているかを確認できます。設定アイテムの詳細については、「[概念 \$1 設定項目](https://docs.aws.amazon.com/config/latest/developerguide/config-concepts.html#config-items.html)」を参照してください。

次のコードスニペットを含めて、削除されたリソースをチェックし、削除されたリソースについてレポート`NOT_APPLICABLE`する場合は、 AWS Config カスタム Lambda ルールの評価結果を に設定します。

------
#### [ Custom Lambda Rules (Node.js) ]

```
// Check whether the resource has been deleted. If the resource was deleted, then the evaluation returns not applicable.
function isApplicable(configurationItem, event) {
    checkDefined(configurationItem, 'configurationItem');
    checkDefined(event, 'event');
    const status = configurationItem.configurationItemStatus;
    const eventLeftScope = event.eventLeftScope;
    return (status === 'OK' || status === 'ResourceDiscovered') && eventLeftScope === false;
}
```

------
#### [ Custom Lambda Rules (Python) ]

```
# Check whether the resource has been deleted. If the resource was deleted, then the evaluation returns not applicable.
def is_applicable(configurationItem, event):
    try:
        check_defined(configurationItem, 'configurationItem')
        check_defined(event, 'event')
    except:
        return True
    status = configurationItem['configurationItemStatus']
    eventLeftScope = event['eventLeftScope']
    if status == 'ResourceDeleted':
        print("Resource Deleted, setting Compliance Status to NOT_APPLICABLE.")
    return (status == 'OK' or status == 'ResourceDiscovered') and not eventLeftScope
```

------

**注記**  
AWS Config マネージドルールと AWS Config カスタムポリシールールは、デフォルトでこの動作を処理します。  
Development Kit (RDK) と AWS Config Development AWS Config Kit Library (RDKlib) を使用して Python で AWS Config カスタム Lambd ルールを作成すると、インポートされた[評価者](https://github.com/awslabs/aws-config-rdklib/blob/master/rdklib/evaluator.py#L56)クラスがこの動作を確認します。RDK および RDKLib でルールを記述する方法については、「[RDK および RDKLib でルールを記述する](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_components.html#evaluate-config_components_logic)」を参照してください。