AWS CDK で Amazon Detective を有効化してみた

AWS CDK で Amazon Detective を有効化してみた

Clock Icon2025.03.20

こんにちは、製造ビジネステクノロジー部の若槻です。

Amazon Detective は、AWS のリソースのデータを自動的に収集して、ビジュアライズや分析を行うサービスです。これにより、セキュリティインシデントの検出や調査を効率的に行うことが可能となります。

https://docs.aws.amazon.com/ja_jp/detective/latest/userguide/what-is-detective.html

Amazon Detective の動作イメージは下記のようになります。各データソースから Detective に収集されたデータがグラフ (Behavior Graph) に入力され、分析が行われます。

Detective が動作グラフに入力する方法 - Amazon Detective より引用

今回は、Amazon Detective の有効化設定を AWS CDK で行う方法を確認してみました。

確認してみた

Detective の有効化

Amazon Detective ではグラフを作成することによりサービスを有効化して利用を開始できます。

AWS CDK では CfnGraph コンストラクトクラスを使用してグラフを作成します。

lib/detective-construct.ts
import * as detective from "aws-cdk-lib/aws-detective";
import { Construct } from "constructs";

export class DetectiveConstruct extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    // グラフの作成
    new detective.CfnGraph(this, "Default");
  }
}

上記をデプロイして Detective を有効化した状態です。有効化してからしばらく経っているため IAM プリンシパルごとの API コールの履歴が蓄積されています。

マルチアカウント設定時の有効化例

検証用の環境がなく動作確認まではできていないのですが、Detective をマルチアカウントで設定する際の実装は下記のようになります。

import * as detective from "aws-cdk-lib/aws-detective";
import { Construct } from "constructs";

export class DetectiveConstruct extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    // グラフの作成
    const graph = new detective.CfnGraph(this, "Graph", {
      // 新規組織アカウントを自動でメンバーとして有効化する設定
      autoEnableMembers: true,
      // グラフに付与するタグの設定
      tags: [{ key: "Environment", value: "Production" }],
    });

    // メンバーの招待
    new detective.CfnMemberInvitation(this, "MemberInvitation", {
      // メンバーを招待する対象の動作グラフの ARN
      graphArn: graph.attrArn,
      // 招待する AWS アカウントの ID
      memberId: "123456789012",
      // 招待するアカウントのルートユーザーメールアドレス
      memberEmailAddress: "admin@example.com",
      // 招待メールに含めるカスタムメッセージ
      message: "Please join our Detective graph",
    });

    // 組織管理者の設定
    new detective.CfnOrganizationAdmin(this, "OrgAdmin", {
      // Detective 管理者として指定する AWS アカウント ID
      accountId: "123456789012",
    });
  }
}

ちなみに Amazon Detective は CloudFormation でできる設定が限られており、上記でほぼすべてとなります。

すでに Detective が有効化されている場合はエラーとなる

Detective グラフはアカウント/リージョンごとに 1 つしか作成できないため、すでに作成されている場合はエラーとなります。下記の CDK コードをデプロイしてみます。

lib/detective-construct.ts
import * as detective from "aws-cdk-lib/aws-detective";
import { Construct } from "constructs";

export class DetectiveConstruct extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    // Detective を有効化
    new detective.CfnGraph(this, "Default");

    // Detective を有効化 (重複)
    new detective.CfnGraph(this, "Duplicate");
  }
}

下記のようにデプロイが Only 1 graph is allowed per account というエラーで失敗します。

cdk deploy

✨  Synthesis time: 4.39s

Security: start: Building 56fe2b69991253f1b618a0be029a552c3b0111b8a2d9dbdb57eadedc1be630f2:300561038900-ap-northeast-1
Security: success: Built 56fe2b69991253f1b618a0be029a552c3b0111b8a2d9dbdb57eadedc1be630f2:300561038900-ap-northeast-1
Security: start: Publishing 56fe2b69991253f1b618a0be029a552c3b0111b8a2d9dbdb57eadedc1be630f2:300561038900-ap-northeast-1
Security: success: Published 56fe2b69991253f1b618a0be029a552c3b0111b8a2d9dbdb57eadedc1be630f2:300561038900-ap-northeast-1
Security: deploying... [1/1]
Security: updating stack...
Security |   0 | 9:25:34 PM | UPDATE_IN_PROGRESS   | AWS::CloudFormation::Stack            | Security User Initiated
Security |   0 | 9:25:39 PM | CREATE_IN_PROGRESS   | AWS::Detective::Graph                 | Detective/Duplicate (DetectiveDuplicate6EE00B27)
Security |   0 | 9:25:39 PM | CREATE_IN_PROGRESS   | AWS::Detective::Graph                 | Detective/Default (Detective)
Security |   0 | 9:25:40 PM | CREATE_FAILED        | AWS::Detective::Graph                 | Detective/Duplicate (DetectiveDuplicate6EE00B27) Resource handler returned message: "Only 1 graph is allowed per account" (RequestToken: 5003c22c-41d5-a353-de34-5d003a3890f4, HandlerErrorCode: AlreadyExists)

すでに Amazon Detective が有効化されたアカウントで設定を IaC 化したい場合は、一度 Detective を無効化してから CDK で再設定する必要があります。

おわりに

AWS CDK で Amazon Detective を有効化してみました。

Detective は最近まであまり知らないサービスだったのですが、コード 1 行の実装だけでセキュリティインシデントの調査を効率化できるのは非常にお得な感じがしますね。

参考

https://dev.classmethod.jp/articles/enable-detective-cfn-stacksets/

以上

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.