

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

# AWS CloudFormation テンプレートを使用して Amazon GuardDuty を条件付きで有効にする
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates"></a>

*Amazon Web Services、Ram Kandaswamy*

## 概要
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-summary"></a>

[AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)は、Infrastructure as Code (IaC) ツールであり、テンプレートベースのデプロイを通じて AWS リソースを管理するのに役立ちます。CloudFormation は通常、 AWS リソースの管理に使用されます。[Amazon GuardDuty](https://docs.aws.amazon.com/guardduty/latest/ug/what-is-guardduty.html) AWS のサービスなどの を有効にするために使用すると、固有の課題が発生する可能性があります。GuardDuty は、悪意のあるアクティビティや不正な動作 AWS アカウント について を継続的にモニタリングする脅威検出サービスです。複数回作成できる一般的なリソースとは異なり、GuardDuty はアカウントおよび ごとに 1 回有効にする必要があるサービスです AWS リージョン。従来の CloudFormation 条件は静的な値の比較のみをサポートしているため、GuardDuty などのサービスの現在の状態を確認することは困難です。既にアクティブなアカウントで CloudFormation を介して GuardDuty を有効にしようとすると、スタックのデプロイは失敗します。これにより、マルチアカウント環境を管理している DevOps チームに運用上の課題が生じる可能性があります。

このパターンは、この課題に対するソリューションを導入します。[AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 関数によってバックアップされた CloudFormation カスタムリソースを使用して、動的状態チェックを実行します。条件付きロジックは、GuardDuty がまだ有効になっていない場合にのみ有効にします。スタック出力を使用して、将来の参照用に GuardDuty ステータスを記録します。

このパターンに従うことで、クリーンで予測可能な CloudFormation スタックオペレーションを維持しながら、 AWS インフラストラクチャ全体の GuardDuty デプロイを自動化できます。このアプローチは、次のような組織にとって特に重要です。
+ IaC AWS アカウント による複数の の管理
+ セキュリティサービスを大規模に実装する
+ べき等インフラストラクチャのデプロイを要求する
+ セキュリティサービスのデプロイの自動化

## 前提条件と制限
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-prereqs"></a>

**前提条件**
+ アクティブな AWS アカウント
+ CloudFormation スタックを作成、更新、削除するアクセス許可を持つ AWS Identity and Access Management (IAM) ロール
+ AWS Command Line Interface (AWS CLI)、[インストール](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html#getting-started-install-instructions)および[設定](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)済み

**制限事項**

 AWS アカウント または で GuardDuty を手動で無効にした場合 AWS リージョン、このパターンではそのターゲットアカウントまたはリージョンの GuardDuty は有効になりません。

## アーキテクチャ
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-architecture"></a>

**ターゲットテクノロジースタック**

このパターンでは、Infrastructure as Code (IaC) に CloudFormation を使用しています。Lambda 関数に支えられた CloudFormation カスタムリソースを使用して、動的なサービス有効化機能を実現します。

**ターゲットアーキテクチャ**

以下の大まかなアーキテクチャ図は、CloudFormation テンプレートをデプロイして GuardDuty を有効にするプロセスを示しています。

![\[CloudFormation スタックを使用して AWS アカウントで GuardDuty を有効にします。\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/images/pattern-img/3abd7cb7-0937-41fe-8eaa-79aedb182732/images/71624052-eebc-474a-9aa3-8606d87fc51d.png)


1. CloudFormation テンプレートをデプロイして CloudFormation スタックを作成します。

1. スタックは IAM ロールと Lambda 関数を作成します。

1. Lambda 関数は IAM ロールを引き受けます。

1. ターゲットで GuardDuty がまだ有効になっていない場合 AWS アカウント、Lambda 関数はそれを有効にします。

**自動化とスケール**

 AWS CloudFormation StackSet 機能を使用して、このソリューションを複数の AWS アカウント と に拡張できます AWS リージョン。詳細については、CloudFormation ドキュメントの[AWS CloudFormation StackSets の使用](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html)」を参照してください。

## ツール
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-tools"></a>
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) は、コマンドラインシェルのコマンド AWS のサービス を通じて を操作するのに役立つオープンソースツールです。
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) は、 AWS リソースをセットアップし、迅速かつ一貫してプロビジョニングし、 AWS アカウント および リージョン全体のライフサイクルを通じてリソースを管理するのに役立ちます。
+ [Amazon GuardDuty](https://docs.aws.amazon.com/guardduty/latest/ug/what-is-guardduty.html) は、ログを分析して処理し、 AWS 環境内の予期しないアクティビティや不正なアクティビティの可能性を特定する継続的なセキュリティモニタリングサービスです。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) は、誰を認証し、誰に使用を許可するかを制御することで、 AWS リソースへのアクセスを安全に管理できます。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) は、サーバーのプロビジョニングや管理を行うことなくコードを実行できるコンピューティングサービスです。必要に応じてコードを実行し、自動的にスケーリングするため、課金は実際に使用したコンピューティング時間に対してのみ発生します。

## エピック
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-epics"></a>

### CloudFormation テンプレートを作成し、スタックをデプロイします。
<a name="create-the-cfnshort-template-and-deploy-the-stack"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| コードを Amazon S3 に保存します。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates.html) | AWS DevOps | 
| CloudFormation テンプレートを作成します。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates.html) | AWS DevOps | 
| CloudFormation スタックを更新する。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates.html) | AWS DevOps | 
|  AWS アカウントで GuardDuty が有効になっていることを確認します。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates.html) | クラウド管理者、AWS 管理者 | 
| 追加のアカウントまたはリージョンを設定する。 | ユースケースに応じて、CloudFormation StackSet 機能を使用して、このソリューションを複数の AWS アカウント と に拡張します AWS リージョン。詳細については、CloudFormation ドキュメントの[AWS CloudFormation StackSets の使用](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html)」を参照してください。 | クラウド管理者、AWS 管理者 | 

