

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

# AWS CloudFormation Guard ポリシーを使用して AWS Config カスタムルールを作成する
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies"></a>

*Amazon Web Services、Andrew Lok、Nicole Brown、Kailash Havildar、および Tanya Howell*

## 概要
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-summary"></a>

[AWS Config](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html) ルールは、 AWS リソースとそのターゲット設定の状態を評価するのに役立ちます。ルールには、マネージドルールとカスタム AWS Config ルールの 2 種類があります。は、 AWS Lambda 関数または policy-as-code 言語である [AWS CloudFormation Guard](https://github.com/aws-cloudformation/cloudformation-guard) (GitHub) を使用してカスタムルールを作成できます。

Guard で作成されたルールは、マネージドルールに比べて詳細な制御ができて、通常、フルカスタムの Lambda ルールよりも設定が簡単です。このアプローチにより、エンジニアやアーキテクトは、Lambda を介してカスタムルールを展開するために必要な Python や NodeJS、Java の知識がなくてもルールを構築することができます。

このパターンでは、Guard でカスタムルールを採用するのに役立つ実用的なテンプレート、コードサンプル、およびデプロイの方法を提供します。このパターンを使用すると、管理者は を使用して AWS Config 、[設定項目](https://docs.aws.amazon.com/config/latest/developerguide/config-concepts.html#config-items)属性を持つカスタムコンプライアンスルールを構築できます。例えば、開発者は AWS Config 設定項目に対して Guard ポリシーを使用して、デプロイされたAWS リソース AWS と非リソースの状態を継続的にモニタリングし、ルール違反を検出して、修復を自動的に開始できます。

**目的**

このパターンを読み終えると、次のことができるようになります。
+ Guard ポリシーコードが AWS Config サービスとやり取りする方法を理解します。
+ *シナリオ 1* をデプロイします。シナリオ 1 は、Guard 構文を使用して暗号化されたボリュームのコンプライアンスを検証する AWS Config カスタムルールです。このルールは、ドライブが使用中であり、ドライブタイプが [gp3](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/general-purpose.html#gp3-ebs-volume-type) であることを確認します。
+ *シナリオ 2 *をデプロイします。シナリオ 2 は、Guard 構文を使用して Amazon GuardDuty のコンプライアンスを検証する AWS Config カスタムルールです。このルールは、GuardDuty レコーダーで [Amazon Simple Storage Service (Amazon S3) 保護](https://docs.aws.amazon.com/guardduty/latest/ug/s3-protection.html)と [Amazon Elastic Kubernetes Service (Amazon EKS) 保護](https://docs.aws.amazon.com/guardduty/latest/ug/kubernetes-protection.html)が有効になっていることを確認します。

## 前提条件と制限
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-prereqs"></a>

**前提条件**
+ アクティブな AWS アカウント
+ AWS Config、 で [をセットアップ](https://docs.aws.amazon.com/config/latest/developerguide/getting-started.html)する AWS アカウント

**制限事項**
+ Guard カスタムルールは、ターゲットの設定項目の JSON レコード内のキーと値のペアのみをクエリできます。

## アーキテクチャ
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-architecture"></a>

Guard 構文をカスタム policy として AWS Config ルールに適用します。 AWS Config は、指定された各リソースの階層 JSON を取得します。 AWS Config 設定項目の JSON には、キーと値のペアが含まれます。これらの属性は、対応する値に割り当てられた変数として Guard 構文で使用されます。 

以下で、Guard 構文について説明します。設定項目 JSON の変数が使用され、`%` 文字が先頭に付加されます。

```
# declare variable
let <variable name> = <'value'>

# create rule and assign condition and policy
    rule <rule name> when 
        <CI json key> == <"CI json value"> {
            <top level CI json key>.<next level CI json key> == %<variable name>
        }
```

**シナリオ 1: Amazon EBS ボリューム**

シナリオ 1 は、Guard 構文を使用して暗号化されたボリュームのコンプライアンスを検証する AWS Config カスタムルールをデプロイします。このルールは、ドライブが使用中であり、ドライブタイプが gp3 であることを確認します。

シナリオ 1 AWS Config の設定項目の例を次に示します。この設定項目には、、`volumestatus`、および の変数として を使用した 3 つのキー`volumeencryptionstatus`と値のペアがあります`volumetype`。また、`resourceType` キーは Guard ポリシーのフィルターとして使用されます。

```
{
  "version": "1.3",
  "accountId": "111111111111",
  "configurationItemCaptureTime": "2023-01-15T19:04:45.402Z",
  "configurationItemStatus": "ResourceDiscovered",
  "configurationStateId": "4444444444444",
  "configurationItemMD5Hash": "",
  "arn": "arn:aws:ec2:us-west-2:111111111111:volume/vol-222222222222",
  "resourceType": "AWS::EC2::Volume",
  "resourceId": "vol-222222222222",
  "awsRegion": "us-west-2",
  "availabilityZone": "us-west-2b",
  "resourceCreationTime": "2023-01-15T19:03:22.247Z",
  "tags": {},
  "relatedEvents": [],
  "relationships": [
    {
      "resourceType": "AWS::EC2::Instance",
      "resourceId": "i-33333333333333333",
      "relationshipName": "Is attached to Instance"
    }
  ],
  "configuration": {
    "attachments": [
      {
        "attachTime": "2023-01-15T19:03:22.000Z",
        "device": "/dev/xvda",
        "instanceId": "i-33333333333333333",
        "state": "attached",
        "volumeId": "vol-222222222222",
        "deleteOnTermination": true,
        "associatedResource": null,
        "instanceOwningService": null
      }
    ],
    "availabilityZone": "us-west-2b",
    "createTime": "2023-01-15T19:03:22.247Z",
    "encrypted": false,
    "kmsKeyId": null,
    "outpostArn": null,
    "size": 8,
    "snapshotId": "snap-55555555555555555",
    "state": "in-use",
    "volumeId": "vol-222222222222",
    "iops": 100,
    "tags": [],
    "volumeType": "gp2",
    "fastRestored": null,
    "multiAttachEnabled": false,
    "throughput": null,
    "sseType": null
  },
  "supplementaryConfiguration": {}
}
```

シナリオ 1 で Guard 構文を使用して変数とルールを定義する例を次に示します。以下の例で、次の操作を行います。
+ 最初の 3 行は、`let` コマンドを使用して変数を定義しています。  それらの変数には、設定項目の属性から派生した名前と値が割り当てられています。
+ `compliancecheck` ルールブロックは、`AWS::EC2::Volume` に一致する `resourceType` のキーと値のペアを検索する条件付き依存関係を追加します。一致が見つかった場合、ルールは残りの JSON 属性を続行し、`state`、`encrypted`、および `volumeType` の 3 つの条件で一致を検索します。

```
let volumestatus = 'available'
let volumetype = 'gp3'
let volumeencryptionstatus = true

    rule compliancecheck when 
        resourceType == "AWS::EC2::Volume" {
            configuration.state == %volumestatus
            configuration.encrypted == %volumeencryptionstatus
            configuration.volumeType == %volumetype
        }
```

このカスタムルールを実装する完全な Guard カスタムポリシーは、GitHub コードリポジトリの「[awsconfig-guard-cft.yaml](https://github.com/aws-samples/aws-config-custom-rule-cloudformation-guard/blob/main/awsconfig-guard-cft.yaml)」または「[awsconfig-guard-tf-ec2vol.json](https://github.com/aws-samples/aws-config-custom-rule-cloudformation-guard/blob/main/awsconfig-guard-tf-ec2vol.json)」を参照してください。このカスタムポリシーを Guard にデプロイする HashiCorp Terraform のコードは、コードリポジトリの「[awsconfig-guard-tf-example.json](https://github.com/aws-samples/aws-config-custom-rule-cloudformation-guard/blob/main/awsconfig-guard-tf-example.json)」を参照してください。

**シナリオ 2: GuardDuty コンプライアンス**

シナリオ 2 は、Guard 構文を使用して Amazon GuardDuty のコンプライアンスを検証する AWS Config カスタムルールをデプロイします。このルールは、GuardDuty レコーダーで Amazon S3 保護と Amazon EKS 保護が有効になっていることを確認します。また、GuardDuty の検出結果が 15 分ごとに公開されることを確認します。このシナリオは、組織全体 AWS アカウント および組織内 ( 内) AWS リージョン にデプロイできます AWS Organizations。

シナリオ 2 AWS Config の設定項目の例を次に示します。この設定項目には、ガードポリシーの変数として使用される `FindingPublishingFrequency`、`S3Logs`、および `Kubernetes` の 3 つのキーと値のペアがあります。また、`resourceType` キーはポリシーでフィルターとして使用されます。

```
{
  "version": "1.3",
  "accountId": "111111111111",
  "configurationItemCaptureTime": "2023-11-27T13:34:28.888Z",
  "configurationItemStatus": "OK",
  "configurationStateId": "7777777777777",
  "configurationItemMD5Hash": "",
  "arn": "arn:aws:guardduty:us-west-2:111111111111:detector/66666666666666666666666666666666",
  "resourceType": "AWS::GuardDuty::Detector",
  "resourceId": "66666666666666666666666666666666",
  "resourceName": "66666666666666666666666666666666",
  "awsRegion": "us-west-2",
  "availabilityZone": "Regional",
  "resourceCreationTime": "2020-02-17T02:48:04.511Z",
  "tags": {},
  "relatedEvents": [],
  "relationships": [],
  "configuration": {
    "Enable": true,
    "FindingPublishingFrequency": "FIFTEEN_MINUTES",
    "DataSources": {
      "S3Logs": {
        "Enable": true
      },
      "Kubernetes": {
        "AuditLogs": {
          "Enable": true
        }
      }
    },
    
    "Id": "66666666666666666666666666666666",
    "Tags": []
  },
  "supplementaryConfiguration": {
    "CreatedAt": "2020-02-17T02:48:04.511Z"
  }
}
```

シナリオ 2 で Guard 構文を使用して変数とルールを定義する例を次に示します。以下の例で、次の操作を行います。
+ 最初の 3 行は、`let` コマンドを使用して変数を定義しています。  それらの変数には、設定項目の属性から派生した名前と値が割り当てられています。
+ `compliancecheck` ルールブロックは、`AWS::GuardDuty::Detector` に一致する `resourceType` のキーと値のペアを検索する条件付き依存関係を追加します。一致が見つかった場合、ルールは残りの JSON 属性を続行し、`S3Logs.Enable`、`Kubernetes.AuditLogs.Enable`、および `FindingPublishingFrequency` の 3 つの条件で一致を検索します。

```
let s3protection = true
let kubernetesprotection = true
let publishfrequency = 'FIFTEEN_MINUTES'

    rule compliancecheck when 
        resourceType == "AWS::GuardDuty::Detector" {
            configuration.DataSources.S3Logs.Enable == %s3protection
            configuration.DataSources.Kubernetes.AuditLogs.Enable == %kubernetesprotection
            configuration.FindingPublishingFrequency == %publishfrequency
        }
```

このカスタムルールを実装する完全な Guard カスタムポリシーは、GitHub コードリポジトリの「[awsconfig-guard-cft-gd.yaml](https://github.com/aws-samples/aws-config-custom-rule-cloudformation-guard/blob/main/awsconfig-guard-cft-gd.yaml)」を参照してください。このカスタムポリシーを Guard にデプロイする HashiCorp Terraform のコードは、コードリポジトリの「[awsconfig-guard-tf-gd.json](https://github.com/aws-samples/aws-config-custom-rule-cloudformation-guard/blob/main/awsconfig-guard-tf-gd.json)」を参照してください。

## ツール
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-tools"></a>

**AWS のサービス**
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) は、 AWS リソースをセットアップし、迅速かつ一貫してプロビジョニングし、 AWS アカウント および リージョン全体のライフサイクルを通じてリソースを管理するのに役立ちます。
+ [AWS Config](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html) は、 内のリソースの詳細ビュー AWS アカウント と設定方法を提供します。リソースがどのように相互に関連しているか、またそれらの構成が時間の経過とともにどのように変化したかを特定するのに役立ちます。

**その他のツール**
+ [HashiCorp Terraform](https://www.terraform.io/docs) は、コードを使用してクラウドインフラストラクチャとリソースをプロビジョニングして管理するのを支援する Infrastructure as Code (IaC) ツールです。

**コードリポジトリ**

このパターンのコードは GitHub の「[AWS Config with AWS CloudFormation Guard](https://github.com/aws-samples/aws-config-custom-rule-cloudformation-guard/tree/main)」リポジトリで入手可能です。このコードリポジトリには、このパターンで説明されている両方のシナリオのサンプルが含まれています。

## エピック
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-epics"></a>

### AWS Config カスタムルールの作成
<a name="creating-cc-custom-rules"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| (オプション) ルールに使うキーと値のペアを選択する。 | カスタム Guard ポリシーを定義する場合は、次の手順を完了します。シナリオ 1 または 2 のサンプルポリシーのいずれかを使用している場合は、これらの手順を省略します。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies.html) | AWS 管理者、セキュリティエンジニア | 
| カスタムルールを作成する。 | 以前に特定したキーと値のペアを使用するか、提供されているサンプル Guard ポリシーのいずれかを使用して、[AWS Config 「カスタムポリシールールの作成](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules_cfn-guard.html#create-cfn-guard-rule-console)」の手順に従ってカスタムルールを作成します。 | AWS 管理者、セキュリティエンジニア | 
| カスタムルールを検証する。 | カスタム Guard ルールを検証するには、次のいずれかを実行します。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies.html) | AWS 管理者、セキュリティエンジニア | 

## トラブルシューティング
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-troubleshooting"></a>


| 問題 | ソリューション | 
| --- | --- | 
| の外部で Guard ポリシーをテストする AWS Config | ユニットテストは、ローカルデバイスまたは IDE などの統合開発環境 (IDE) AWS Cloud9 で実行できます。ユニットテストを実施するには、次の手順を実行します。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies.html) | 
|  AWS Config カスタムルールのデバッグ | ガードポリシーで、`EnableDebugLogDelivery` 値を `true` に変更します。デフォルト値は `false` です。ログメッセージは Amazon CloudWatch に保存されます。 | 

## 関連リソース
<a name="create-aws-config-custom-rules-by-using-aws-cloudformation-guard-policies-resources"></a>

**AWS ドキュメント**
+ [AWS Config カスタムポリシールールの作成](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules_cfn-guard.html) (AWS Config ドキュメント)
+ [AWS CloudFormation Guard ルールの記述](https://docs.aws.amazon.com/cfn-guard/latest/ug/writing-rules.html) (ガードドキュメント)

**AWS ブログ投稿とワークショップ**
+ [Introducing AWS CloudFormation Guard 2.0](https://aws.amazon.com/blogs/mt/introducing-aws-cloudformation-guard-2-0/) (AWS ブログ記事)

**その他のリソース**
+ [AWS CloudFormation Guard](https://github.com/aws-cloudformation/cloudformation-guard) (GitHub)
+ [AWS CloudFormation Guard CLI ドキュメント](https://github.com/aws-cloudformation/cloudformation-guard#guard-cli) (GitHub)