LoginSignup
4
7

More than 3 years have passed since last update.

AWSで発生したイベントを AWS Chatbot + Lambda で Slack にお手軽に通知する - ②EventBridge篇

Last updated at Posted at 2020-05-11

はじめに

前回の記事では、AWS上の システムやアプリケーションの健全性可用性追求するために、AWSアカウント上で発生したさまざまなイベントを、AWS ChatbotAWS Lambda を使って お手軽にSlackに通知する システムのデプロイ方法についてご説明しました。今回は本システムのうち、受信したイベントをAmazon SNSにルーティングする役割を担う、Amazon EventBridgeの設定 についてご説明します。

Slack が受信した通知の例

デプロイ

以下のCloudFormationテンプレートを実行することで、 AWSで発生したイベントをSlackに通知するシステムをお手軽に実現 します。 詳細については、前回の記事 をご覧ください。

順序 内容 実行
1 セキュリティ関連サービスを有効化 するテンプレート (オプション) cloudformation-launch-stack
2 Slack通知を有効化 するテンプレート cloudformation-launch-stack

Amazon EventBridge

AWSサービスによっては、そのサービス内で発生した事象に関するメッセージを、指定したAmazon SNSトピックに直接送信することができます。この場合、それぞれのAWSサービス内の設定にて、送信先のAmazon SNSトピックを指定します。

一方で、AWSサービスによっては、指定したAmazon SNSトピックに直接メッセージを送信するのではなく、まず Amazon EventBridge にイベントを送信し、Amazon EventBridge がこのイベントを Amazon SNS にルーティング するというパターンも存在します。この場合は、Amazon EventBridge にて、どんな形式のどのデータどのサービスにルーテインングするか を指定する必要があります。(= つまり設定によって、イベントをフィルタリングしたり、ルーティング先のサービスを自由に選択できます。)

Amazon EventBridgeに対してイベントを送信するAWSサービスとそのイベントの詳細については、サポートされている AWS サービスからの EventBridge イベントの例 - Amazon EventBridge 内に掲載されています。

IAM Access Analyzer

以下の条件を満たすイベントをAmazon SNSにルーティングします。

source detail-type 通知内容とタイミング
aws.access-analyzer Access Analyzer Finding 結果の生成時、既存の結果の状態の変更時、および結果の削除時

なお、IAM Access Analyzer の初期設定については、「セキュアで堅牢なAWSアカウント」を実現する CloudFormationテンプレート - IAM Access Analyzer もあわせてご覧ください。

Resources:
  EventBridgeForAccessAnalyzer:    
    Type: AWS::Events::Rule
    Properties: 
      Description: Rule for AccessAnalyzer.
      EventPattern:
        source:
           - aws.access-analyzer
        detail-type:
           - Access Analyzer Finding
      Name: AccessAnalyzer
      State: ENABLED
      Targets: 
        - Arn: !Ref SnsTopicARN
          Id: SNS

AWS Security Hub

以下の条件を満たすイベントをAmazon SNSにルーティングします。なお、Security Hub Findings - Imported に関しては、準拠から非準拠となった状態のイベントのみ を受信して、Amazon SNS に送信します。

source detail-type 通知内容とタイミング
aws.securityhub Security Hub Findings - Imported 全ての結果 を BatchImportFindings, BatchUpdateFindings 実行時

なお、AWS Security Hub の初期設定については、「セキュアで堅牢なAWSアカウント」を実現する CloudFormationテンプレート - AWS Security Hub もあわせてご覧ください。

Resources:
  CloudWatchEventsForSecurityHubFindings:
    Type: 'AWS::Events::Rule'
    Properties: 
      Description: Rule for SecurityHub Findings.
      EventPattern:
        source:
          - aws.securityhub
        detail-type: 
          - Security Hub Findings - Imported
        detail:
          findings:
            Compliance:
              Status:
                - PASSED
            RecordState:
              - ARCHIVED
      Name: SecurityHub-Findings
      State: ENABLED
      Targets:
        - Arn: !Ref SnsTopicARN
          Id: CloudWatchEventsForSecurityHub
  CloudWatchEventsForSecurityHubInsightResults:
    Type: 'AWS::Events::Rule'
    Properties: 
      Description: Rule for SecurityHub Insight Results.
      EventPattern:
        source:
          - aws.securityhub
        detail-type: 
          - Security Hub Insight Results
      Name: SecurityHub-InsightResults
      State: ENABLED
      Targets:
        - Arn: !Ref SnsTopicARN
          Id: CloudWatchEventsForSecurityHub

