AWS Sample のAmazon Q Slack Gateway をデプロイしてAmazonQBotを作成してみた

先日発表されたAmazon Q の紹介の中に、AWSの公式サンプルのリポジトリで、Amazon QのアプリケーションをSlack と連携するアプリケーションが紹介されていたので、実際にデプロイしてみました
2023.12.01

先日発表されたAmazon Q の紹介の中に、AWSの公式サンプルのリポジトリで、Amazon QのアプリケーションをSlack と連携するアプリケーションが紹介されていたので、実際にデプロイしてみました。

Introducing Amazon Q, a new generative AI-powered assistant (preview) | Amazon Web Services

GitHub のリポジトリは下記になります。

Amazon Q Slack Gateway https://github.com/aws-samples/amazon-q-slack-gateway

単純なSlack連携であれば、ChatBotと連携する方法もあるようなので、ちょっと試すだけならそちらの方がよいとおもいますが、こちらはSlackアプリケーションとしてAmazon Qと連携するサンプルになります。またオープンソースなのでカスタマイズなども可能なようでした。

動かすまでにちょっと手こずってしまったので、ご参考になれば幸いです。

なお、実際に動くサンプル動作などはGithubのページの動画にもありますので、ぜひご覧になってみてください。

やってみる

なお、Amazon Qは現在プレビューですのでGA後の動作やデプロイ方法などは変わることもあることをご承知おきください。

事前準備

すでにAmazon Q のアプリケーションが作られている場合は以下のAmazon Qのユーザー登録、アプリケーション作成は不要です。

既存のAmazon Q ユーザ、またAmazon Qアプリケーションを使用してください。

Amazon Q のユーザー登録

デプロイするアプリケーションで使用するユーザーを追加します。

なお、このSlackアプリケーションでは質問するユーザーのEmailアドレスとSlackのEmailアドレスが一致している必要があります。

https://us-east-1.console.aws.amazon.com/amazonq/home?region=us-east-1#user-provisioning

Amazon Qのアプリケーションの作成

Amazon Q のアプリケーションを作成します。

パラメータなどはデフォルトで作ってくれるので、また、必要なIAMロールもコンソールより作成してくれるようなのでそのまま使用します。

データソースはSample doucumentsを使用します。

正常にできているとアプリケーション一覧に表示されるかと思います。

一覧から連携したいアプリケーションを選択して、Application ID をメモしておいてください

デプロイ環境の準備

ここからは基本的にはリポジトリの手順書どおりに実施していきます。

  1. Dependencies

You need to have the following packages installed on your computer to build and deploy the project.

  1. bash shell (Linux, MacOS, Windows-WSL)
  2. node and npm: https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
  3. tsc (typescript): npm install -g typescript
  4. esbuild: npm i -g esbuild
  5. jq: https://jqlang.github.io/jq/download/
  6. aws (AWS CLI): https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
  7. cdk (AWS CDK): https://docs.aws.amazon.com/cdk/v2/guide/cli.html

node の確認

nvm use 20
node --version
v20.10.0

ts,esbuildのインストール

$ npm install -g typescript
$ npm i -g esbuild

なお、必要に応じて、AWS CLI , CDKの最新化を実施してください。

$ npm install -g aws-cdk

リポジトリのクローン

$ git clone https://github.com/aws-samples/amazon-q-slack-gateway.git
Cloning into 'amazon-q-slack-gateway'...
remote: Enumerating objects: 451, done.
remote: Counting objects: 100% (182/182), done.
remote: Compressing objects: 100% (110/110), done.
remote: Total 451 (delta 93), reused 109 (delta 66), pack-reused 269
Receiving objects: 100% (451/451), 17.95 MiB | 22.47 MiB/s, done.
Resolving deltas: 100% (199/199), done.

AWS クレデンシャルの確認

またビルドの途中でAWS と接続する必要がありますので、以下のコマンドなどを実行してAWSのクレデンシャルも確認しておいてください。

$ aws s3 ls

パラメータ設定とビルド

