EventBridgeの入力トランスフォーマーでSNSのメール通知内容を整形してみた

記事タイトルとURLをコピーする

CI部2課の山﨑です。

SNSのメール通知内容を整形する方法としてLambdaを利用するという方法がありますが、今回はEventBridgeの入力トランスフォーマーを利用して、SNSのメール通知内容を整形してみました docs.aws.amazon.com

想定した利用シーン

今回はSecurity Hubが検知した内容をメールで運用担当者に通知するという利用シーンを想定しました。以降はSNSとSecurity Hubはすでに構築済という前提でEventBridgeの設定に絞ってご紹介します。

f:id:swx-yamasaki:20210424171224p:plain

EventBridgeのルール作成

イベントパターン

Security Hubの通知の中でも重要度が高い内容のみを通知するようにイベントパターンをカスタムしました

f:id:swx-yamasaki:20210424171557p:plain

【今回利用したイベントパターン】

{
  "source": [
    "aws.securityhub"
  ],
  "detail-type": [
    "Security Hub Findings - Imported"
  ],
  "detail": {
    "findings": {
      "Severity": {
        "Label": [
          "CRITICAL",
          "HIGH"
        ]
      }
    }
  }
}

ターゲット

ターゲットにはSNSトピックを選択します。そして「入力の設定」で入力トランスフォーマーを選択します。

f:id:swx-yamasaki:20210424172903p:plain

入力パス

入力パスではEventBridgeに送られてきたJSONログイベントから特定の値を変数として抽出します。Security Hubでは以下のようなJSONログイベントが送られてきます。

{
  "version": "0",
  "id": "54c4169f-167d-4d41-0427-cf572ab50f96",
  "detail-type": "Security Hub Findings - Imported",
  "source": "aws.securityhub",
  "account": "123456789123",
  "time": "2021-04-07T19:03:24Z",
  "region": "ap-northeast-1",
  "resources": [
    "arn:aws:securityhub:ap-northeast-1::product/aws/securityhub/arn:aws:securityhub:ap-northeast-1:123456789123:subscription/aws-foundational-security-best-practices/v/1.0.0/IAM.6/finding/369e4182-3820-44db-aa82-1b2e6d10017e"
  ],
  "detail": {
    "findings": [
      {
        "ProductArn": "arn:aws:securityhub:ap-northeast-1::product/aws/securityhub",
        "Types": [
          "Software and Configuration Checks/Industry and Regulatory Standards/AWS-Foundational-Security-Best-Practices"
        ],
        "Description": "This AWS control checks whether your AWS account is enabled to use hardware multi-factor authentication (MFA) device to sign in with root credentials.",
        "SchemaVersion": "2018-10-08",
        "Compliance": {
          "Status": "FAILED"
        },
        "GeneratorId": "aws-foundational-security-best-practices/v/1.0.0/IAM.6",
        "FirstObservedAt": "2021-03-17T02:24:37.207Z",
        "CreatedAt": "2021-03-17T02:24:37.207Z",
        "RecordState": "ACTIVE",
        "Title": "IAM.6 Hardware MFA should be enabled for the root user",
        "Workflow": {
          "Status": "NEW"
        },
        "LastObservedAt": "2021-04-07T19:03:18.570Z",
        "Severity": {
          "Normalized": 90,
          "Label": "CRITICAL",
          "Product": 90,
          "Original": "CRITICAL"
        },
        "UpdatedAt": "2021-04-07T19:03:17.414Z",
        "FindingProviderFields": {
          "Types": [
            "Software and Configuration Checks/Industry and Regulatory Standards/AWS-Foundational-Security-Best-Practices"
          ],
          "Severity": {
            "Normalized": 90,
            "Label": "CRITICAL",
            "Product": 90,
            "Original": "CRITICAL"
          }
        },
        "WorkflowState": "NEW",
        "ProductFields": {
          "StandardsArn": "arn:aws:securityhub:::standards/aws-foundational-security-best-practices/v/1.0.0",
          "StandardsSubscriptionArn": "arn:aws:securityhub:ap-northeast-1:123456789123:subscription/aws-foundational-security-best-practices/v/1.0.0",
          "ControlId": "IAM.6",
          "RecommendationUrl": "https://docs.aws.amazon.com/console/securityhub/IAM.6/remediation",
          "RelatedAWSResources:0/name": "securityhub-root-account-hardware-mfa-enabled-15ae6cab",
          "RelatedAWSResources:0/type": "AWS::Config::ConfigRule",
          "StandardsControlArn": "arn:aws:securityhub:ap-northeast-1:123456789123:control/aws-foundational-security-best-practices/v/1.0.0/IAM.6",
          "aws/securityhub/ProductName": "Security Hub",
          "aws/securityhub/CompanyName": "AWS",
          "aws/securityhub/FindingId": "arn:aws:securityhub:ap-northeast-1::product/aws/securityhub/arn:aws:securityhub:ap-northeast-1:123456789123:subscription/aws-foundational-security-best-practices/v/1.0.0/IAM.6/finding/369e4182-3820-44db-aa82-1b2e6d10017e"
        },
        "AwsAccountId": "123456789123",
        "Id": "arn:aws:securityhub:ap-northeast-1:123456789123:subscription/aws-foundational-security-best-practices/v/1.0.0/IAM.6/finding/369e4182-3820-44db-aa82-1b2e6d10017e",
        "Remediation": {
          "Recommendation": {
            "Text": "For directions on how to fix this issue, please consult the AWS Security Hub Foundational Security Best Practices documentation.",
            "Url": "https://docs.aws.amazon.com/console/securityhub/IAM.6/remediation"
          }
        },
        "Resources": [
          {
            "Partition": "aws",
            "Type": "AwsAccount",
            "Region": "ap-northeast-1",
            "Id": "AWS::::Account:123456789123"
          }
        ]
      }
    ]
  }
}

