LoginSignup
2
0

More than 1 year has passed since last update.

EventBridgeのインプットトランスフォーマーを使ってLambdaを使わずに通知メールを見やすくする

Last updated at Posted at 2022-03-13

はじめに

EventBridge(旧CloudWatchEvents)のイベント通知をSNSで通知したはいいものの、
もはやなんのメールかわからないなんてことはありませんか?
メールの内容をカスタマイズするために、Lambdaでコードをガリガリかくなんてこともあったりしますよね。
今回は、GuadDutyのイベント通知を例に、EventBridgeのインプットトランスフォーマーという機能を利用し、コード作成不要で通知を見やすくする方法をご紹介します。

EventBridgeとは?

https://aws.amazon.com/jp/eventbridge/
上記URLより以下の説明が記載されています。簡単にまとめると、様々なソースから、イベントを受け取り、ターゲットとなるアプリケーションやAWSのサービスにデータを配信するサービスです。

「Amazon EventBridge はサーバーレスイベントバスであり、アプリケーション、統合された Software-as-a-Service (SaaS) アプリケーション、および AWS のサービスから生成されたイベントを使用して、イベント駆動型アプリケーションを大規模に構築することを容易にします。EventBridge は、Zendesk や Shopify などのイベントソースから AWS Lambda やその他の SaaS アプリケーションなどのターゲットにリアルタイムデータのストリームを配信します。ルーティングルールを設定して、データの送信先を決定し、イベントパブリッシャーとコンシューマーが完全に疎結合化された状態でデータソースにリアルタイムで反応するアプリケーションアーキテクチャを構築できます。」

今回の構成

以下のような構成を取ります。
GuardDutyを有効化し、EventBridgeのルールを設定、GuardDutyの検知の際に、SNSをターゲットとし、
Eメール通知を飛ばします。
スクリーンショット 2022-03-13 10.39.36.png
GuardDutyってなんぞやって方は以下を見てください。

通知設定

まずは、特に何もせずに、素の状態で設定をしてみます。
EventBridgeの画面から、以下のようにGuardDutyFindingをイベントタイプとするようなイベントパターンで
ルールを作成します。ターゲットにはSNSのトピックを指定します。
スクリーンショット 2022-03-09 135617.png
最近イベントパターンがこんな感じでテストできるようになったようです。便利ですね。
スクリーンショット 2022-03-09 135633.png
ちなみに、GuardDutyにはシビリティが定義されており、特定のシビリティを通知したい場合は以下のような指定を行います。「severity」で通知対象を選択します。

{
  "source": [
    "aws.guardduty"
  ],
  "detail-type": [
    "GuardDuty Finding"
  ],
  "detail": {
    "severity": [
      4,
      4.0,
      4.1,
      4.2,
      4.3,
      4.4,
      4.5,
      4.6,
      4.7,
      4.8,
      4.9,
      5,
      5.0,
      5.1,
      5.2,
      5.3,
      5.4,
      5.5,
      5.6,
      5.7,
      5.8,
      5.9,
      6,
      6.0,
      6.1,
      6.2,
      6.3,
      6.4,
      6.5,
      6.6,
      6.7,
      6.8,
      6.9,
      7,
      7.0,
      7.1,
      7.2,
      7.3,
      7.4,
      7.5,
      7.6,
      7.7,
      7.8,
      7.9,
      8,
      8.0,
      8.1,
      8.2,
      8.3,
      8.4,
      8.5,
      8.6,
      8.7,
      8.8,
      8.9
    ]
  }
}

素の通知

上記の設定を実施した場合、以下のような通知が飛びます。
1行でJSON....ナンジャコリャってなりますよね