Amazon Q のユーザーID、アプリケーションID, リージョン、エンドポイントなどを設定していきます。

$ cd amazon-q-slack-gateway/
$ ./init.sh 
node v20.10.0 installed - OK
npm v10.2.4 installed - OK
tsc v5.3.2 installed - OK
esbuild v0.19.8 installed - OK
jq v1.6 installed - OK
aws v2.13.22 installed - OK
cdk v2.111.0 installed - OK

Setting up environment...
Name for slack bot [AmazonQBot]:[Enter] <=デフォルトのAmazonQBotにしています

Amazon Q User ID (leave empty to use slack users' email as user Id) []: hoge@exsample.com [Enter] <= slackユーザーのメールを設定してください
Amazon Q Application ID (copy from AWS console) [none]: xxxxx-xxx-xxxx-xxx <= コピーしたIDを設定してください
Amazon Q Region [^[a-z]{2}-[a-z]+-[0-9]+$]: us-east-1  <= Amazon Q アプリケーションのリージョンになります
Amazon Q Endpoint (leave empty for default endpoint) []: [Enter] <=デフォルトのendpointにしています
Number of days to keep conversation context [90]:  [Enter]
Configuration saved to environment.json
Running npm install and build...

設定後は cdkのビルドが実施されます。割と時間がかかるので待ちます。。。

Configuration saved to environment.json
Running npm install and build...

up to date, audited 555 packages in 3s

99 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

> my_amazon_q_slack_bot@0.1.0 build
> tsc

Running cdk bootstrap...
 ⏳  Bootstrapping environment aws://123456789012/us-east-1...
Trusted accounts for deployment: (none)
Trusted accounts for lookup: (none)
Using default execution policy of &#039;arn:aws:iam::aws:policy/xxxxxxxxx&#039;. Pass &#039;--cloudformation-execution-policies&#039; to customize.
CDKToolkit: creating CloudFormation changeset...
 ✅  Environment aws://123456789012/us-east-1 bootstrapped.

All done!

All done! が表示されればビルドはできてますので、デプロイしてきます。

./deploy.sh

省略

This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening).
Please confirm you intend to make the following modifications:

