AWS Step FunctionsでWorkflowsを利用しTeams通知してみた(JSONata)

AWS Step FunctionsでWorkflowsを利用しTeams通知してみた(JSONata)

Clock Icon2025.06.03

はじめに

以前、JSONPathを利用したAWS Step Functionsで、Incoming Webhookを利用してTeams通知したブログを執筆しました。

https://dev.classmethod.jp/articles/aws-stepfunctions-notify-microsoft-teams/

今回は、JSONataを利用したAWS Step Functionsで、Incoming Webhookではなく、Workflowsを使ってTeamsに通知します。

Incoming Webhookは廃止予定であり、公式の情報によると、2025年12月ごろまでに移行が推奨されています。

Microsoft TeamsのOfficeコネクタが廃止される予定で、その中にIncoming Webhookが含まれています。廃止に伴い、移行先としてWorkflowsを使うことが推奨されています。

https://devblogs.microsoft.com/microsoft365dev/retirement-of-office-365-connectors-within-microsoft-teams/

また、AWS Step FunctionsではJSONataを利用できるようになっています。JSONataは、JSONデータのクエリや変換、加工を行うための言語で、AWSでもステートマシンでのJSONataの利用を推奨しています。

https://aws.amazon.com/jp/blogs/news/simplifying-developer-experience-with-variables-and-jsonata-in-aws-step-functions/

全体の構成は以下の通りです。HTTPタスクを利用してTeamsに通知します。

2744e15cdecb02613ee1d898e2e5a4b6

HTTPタスクは、Step Functionsの機能の1つで、Microsoft TeamsやSalesforceなどのサードパーティのAPIを直接呼び出すことができます。

HTTPタスクでは、API呼び出しの認証情報をEventBridge接続で管理できるため、シークレットをステートマシン定義にハードコーディングすることを避けられます。

WorkflowsでTeamsのWebhook URLを発行

Workflowを作成し、発行されたWebhook URLを取得します。

Workflowの作成方法については、以下の記事をご参照ください。

https://blog.devplatform.techmatrix.jp/blog/teams_workflows_notification/

EventBridge接続を作成

EventBridge接続では、APIキー名をContent-Type、キーの値をapplication/jsonと指定して作成します。

835bbdac03f491fe2d16c49fb600c2b6

作成すると、EventBridge接続の情報を保存するSecrets Managerのシークレットが自動作成されます。

Step Functionsステートマシンを作成

フローには、Call third-party APIのみを挿入します。

cm-hirai-screenshot 2025-05-27 9.33.38

コード全体(クリックで展開)
{
    "Comment": "A description of my state machine",
    "StartAt": "Call HTTPS APIs",
    "States": {
      "Call HTTPS APIs": {
        "Type": "Task",
        "Resource": "arn:aws:states:::http:invoke",
        "Arguments": {
          "ApiEndpoint": "https://xxxjapaneast.logic.azure.com/workflows/xxxxxxxxxx/triggers/manual/paths/invoke",
          "Method": "POST",
          "InvocationConfig": {
            "ConnectionArn": "arn:aws:events:ap-northeast-1:アカウントID:connection/teams/95886171-4978-49f4-93b7-b89b427ed190"
          },
          "RequestBody": {
            "type": "message",
            "attachments": [
              {
                "contentType": "application/vnd.microsoft.card.adaptive",
                "content": {
                  "type": "AdaptiveCard",
                  "body": [
                    {
                      "type": "TextBlock",
                      "text": "<at>平井 裕二</at>",
                      "weight": "bolder",
                      "size": "medium"
                    },
                    {
                      "type": "TextBlock",
                      "text": "アラート通知です",
                      "size": "Large",
                      "wrap": true,
                      "weight": "Bolder"
                    },
                    {
                      "type": "Table",
                      "columns": [
                        {
                          "width": 1
                        },
                        {
                          "width": 2
                        }
                      ],
                      "rows": [
                        {
                          "type": "TableRow",
                          "cells": [
                            {
                              "type": "TableCell",
                              "items": [
                                {
                                  "type": "TextBlock",
                                  "text": "項目",
                                  "weight": "Bolder"
                                }
                              ]
                            },
                            {
                              "type": "TableCell",
                              "items": [
                                {
                                  "type": "TextBlock",
                                  "wrap": true,
                                  "text": "内容"
                                }
                              ]
                            }
                          ]
                        },
                        {
                          "type": "TableRow",
                          "cells": [
                            {
                              "type": "TableCell",
                              "items": [
                                {
                                  "type": "TextBlock",
                                  "text": "test",
                                  "weight": "Bolder"
                                }
                              ]
                            },
                            {
                              "type": "TableCell",
                              "items": [
                                {
                                  "type": "TextBlock",
                                  "wrap": true,
                                  "text": "hoge"
                                }
                              ]
                            }
                          ]
                        }
                      ]
                    }
                  ],
                  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
                  "version": "1.5",
                  "msteams": {
                    "width": "full",
                    "entities": [
                      {
                        "type": "mention",
                        "text": "<at>平井 裕二</at>",
                        "mentioned": {
                          "id": "example@example.com",
                          "name": "平井 裕二"
                        }
                      }
                    ]
                  }
                }
              }
            ]
          },
          "QueryParameters": {
            "api-version": "2016-06-01",
            "sig": "xxxxxxxx-xxxxxxx",
            "sp": "/triggers/manual/run",
            "sv": "1.0"
          }
        },
        "Retry": [
          {
            "ErrorEquals": [
              "States.ALL"
            ],
            "BackoffRate": 2,
            "IntervalSeconds": 1,
            "MaxAttempts": 3,
            "JitterStrategy": "FULL"
          }
        ],
        "End": true
      }
    },
    "QueryLanguage": "JSONata"
  }

