TerraformでAmazon Detectiveを有効化してみた

TerraformでAmazon Detectiveを有効化してみた

Clock Icon2025.06.12

こんにちは、ゲームソリューション部のsoraです。
今回は、TerraformでAmazon Detectiveを有効化してみたことについて書いていきます。

Amazon Detectiveとは何かについては本ブログでは割愛します。
https://docs.aws.amazon.com/ja_jp/detective/latest/userguide/what-is-detective.html
https://pages.awscloud.com/rs/112-TZM-766/images/AWS-Black-Belt_2025_Amazon-Detective_0131_v1.pdf

Detectiveの有効化(グラフの作成)

Amazon Detectiveは、グラフを作成することで有効化されます。
tagなども付けることは可能ですが必須パラメータはないため、resourceの中身は空で作成できます。

### provider ###
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "6.0.0-beta3"
    }
  }
}
# AWSプロバイダーの定義
provider "aws" {
  region = "ap-northeast-1"
}

### Resource ###
# 実行可能なアカウント:任意アカウント
## Detectiveグラフの作成(Detectiveの基本単位)
## 各アカウントで各リージョンごとに1つのグラフのみ作成可能
resource "aws_detective_graph" "main" {}

sr-detective-tf-01

Detectiveの管理権限の委任

ここからはOrganizationsを利用している場合の機能を試していきます。
まずは、Organizations管理アカウントからメンバーアカウントへDetectiveの管理権限を委任してみます。

ちなみに、委任先のアカウントにてDetectiveが有効化されていない場合は有効化されます。
また、この委任部分をterraform destroyすると委任先のアカウントのDetectiveも無効化されました。

# 実行可能なアカウント:Organizations管理者アカウント(Detective管理者アカウントではない)
## Detectiveの管理者を委任
resource "aws_detective_organization_admin_account" "admin" {
  account_id = "xxxxxxxxxxxx" # 管理者として設定したいAWSアカウントID
}

メンバーアカウント追加時の自動設定・メンバーアカウントの追加

次に、Detective管理者アカウントにて、メンバーアカウント追加時の設定とメンバーアカウントの追加をやってみます。

ちなみに、招待するメンバーアカウントが招待元アカウントのOrganizations配下にある場合は、auto_enable=falseであっても自動で有効化されました。

# 実行可能なアカウント:Detective管理者アカウント
## Organizationの設定
resource "aws_detective_organization_configuration" "org_config" {
  # 新しいアカウントをDetective管理者のメンバーアカウントとして自動的で追加
  auto_enable = true
  graph_arn   = aws_detective_graph.main.id
}
## メンバーアカウントをDetectiveへ追加
resource "aws_detective_member" "member_account" {
  graph_arn                  = aws_detective_graph.main.id
  # メンバーとして追加したいアカウントID
  account_id                 = "xxxxxxxxxxxx"
  # Detectiveへの招待通知先メールアドレス
  email_address              = "xxxxx@xxxxx.xxx"
  message                    = "Detectiveメンバーの招待"
  # メールでの招待通知を無効にしない(trueだと招待通知がされない)
  disable_email_notification = false
}
  • 自動有効化
    sr-detective-tf-02

  • 管理者アカウントでの表示
    sr-detective-tf-03

  • メンバーアカウントでの表示
    sr-detective-tf-04

招待を受けたメンバーアカウントでは、以下で招待を受諾することが可能です。

# 実行可能なアカウント:招待を受けたメンバアカウント
## メンバーアカウントでの招待受諾
resource "aws_detective_invitation_accepter" "member" {
  graph_arn = aws_detective_graph.main.id
}

参考

今回使用したコードまとめを記載します。

管理者側
### provider ###
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "6.0.0-beta3"
    }
  }
}
# AWSプロバイダーの定義
provider "aws" {
  region = "ap-northeast-1"
}

### Resource ###
# 実行可能なアカウント:任意アカウント
## Detectiveグラフの作成(Detectiveの基本単位)
## 各アカウントで1つのグラフのみ作成可能
resource "aws_detective_graph" "main" {}

# 実行可能なアカウント:Organizations管理者アカウント(Detective管理者アカウントではない)
## Detectiveの管理者を委任
resource "aws_detective_organization_admin_account" "admin" {
  account_id = "xxxxxxxxxxxx" # 管理者として設定したいAWSアカウントID
}

# 実行可能なアカウント:Detective管理者アカウント
## Organizationの設定
resource "aws_detective_organization_configuration" "org_config" {
  # 新しいアカウントをDetective管理者のメンバーアカウントとして自動的で追加
  auto_enable = true
  graph_arn   = aws_detective_graph.main.id
}
## メンバーアカウントをDetectiveへ追加
resource "aws_detective_member" "member_account" {
  graph_arn                  = aws_detective_graph.main.id
  # メンバーとして追加したいアカウントID
  account_id                 = "xxxxxxxxxxxx"
  # Detectiveへの招待通知先メールアドレス
  email_address              = "xxxxx@xxxxx.xxx"
  message                    = "Detectiveメンバーの招待"
  # メールでの招待通知を無効にしない(trueだと招待通知がされない)
  disable_email_notification = false
}
メンバー側(招待を受ける側)
### provider ###
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "6.0.0-beta3"
    }
  }
}
# AWSプロバイダーの定義
provider "aws" {
  region = "ap-northeast-1"
}

### Resource ###
# 実行可能なアカウント:招待を受けたメンバアカウント
## メンバーアカウントでの招待受諾
resource "aws_detective_invitation_accepter" "member" {
  graph_arn = aws_detective_graph.main.id
}

最後に

今回は、TerraformでAmazon Detectiveを有効化してみたことを記事にしました。
どなたかの参考になると幸いです。

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.