IAM Statement Changes
┌───┬──────────────────────────────────────┬────────┬──────────────────────────────────────┬──────────────────────────────────────┬──────────────────────────────────────┐
│   │ Resource                             │ Effect │ Action                               │ Principal                            │ Condition                            │ 
├───┼──────────────────────────────────────┼────────┼──────────────────────────────────────┼──────────────────────────────────────┼──────────────────────────────────────┤ 
│ + │ ${AmazonQBot-DynamoCache.Arn}        │ Allow  │ dynamodb:DeleteItem                  │ AWS:${AmazonQBot-SlackEventHandler-R │                                      │ 
│   │ ${AmazonQBot-MessageMetadata.Arn}    │        │ dynamodb:GetItem                     │ ole}                                 │                                      │ 
│   │                                      │        │ dynamodb:PutItem                     │                                      │                                      │ 
│ + │ ${AmazonQBot-DynamoCache.Arn}        │ Allow  │ dynamodb:DeleteItem                  │ AWS:${AmazonQBot-SlackInteractionHan │                                      │ 
│   │ ${AmazonQBot-MessageMetadata.Arn}    │        │ dynamodb:GetItem                     │ dler-Role}                           │                                      │ 
│   │                                      │        │ dynamodb:PutItem                     │                                      │                                      │ 
│ + │ ${AmazonQBot-DynamoCache.Arn}        │ Allow  │ dynamodb:DeleteItem                  │ AWS:${AmazonQBot-SlackCommandHandler │                                      │ 
│   │ ${AmazonQBot-MessageMetadata.Arn}    │        │ dynamodb:GetItem                     │ -Role}                               │                                      │ 
│   │                                      │        │ dynamodb:PutItem                     │                                      │                                      │ 
├───┼──────────────────────────────────────┼────────┼──────────────────────────────────────┼──────────────────────────────────────┼──────────────────────────────────────┤ 
│ + │ ${AmazonQBot-Secret}                 │ Allow  │ secretsmanager:GetSecretValue        │ AWS:${AmazonQBot-SlackEventHandler-R │                                      │ 
│   │                                      │        │                                      │ ole}                                 │                                      │ 
│ + │ ${AmazonQBot-Secret}                 │ Allow  │ secretsmanager:GetSecretValue        │ AWS:${AmazonQBot-SlackInteractionHan │                                      │ 
│   │                                      │        │                                      │ dler-Role}                           │                                      │ 
│ + │ ${AmazonQBot-Secret}                 │ Allow  │ secretsmanager:GetSecretValue        │ AWS:${AmazonQBot-SlackCommandHandler │                                      │ 
│   │                                      │        │                                      │ -Role}                               │                                      │ 
├───┼──────────────────────────────────────┼────────┼──────────────────────────────────────┼──────────────────────────────────────┼──────────────────────────────────────┤
├───┼──────────────────────────────────────┼────────┼──────────────────────────────────────┼──────────────────────────────────────┼──────────────────────────────────────┤ 
│ + │ arn:aws:qbusiness:*:*:application/ce │ Allow  │ qbusiness:ChatSync                   │ AWS:${AmazonQBot-SlackEventHandler-R │                                      │ 
│   │ xxxxx-xxxxx-xxxxx-xxxxx-xxxxx│        │ qbusiness:PutFeedback                │ ole}                                 │                                      │ 
│ + │ arn:aws:qbusiness:*:*:application/ce │ Allow  │ qbusiness:ChatSync                   │ AWS:${AmazonQBot-SlackInteractionHan │                                      │ 
│   │ xxxxx-2ed2-42d2-82a5-xxxxx│        │ qbusiness:PutFeedback                │ dler-Role}                           │                                      │ 
│ + │ arn:aws:qbusiness:*:*:application/ce │ Allow  │ qbusiness:ChatSync                   │ AWS:${AmazonQBot-SlackCommandHandler │                                      │ 
│   │ 67c873-2ed2-xxxxx-82a5-xxxxx│        │ qbusiness:PutFeedback                │ -Role}                               │                                      │ 
└───┴──────────────────────────────────────┴────────┴──────────────────────────────────────┴──────────────────────────────────────┴──────────────────────────────────────┘ 
IAM Policy Changes
┌───┬────────────────────────────────────────────┬────────────────────────────────────────────────────────────────────────────────────┐
│   │ Resource                                   │ Managed Policy ARN                                                                 │
├───┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────────────────────┤
│ + │ ${AmazonQBot-SlackCommandHandler-Role}     │ arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole │
├───┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────────────────────┤
│ + │ ${AmazonQBot-SlackEventHandler-Role}       │ arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole │
├───┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────────────────────┤
│ + │ ${AmazonQBot-SlackInteractionHandler-Role} │ arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole │
└───┴────────────────────────────────────────────┴────────────────────────────────────────────────────────────────────────────────────┘
Security Group Changes
┌───┬────────────────────────────────────────────────────────────────┬─────┬────────────┬─────────────────┐
│   │ Group                                                          │ Dir │ Protocol   │ Peer            │
├───┼────────────────────────────────────────────────────────────────┼─────┼────────────┼─────────────────┤
│ + │ ${AmazonQBot-SlackCommandHandler-Fn/SecurityGroup.GroupId}     │ Out │ Everything │ Everyone (IPv4) │
├───┼────────────────────────────────────────────────────────────────┼─────┼────────────┼─────────────────┤
│ + │ ${AmazonQBot-SlackEventHandler-Fn/SecurityGroup.GroupId}       │ Out │ Everything │ Everyone (IPv4) │
├───┼────────────────────────────────────────────────────────────────┼─────┼────────────┼─────────────────┤
│ + │ ${AmazonQBot-SlackInteractionHandler-Fn/SecurityGroup.GroupId} │ Out │ Everything │ Everyone (IPv4) │
└───┴────────────────────────────────────────────────────────────────┴─────┴────────────┴─────────────────┘

