AWS User Notifications + Amazon Q Developer in chat applications を使ってSlackにイベント通知する際のハマりポイントをまとめた

AWS User Notifications + Amazon Q Developer in chat applications を使ってSlackにイベント通知する際のハマりポイントをまとめた

Clock Icon2025.06.18

はじめに

みなさんこんにちは、クラウド事業本部コンサルティング部の浅野です。

SlackのチャンネルにAWS上のさまざまなイベントを通知したい時ありますよね。そんな時便利なのがAWS User Notifications + Amazon Q Developer in chat applications(旧 Chatbot)の組み合わせです。

AWS User NotificationsはAWSサービスからの通知をマネージドEventBridgeルールによって統合的に管理・配信してくれるサービスです。その通知先ターゲットとしてAWS Q Developer in chat applicationsで設定したSlackチャンネルが設定可能です。

最近ではUser Notificationsに関するリソースがCloudFormationで扱えるようになるアップデートもありました。

https://dev.classmethod.jp/articles/user-notifications-ga-cloudformation/

今回はCloudFormationを用いて以下構成を作成するまでに、自分なりにハマったところを記述してみました。

構成

2025-06-17-chatbot-user-notifications-slack-01

※この構成の場合あらかじめ対象のS3バケット設定にてEventBridgeへのイベント通知をONにしておく必要があります。

2025-06-17-chatbot-user-notifications-slack-02

Slackワークスペース認証

CloudFormationでSlackワークスペースとAmazon Q Developer in chat applicationsの連携はサポートされていないので、あらかじめコンソール画面でSlackワークスペースとの初期認証を完了させておく必要があります。

https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/TemplateReference/aws-resource-chatbot-slackchannelconfiguration.html

  1. マネジメントコンソールから「新しいクライアントを設定」を押下し、「Slack」を選択

2025-06-17-chatbot-user-notifications-slack-03

  1. ワークスペースとの連携を許可

2025-06-17-chatbot-user-notifications-slack-04

CloudFormation テンプレート

下記コード上にて「Slack ワークスペースID」, 「Slack チャンネルID」、「S3バケット名」を適宜入力していただき、スタックをデプロイすれば通知設定まで完了します。

※「us-east-1」リージョン上にスタックを作成する必要があります。

AWSTemplateFormatVersion: '2010-09-09'

Parameters:
  SlackWorkspaceId:
    Type: String
    Default: {Slack ワークスペースID}
    Description: SlackワークスペースのワークスペースID

  SlackChannelId:
    Type: String
    Default: {Slack チャンネルID}
    Description: 通知先のSlackチャンネルID

Resources:
  # Chatbot用のIAMロール
  ChatbotRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: ChatbotRole
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: chatbot.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/ReadOnlyAccess

  # Chatbotの設定
  ChatbotSlackConfiguration:
    Type: AWS::Chatbot::SlackChannelConfiguration
    Properties:
      ConfigurationName: health-event-notifications
      IamRoleArn: !GetAtt ChatbotRole.Arn
      SlackChannelId: !Ref SlackChannelId
      SlackWorkspaceId: !Ref SlackWorkspaceId
      GuardrailPolicies:
        - arn:aws:iam::aws:policy/ReadOnlyAccess

  # User Notificationsの設定

  # ハブ設定(必須)
  NotificationHub:
    Type: AWS::Notifications::NotificationHub
    Properties:
      Region: us-east-1

  # 構成設定
  NotificationConfiguration:
    Type: AWS::Notifications::NotificationConfiguration
    Properties:
      Name: HealthEventNotifyToSlack
      Description: Health Event Notify
      AggregationDuration: SHORT

  # EventBridgeRule作成
  NotificationEventRule:
    Type: AWS::Notifications::EventRule
    Properties:
      NotificationConfigurationArn: !Ref NotificationConfiguration
      Source: "aws.s3"
      EventType: "Object Created"
      EventPattern: |
        {
          "detail": {
            "bucket": {
              "name": ["{S3バケット名}"]
            }
          }
        }
      Regions: 
        - ap-northeast-1

  # 通知先設定
  NotificationChannelAssociation:
    Type: AWS::Notifications::ChannelAssociation
    Properties:
      Arn: !Ref ChatbotSlackConfiguration
      NotificationConfigurationArn: !Ref NotificationConfiguration

Outputs:
  ChatbotConfigurationArn:
    Value: !Ref ChatbotSlackConfiguration
  NotificationConfigurationArn:
    Value: !Ref NotificationConfiguration

通知結果

上記CloudFormationテンプレートで作成したリソースにてSlackに通知される内容は以下のような結果になりました。

今回は通知設定にて集約設定を5分に設定しているので実際にオブジェクトを作成してから配信まで7,8分程度のラグがありました。

2025-06-17-chatbot-user-notifications-slack-05

ハマりポイント

1. User Notifications Hubを設定する必要がある。

アカウントでUser Notificationsを一度も使用したことがない状態ではUser Notificationsでの通知を保存しておくHubが以下の画面のように有効になっていません。

2025-06-17-chatbot-user-notifications-slack-06

コンソール上からリージョンを選択して「保存して続行」を押しておくか、上記コードのようにCloudFormationテンプレート内で設定することも可能です。

ちなみにUser Notifications Hubが無効な状態で「AWS::Notifications::NotificationConfiguration」を作成しようとすると以下エラーが発生します。

2025-06-17-chatbot-user-notifications-slack-07

2. User Notificationsでは対応していないイベントがある

当初はIAMユーザの作成通知をSlack上に送信してみたく、以下のようなイベントパターンにてスタックを作成してみましたが、できませんでした。

NotificationEventRule:
    Type: AWS::Notifications::EventRule
    Properties:
      NotificationConfigurationArn: !Ref NotificationConfiguration
      Source: "aws.iam"
      EventType: "AWS API Call via CloudTrail"
      EventPattern: |
        {
          "detail": {
            "eventSource": "iam.amazonaws.com",
            "eventName": "CreateUser"
          }
        }
      Regions: 
        - us-east-1

エラー内容

05

どうやらIAMに関する詳細なイベントを拾うことはできなさそうでした。どのサービスやイベントがサポートされているか明確なドキュメントは見当たりませんでしたが、マネコン上の「通知設定」から作成画面にないものはサポートされてなさそうです。

06

3. Slackのプライベートチャンネルにイベントを送付する場合はチャンネルに「Amazon Q」を招待する必要がある

以下公式ドキュメントにもあるようにプライベートチャンネルにイベントを通知する場合は、「Amazon Q」をチャンネルに招待 する必要があります。パブリックチャンネルの場合は不要です。

Chatbot時代は「aws」ユーザーを招待するみたいでしたが、サービス名称が変わったので合わせて変更になったみたいですね。

https://docs.aws.amazon.com/chatbot/latest/adminguide/slack-setup.html

最後に

今回はCloudFormationを使ってAWS User Notifications + Amazon Q Developer in chat applicationsを設定してSlackにイベントを送信する際のハマりポイントをいくつか挙げてみました。
理想としてはイベントに関する挙動を全てCloudFormationで一元管理できれば良いですが、現状では手動対応が必要な箇所がいくつか存在していました。

AWS User Notificationsに関してはまだ理解できていないポイントもあるので引き続き調査を続けていこうと思います。今回は以上です。

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.