Amazon GuardDuty

以下の条件を満たすイベントをAmazon SNSにルーティングします。

source detail-type 通知内容とタイミング
aws.guardduty GuardDuty Finding 結果のモニタリング を参照

なお、Amazon GuardDuty の初期設定については、「セキュアで堅牢なAWSアカウント」を実現する CloudFormationテンプレート - Amazon Inspector もあわせてご覧ください。

Resources:
  CloudWatchEventsForGuardDuty:
    Type: 'AWS::Events::Rule'
    Properties: 
      Description: Rule for GuardDuty.
      EventPattern:
        source:
          - aws.guardduty
        detail-type: 
          - GuardDuty Finding
      Name: GuardDuty
      State: ENABLED
      Targets:
        - Arn: !Ref SnsTopicARN
          Id: CloudWatchEventsForGuardDuty

Scheduled Events

以下の条件を満たすイベントをAmazon SNSにルーティングします。

source detail-type 通知内容とタイミング
aws.events Scheduled Event 予定されていたイベントの実行時
Resources:
  EventBridgeForScheduledEvents:
    Type: 'AWS::Events::Rule'
    Properties: 
      Description: Rule for Scheduled Events.
      EventPattern:
        source:
          - aws.events
        detail-type: 
          - Scheduled Event
      Name: ScheduledEvents
      State: !Ref ScheduledEventsRule
      Targets:
        - Arn: !Ref SNSForAlertArn
          Id: SNSForAlert

Amazon EBS Events

以下の条件を満たすイベントをAmazon SNSにルーティングします。

source detail-type 通知内容とタイミング
aws.ec2 EBS Volume Notification ボリュームの作成/変更/削除/アタッチ/デタッチ時
aws.ec2 EBS Snapshot Notification スナップショットの作成/コピー/共有時
aws.ec2 EBS Multi-Volume Snapshots Completion Status マルチボリュームスナップショット終了時
aws.ec2 EBS Fast Snapshot Restore State-change Notification 高速スナップショット復元終了時
Resources:
  EventBridgeForEBS:
    Type: 'AWS::Events::Rule'
    Properties: 
      Description: Rule for EBS.
      EventPattern:
        source:
          - aws.ec2
        detail-type: 
          - EBS Volume Notification
          - EBS Snapshot Notification
          - EBS Multi-Volume Snapshots Completion Status
          - EBS Fast Snapshot Restore State-change Notification
      Name: EBS
      State: ENABLED
      Targets:
        - Arn: !Ref SNSForAlertArn
          Id: SNSForAlert

Amazon EC2 Auto Scaling Events

以下の条件を満たすイベントをAmazon SNSにルーティングします。

source detail-type 通知内容とタイミング
aws.autoscaling EC2 Instance-launch Lifecycle Action ライフサイクルアクション
aws.autoscaling EC2 Instance Launch Successful インスタンスの起動に成功時
aws.autoscaling EC2 Instance Launch Unsuccessful インスタンスの起動に失敗時
aws.autoscaling EC2 Instance-terminate Lifecycle Action インスタンス削除のライフサイクルアクション
aws.autoscaling EC2 Instance Terminate Successful インスタンスの削除に成功時
aws.autoscaling EC2 Instance Terminate Unsuccessful インスタンスの削除に失敗時
Resources:
  EventBridgeForAutoScaling:
    Type: 'AWS::Events::Rule'
    Properties: 
      Description: !Sub Rule for AutoScaling.
      EventPattern:
        source:
          - aws.autoscaling
        detail-type: 
          - EC2 Instance-launch Lifecycle Action
          - EC2 Instance Launch Successful
          - EC2 Instance Launch Unsuccessful
          - EC2 Instance-terminate Lifecycle Action
          - EC2 Instance Terminate Successful
          - EC2 Instance Terminate Unsuccessful
      Name: AutoScaling
      State: ENABLED
      Targets:
        - Arn: !Ref SNSForAlertArn
          Id: SNSForAlert