作成されるリソースが表示されますので、確認後yを選択します。

Do you wish to deploy these changes (y/n)?

CDKのデプロイ状況をコンソールで確認したい場合はCloudFormationに”AmazonQBot”のスタックができていますので確認してください。

AmazonQSlackGatewayStack (AmazonQBot): deploying... [1/1]
AmazonQBot: creating CloudFormation changeset...

 ✅  AmazonQSlackGatewayStack (AmazonQBot)

✨  Deployment time: 440.4s

Outputs:
AmazonQSlackGatewayStack.AmazonQBotSlackCommandHandlerApiEndpointxxxxx = https://xxxxxxx.execute-api.us-east-1.amazonaws.com/prod/
AmazonQSlackGatewayStack.AmazonQBotSlackEventHandlerApiEndpointxxxxx = https://xxxxxxx.execute-api.us-east-1.amazonaws.com/prod/
AmazonQSlackGatewayStack.AmazonQBotSlackInteractionHandlerApiEndpointxxxxx = https://xxxxxx.execute-api.us-east-1.amazonaws.com/prod/
Stack ARN:
arn:aws:cloudformation:us-east-1:xxxxxxxx:stack/AmazonQBot/3xxxxxxx

✨  Total time: 560.91s

Updating slack app manifest...
Slack app manifest created: slack-manifest-output.json.
URL for your slack bot secrets: https://ap-northeast-1.console.aws.amazon.com/secretsmanager/secret?name=AmazonQBot-Secret®ion=ap-northeast-1
All done!

デプロイが正常に行われるとOutputs に Slackのアプリケーションで設定するエンドポイントなどが表示されるのでメモしてください。

Slack App の作成

https://api.slack.com/apps

でSlack App を作成します。

From App manifest を選択

アプリを作成するワークスペースを選択します。

manifest below で リポジトリの直下にできている slack-manifest-output.json の内容を張り付けます。

jsonから読み込まれた設定でアプリが作られますので、確認して、アプリを作成します。

ちなみに、以下の権限を使うようです

Bot Scopes (10)

app_mentions:read, channels:history, chat:write, files:read, groups:history, im:history, im:read, users:read, users:read.email, commands

続いてワークスペースにインストールします

Install to Workspace を選択してインストールします。

権限を確認して、「許可する」を押下します。

App Credentials をより「SigningSecret」をメモします。

また、OAuth & Permissions より「Bot User OAuth Token」をメモします。

SSM パラメータの設定

AWS コンソールでSlack Appのパラメータ設定を行います。

CDKのデプロイの最後の方で出力されているSSMシークレットをSlack Appの方で取得した値に更新します。

https://ap-northeast-1.console.aws.amazon.com/secretsmanager/secret?name=AmazonQBot-Secret®ion=ap-northeast-1

デプロイと設定は以上になります。

AmazonQBot と話してみる

Slackで"AmazonQBot” (デフォルトの名前)をチャンネルに追加して、メンションしてみてください。

まとめ

実際にはサンプルのコードを流用したのでほとんど何も作ってないですが、Amazon QとSlackのアプリケーションを連携することができました。 あまり中身を見ずにつくってしまったので、あとでアプリケーション構成などをまとめようと思います。

Amazon Q のユーザー登録をおこなったり、IdPをもちいてユーザー制御を行える(Amazon Q の機能)と連携できるのでかなり完成されたサンプルだなと思いました。 Slackアプリと連携するAmazon Q アプリケーションを変更するだけで、Bot(Amazon Q) を変えることができますのでビジネスアプリなどのたたき台にはとても良いとおもいます。

なお、Slack連携とは関係ない。Amazon Q のユーザープロビジョニングでめっちゃはまったので、あらためてIdP設定のところはご紹介したいと思います。

参考

https://aws.amazon.com/jp/blogs/aws/introducing-amazon-q-a-new-generative-ai-powered-assistant-preview/

https://github.com/aws-samples/amazon-q-slack-gateway