From a297976ea0f738831a169d4a06d1e9294b1a319a Mon Sep 17 00:00:00 2001 From: "shimoda.m@nds-tyo.co.jp" Date: Thu, 26 Jan 2023 09:17:34 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Slack=E3=81=AEWebhook=E3=81=AB=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E3=82=92=E9=80=81=E3=82=8BLambda=E9=96=A2=E6=95=B0?= =?UTF-8?q?=E3=82=92=E3=83=AA=E3=83=97=E3=83=AC=E3=83=BC=E3=82=B9=E3=80=82?= =?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=88=E3=83=ABnull=E3=81=AB=E3=82=82?= =?UTF-8?q?=E5=AF=BE=E5=BF=9C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lambda/NoticeToSlack/notice_to_slack.py | 49 +++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 lambda/NoticeToSlack/notice_to_slack.py diff --git a/lambda/NoticeToSlack/notice_to_slack.py b/lambda/NoticeToSlack/notice_to_slack.py new file mode 100644 index 00000000..7814d1c8 --- /dev/null +++ b/lambda/NoticeToSlack/notice_to_slack.py @@ -0,0 +1,49 @@ +import json +import os +import urllib.request as request + + +def make_attaches(event): + """Slackのリクエストボディ attachmentsを作成する""" + attachments = [] + for evt in event.get('Records'): + sns_event = evt.get('Sns') + event_subscription_arn = evt.get('EventSubscriptionArn') + subject = sns_event.get('Subject', 'AWS Notification Message') + sns_timestamp = sns_event.get('Timestamp') + message = sns_event.get('Message') + message_id = sns_event.get('MessageId') + + attachments.append({ + "fallback": f"Notification from mbj-newdwh2021 AWS: {event_subscription_arn}", + "pretext": f"Notification from mbj-newdwh2021 AWS: {event_subscription_arn}", + "color":"#0000D0", + "fields":[ + { + "title": f"{subject}", + "value": f"{sns_timestamp}: {message} (MessageId: {message_id})", + "short": False + } + ] + }) + + return attachments + + +def lambda_handler(event, context): + attachments = make_attaches(event) + request_url = os.environ.get('SLACK_WEBHOOK_URL') + payload = { + 'attachments': attachments + } + headers = { + 'Content-Type': 'application/json', + } + print(payload) + + req = request.Request(request_url, json.dumps(payload, ensure_ascii=False).encode(), headers, method='POST') + + with request.urlopen(req) as res: + body = res.read() + + print(body)