やること
PowerShellでSlack通知してみるの続き、AWS Lambdaで通知してみる
前提
AWSアカウントとSlackアプリのIncoming Webhook URL
を取得していることが前提で始めます。
IAMロール作成
Lambda実行に必要な権限(IAMロール)を作成します。

必要なポリシーを選択します。今回はLambdaが動けばいいので、AWSLambdaBasicExecutionRole
を選択しました。

ロール名を設定します。

Lambda作成
AWSコンソールのLambdaを開き、関数の作成
ボタンをクリックします。

一から作成
を選択します。

赤枠部分を設定します。実行ロールは、さきほど作成したIAMロールを設定します。

このような画面が表示されたら、下にスクロールします。

赤枠部分にあるスクリプトを次に紹介するスクリプトに書き換えます。

書き換えのスクリプトです。
スクリプト内のhttps://hooks.slack.com/services/xxx/xxx/xxx
は、SlackのIncoming Webhookに修正します。
import json
import urllib.request
import logging
from collections import OrderedDict
import pprint
def post_slack(argStr):
message = argStr + "です"
send_data = {
"text": message,
}
send_text = json.dumps(send_data)
request = urllib.request.Request(
"https://hooks.slack.com/services/xxx/xxx/xxx",
data=send_text.encode('utf-8'),
method="POST"
)
with urllib.request.urlopen(request) as response:
response_body = response.read().decode('utf-8')
def lambda_handler(event, context):
post_slack("test")
これで、設定完了です。
動作テストを用意する
Lambda実行のテストを用意をします。作成済みであればこの作業は不要です。
テスト
をクリックします。

イベント名を設定します。緑枠の部分は今回は関係ないので編集不要です。

これで、テストの準備が完了しました。
動作テスト:LambdaからSlackに通知してみる
先ほど作成したテスト(test
)を設定し、テスト
ボタンをクリックします。

実行結果 成功
と表示されれば実行成功です。

Slackに通知が来ていることを確認します。

関連投稿
- PowerShellでSlack通知してみる
- LambdaでSlack通知してみる ←今ココ