設定内容

TeamsのWebhookURLを以下だと仮定します。

APIエンドポイントは以下を設定します。

[設定]タブで以下の設定を行います。[入力]、[出力]、[エラー処理]タブはデフォルトのままです。

  • APIエンドポイント:TeamsのWebhook URL
  • メソッド:POST
  • 接続:先ほど作成したEventBridge接続

クエリパラメータは以下の通りです(クエリストリング以降を利用)。

{
  "api-version": "2016-06-01",
  "sig": "xxxxxxxx-xxxxxxx",
  "sp": "/triggers/manual/run",
  "sv": "1.0"
}

リクエスト本文は以下のとおりです。

{
  "type": "message",
  "attachments": [
    {
      "contentType": "application/vnd.microsoft.card.adaptive",
      "content": {
        "type": "AdaptiveCard",
        "body": [
          {
            "type": "TextBlock",
            "text": "<at>平井 裕二</at>",
            "weight": "bolder",
            "size": "medium"
          },
          {
            "type": "TextBlock",
            "text": "アラート通知です",
            "size": "Large",
            "wrap": true,
            "weight": "Bolder"
          },
          {
            "type": "Table",
            "columns": [
              {
                "width": 1
              },
              {
                "width": 2
              }
            ],
            "rows": [
              {
                "type": "TableRow",
                "cells": [
                  {
                    "type": "TableCell",
                    "items": [
                      {
                        "type": "TextBlock",
                        "text": "項目",
                        "weight": "Bolder"
                      }
                    ]
                  },
                  {
                    "type": "TableCell",
                    "items": [
                      {
                        "type": "TextBlock",
                        "wrap": true,
                        "text": "内容"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "TableRow",
                "cells": [
                  {
                    "type": "TableCell",
                    "items": [
                      {
                        "type": "TextBlock",
                        "text": "test",
                        "weight": "Bolder"
                      }
                    ]
                  },
                  {
                    "type": "TableCell",
                    "items": [
                      {
                        "type": "TextBlock",
                        "wrap": true,
                        "text": "hoge"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ],
        "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
        "version": "1.5",
        "msteams": {
          "width": "full",
          "entities": [
            {
              "type": "mention",
              "text": "<at>平井 裕二</at>",
              "mentioned": {
                "id": "xxx@example.com",
                "name": "平井 裕二"
              }
            }
          ]
        }
      }
    }
  ]
}

メンション設定

リクエスト本文のidnameは、メンション対象者に応じて変更してください。

  • attachments.0.content.body.0.text:ユーザー名を指定し、<at></at>で囲む
  • attachments.0.content.msteams.entities.0.text:ユーザー名を指定し、<at></at>で囲む
  • attachments.0.content.msteams.entities.0.mentioned.id:ユーザーのメールアドレス(xxxx@example.com)を指定
  • attachments.0.content.msteams.entities.0.mentioned.name:ユーザー名を指定

cm-hirai-screenshot 2025-05-27 9.33.45

IAMロール

ステートマシン作成時、IAMロールも自動作成されます。

cm-hirai-screenshot 2025-05-27 9.34.08

テスト

ステートマシンを実行すると、Teamsに通知されました。

cm-hirai-screenshot 2025-05-27 9.39.42

利用ケース

以下のような場面での活用が考えられます。

  • 障害検知時にTeamsに通知し、メンバーにメンションを送る
  • バッチ処理の完了をTeamsに通知する

Step Functionsの利点は、柔軟なワークフローを組み立てながら、Teams通知を組み込める点です。ぜひ活用してみてください。

参考

https://learn.microsoft.com/ja-jp/microsoftteams/platform/task-modules-and-cards/cards/cards-format?tabs=adaptive-md%2Cdesktop%2Cdesktop1%2Cdesktop2%2Cconnector-html

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.