例えばこの中からAccount IDを変数として抽出したければ入力パスは下記の記載となります。

{"Account":"$.account"}

今回は入力パスは以下としました。

{"Account":"$.account","Description":"$.detail.findings[0].Description","Id":"$.detail.findings[0].Id","Label":"$.detail.findings[0].Severity.Label","Time":"$.time"}

入力テンプレート

入力テンプレートでは実際にメール通知する文面を入力します。例えばAccount IDを通知したい場合は下記の記載となります。

"Account ID : <Account> "

今回は入力テンプレートは以下としました。

"Security Hubが重要度 <Label> の異常を検知しました"
"Account ID : <Account> "
"発生時間 : <Time> "
"検出内容 : <Description> "
"関連するリソース1 : <RelatedResource1>"
"関連するリソースID 1 : <ResourceId1>"
"関連するリソース2 : <RelatedResource2>"
"関連するリソースID 2 : <ResourceId2>"
"関連するリソース3 : <RelatedResource3>"
"関連するリソースID 3 : <ResourceId3>"
"関連するリソース4 : <RelatedResource4>"
"関連するリソースID 4 : <ResourceId4>"
"関連するリソース5 : <RelatedResource5>"
"関連するリソースID 5 : <ResourceId5>"
"検索ID : <Id> "
"Security Hubのコンソール画面の左メニュータグより「検出結果」を選択します。検索ウィンドウで「ID」を選択し、検索IDの値を入力して検索してください。"

通知内容の例

実際には以下のようなメール通知が届きます。

f:id:swx-yamasaki:20210424174326p:plain

まとめ

通知内容の細かいカスタマイズが必要な場合はLambdaの方が適しているかもしれませんが、今回のように簡単な通知文面であればEventBridgeの入力トランスフォーマーを使った方が圧倒的にラクなので是非利用してみてください

山﨑 翔平 (Shohei Yamasaki) 記事一覧はコチラ

2019/12〜2023/2までクラウドインテグレーション部でお客様のAWS導入支援を行っていました。現在はIE(インターナルエデュケーション)課にて採用周りのお手伝いや新卒/中途オンボーディングの業務をしています。2023 Japan AWS Top Engineers/2023 Japan AWS Ambassadors