{"version":"0","id":"XXXX","detail-type":"GuardDuty Finding","source":"aws.guardduty","account":"XXXXXX","time":"20XX-XX-XXTXX:XX:XXZ","region":"ab-northeast-1","resources":[],"detail":{"schemaVersion":"2.0","accountId":"XXX","region":"ap-northeast-1","partition":"aws","id":"XXXX","arn":"arn:aws:guardduty:ap-northeast-1:XXXXXX:detector/XXXXXX/finding/XXXXXXXX","type":"Stealth:IAMUser/CloudTrailLoggingDisabled","resource":{"resourceType":"AccessKey","accessKeyDetails":{"accessKeyId":"XXXXXXXX","principalId":"XXXXXXXX","userType":"IAMUser","userName":"XXXXXXX"}},"service":{"serviceName":"guardduty","detectorId":"XXXXXX","action":{"actionType":"AWS_API_CALL","awsApiCallAction":{"api":"DeleteTrail","serviceName":"cloudtrail.amazonaws.com","callerType":"Remote IP","remoteIpDetails":{"ipAddressV4":"X.X.X.X","organization":{"asn":"XXXX","asnOrg":"XXXX","isp":"XXX","org":"XXXX"},"country":{"countryName":"Japan"},"city":{"cityName":"XXX"},"geoLocation":{"lat":XXX,"lon":XXX}},"affectedResources":{"AWS::CloudTrail::Trail":"arn:aws:cloudtrail:ap-northeast-1:XXXXX:trail/XXX"}}},"resourceRole":"TARGET","additionalInfo":{},"evidence":null,"eventFirstSeen":"20XX-XX-XXTXX:XX:XXZ","eventLastSeen":"20XX-XX-XXTXX:XX:XXZ","archived":false,"count":X},"severity":2,"createdAt":"20XX-XX-XXTXX:XX:XX.XXXZ","updatedAt":"20XX-XX-XXTXX:XX:XX.XXXZ","title":"AWS CloudTrail trail arn:aws:cloudtrail:ap-northeast-1:XXXXX:trail/XXXXXX was disabled.","description":"AWS CloudTrail trail arn:aws:cloudtrail:ap-northeast-1:XXXXXXX:trail/XXXXX was disabled by XXXXX calling DeleteTrail under unusual circumstances. This can be attackers attempt to cover their tracks by eliminating any trace of activity performed while they accessed your account."}}

--
If you wish to stop receiving notifications from this topic, please click or visit the link below to unsubscribe:
XXXXXX

Please do not reply directly to this email. If you have any questions or comments regarding this email, please contact us at XXXX

インプットトランスフォーマーを使う

ではこのメールを見やすくしていきましょう。
インプットトランスフォーマーを利用すると、イベントから取得したテキストを、ルールのターゲットに入力する前にカスタマイズが可能です。EventBridgeのルールを編集しターゲットの入力で、入力トランスフォーマーを選択し、
以下の設定をします。
インプットトランスフォーマーでは、まず、イベントのJSON出力を変数に割り当てます。
以下の例では、GuadDutyのdetail.accountidという出力をAccount_IDという変数に格納しています。
※その他も同様。
変数定義後、通知する文面を変数を組み込み作成します。
以下の例では、AWSアカウントのIDとシビリティ等を簡易的に通知する文面にしています。
変数は、”<>”で囲みます。
スクリーンショット 2022-03-16 9.41.51.png

[変数定義]

{"Account_ID":"$.detail.accountId","Finding_ID":"$.detail.id","Finding_Type":"$.detail.type","Finding_description":"$.detail.description","region":"$.region","severity":"$.detail.severity"}

[文面定義]

"AWS <Account_ID> has a severity <severity> GuardDuty finding type <Finding_Type> in the <region> region."
"Finding Description:"
"<Finding_description>. "
"For more details open the GuardDuty console at https://console.aws.amazon.com/guardduty/home?region=<region>#/findings?search=id=<Finding_ID>"

[変更後の通知メール]

"AWS XXXX has a severity 2 GuardDuty finding type Stealth:IAMUser/CloudTrailLoggingDisabled in the ap-northeast-1 region."
"Finding Description:"
"AWS CloudTrail trail XXXXX was disabled by XXXXX calling DeleteTrail under unusual circumstances. This can be attackers attempt to cover their tracks by eliminating any trace of activity performed while they accessed your account.. "
"For more details open the GuardDuty console at XXXXXX
                            

--
If you wish to stop receiving notifications from this topic, please click or visit the link below to unsubscribe:
XXXX

Please do not reply directly to this email. If you have any questions or comments regarding this email, please contact us at XXXXXX                

だいぶ読める様になりましたね。

まとめ

インプットトランスフォーマーを利用すると、EventBridgeからの通知を格段に見やすくすることができます。
件名のカスタマイズなどの要件がなければ、Lambda不要で、通知文面をカスタマイズすることができるのでおすすめです。EC2の停止起動通知など、GuadDutyに限らず、様々なものに応用できるので、ぜひ活用してみてください。

参照

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0