Amazon EC2 State Change Events

以下の条件を満たすイベントをAmazon SNSにルーティングします。

source detail-type 通知内容とタイミング
aws.ec2 EC2 Instance State-change Notification インスタンスの状態が変化した時
Resources:
  EventBridgeForEC2:
    Type: 'AWS::Events::Rule'
    Properties: 
      Description: !Sub Rule for EC2.
      EventPattern:
        source:
          - aws.ec2
        detail-type: 
          - EC2 Instance State-change Notification
      Name: EC2
      State: ENABLED
      Targets:
        - Arn: !Ref SNSForAlertArn
          Id: SNSForAlert

AWS KMS Events

以下の条件を満たすイベントをAmazon SNSにルーティングします。

source detail-type 通知内容とタイミング
aws.kms KMS CMK Rotations CMKキーデータの自動ローテーション時
aws.kms KMS Imported Key Material Expiration CMK期限切れキーマテリアルの削除時
aws.kms KMS CMK Deletion スケジュールによってCMKが削除された時
Resources:
  EventBridgeForKMS:
    Type: 'AWS::Events::Rule'
    Properties: 
      Description: !Sub Rule for KMS.
      EventPattern:
        source:
          - aws.kms
        detail-type: 
          - KMS CMK Rotations
          - KMS Imported Key Material Expiration
          - KMS CMK Deletion
      Name: KMS
      State: ENABLED
      Targets:
        - Arn: !Ref SNSForAlertArn
          Id: SNSForAlert

AWS Management Console Sign-in Events

以下の条件を満たすイベントをAmazon SNSにルーティングします。

source detail-type 通知内容とタイミング
aws.signin AWS Console Sign In via CloudTrail マネジメントコンソール サインイン時
  EventBridgeForManagementConsole:
    Type: 'AWS::Events::Rule'
    Properties: 
      Description: !Sub Rule for Management Console.
      EventPattern:
        source:
          - aws.signin
        detail-type: 
          - AWS Console Sign In via CloudTrail
      Name: ManagementConsole
      State: ENABLED
      Targets:
        - Arn: !Ref SNSForAlertArn
          Id: SNSForAlert

Tag Change Events on AWS Resources

以下の条件を満たすイベントをAmazon SNSにルーティングします。

source detail-type 通知内容とタイミング
aws.tag Tag Change on Resource リソースタグ変更時
  EventBridgeForTag:
    Type: 'AWS::Events::Rule'
    Properties: 
      Description: !Sub Rule for Tag.
      EventPattern:
        source:
          - aws.tag
        detail-type: 
          - Tag Change on Resource
      Name: Tag
      State: ENABLED
      Targets:
        - Arn: !Ref SNSForAlertArn
          Id: SNSForAlert

AWS Trusted Advisor Events

以下の条件を満たすイベントをAmazon SNSにルーティングします。

source detail-type 通知内容とタイミング
aws.trustedadvisor Trusted Advisor Check Item Refresh Notification Trusted Advisor チェックのステータス変更時
  EventBridgeForTrustedAdvisor:
    Type: 'AWS::Events::Rule'
    Properties: 
      Description: Rule for Trusted Advisor.
      EventPattern:
        source:
          - aws.trustedadvisor
        detail-type: 
          - Trusted Advisor Check Item Refresh Notification
      Name: TrustedAdvisor
      State: ENABLED
      Targets:
        - Arn: !Ref SNSForAlertArn
          Id: SNSForAlert

AWS SAM テンプレート

上記のYAMLコードは、aws-cloudformation-templates/notification - GitHubで公開しているCloudFormationテンプレートから、本記事用に一部抜粋したものです。本記事作成用に一部改変を行なっていること、またリポジトリの最新のコードを常に反映している訳ではないことをご了承ください。詳細は、GitHubの該当リポジトリをご覧ください。

また、今回ご紹介したEventBridgeのルーティングの設定は、eventbridge-rules - AWS Serverless Application Repositoryから直接デプロイすることも可能です。

関連リンク

  1. 準備篇 - AWSで発生したイベントを AWS Chatbot + Lambda で Slack にお手軽に通知する
  2. EventBridge篇 - AWSで発生したイベントを AWS Chatbot + Lambda で Slack にお手軽に通知する
4
7
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
4
7