## 関連リソース
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-resources"></a>

**リファレンス**
+ [AWS CloudFormation ドキュメント](https://docs.aws.amazon.com/cloudformation/index.html)
+ [AWS Lambda リソースタイプのリファレンス](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/AWS_Lambda.html)
+ [CloudFormation リソースタイプ: AWS:IAM::Role](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html)
+ [CloudFormation リソースタイプ: AWS:GuardDuty::Detector](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-guardduty-detector.html)
+ [を使用して AWS サービスプロパティを取得する 4 つの方法 AWS CloudFormation](https://aws.amazon.com/blogs/mt/four-ways-to-retrieve-any-aws-service-property-using-aws-cloudformation-part-1/) (ブログ記事)

**チュートリアルと動画**
+ [Simplify Your Infrastructure Management Using AWS CloudFormation](https://www.youtube.com/watch?v=1h-GPXQrLZw) (チュートリアル)
+ [Amazon GuardDuty と AWS Security Hub CSPM を使用して複数のアカウントを保護する](https://www.youtube.com/watch?v=Rg2ZzAAi1nY) (AWS re:Invent 2020)
+ [オーサリングのベストプラクティス AWS CloudFormation](https://www.youtube.com/watch?v=bJHHQM7GGro) (AWS re:Invent 2019)
+ [での脅威検出 AWS: Amazon GuardDuty の概要](https://www.youtube.com/watch?v=czsuZXQvD8E) (AWS re:Inforce 2019)

## 追加情報
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-additional"></a>

**Python コード**

```
import boto3
import os
import json
from botocore.exceptions import ClientError
import cfnresponse
guardduty=boto3.client('guardduty')
cfn=boto3.client('cloudformation')
def lambda_handler(event, context):
    print('Event: ', event)
    if 'RequestType' in event:    
      if event['RequestType'] in ["Create","Update"]:
          enabled=False
          try:  
            response=guardduty.list_detectors()
            if "DetectorIds" in response and len(response["DetectorIds"])>0:
              enabled="AlreadyEnabled"
            elif "DetectorIds" in response and len(response["DetectorIds"])==0:
              cfn_response=cfn.create_stack(
                StackName='guardduty-cfn-stack',
                TemplateBody='{ "AWSTemplateFormatVersion": "2010-09-09",    "Description": "Guard duty creation template",    "Resources": { "IRWorkshopGuardDutyDetector": {  "Type": "AWS::GuardDuty::Detector",    "Properties": {   "Enable": true  }   } } }'
                )
              enabled="True"
          except Exception as e:
              print("Exception: ",e)
          responseData = {}
          responseData['status'] = enabled
          cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, "CustomResourcePhysicalID" )
      elif event['RequestType'] == "Delete":
          cfn_response=cfn.delete_stack(
                  StackName='guardduty-cfn-stack')
          cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
```

