Skip to content

Instantly share code, notes, and snippets.

@momota10s
Last active November 22, 2021 09:55
Show Gist options
  • Save momota10s/34753432a2c7869c693a9a960a5592a6 to your computer and use it in GitHub Desktop.
Save momota10s/34753432a2c7869c693a9a960a5592a6 to your computer and use it in GitHub Desktop.
Amazon SNSのサブスクリプション先としてlambdaを指定、その後lambdaにてslack通知を行う
import json
from pytz import timezone
from dateutil import parser
import urllib.request
print('Loading function')
def lambda_handler(event, context):
message = event['Records'][0]['Sns']['Message']
print("From SNS: " + message)
# jsonに変換して欲しい情報をparseする(この実装ではeventTimeをJSTに変換してslackに通知している)
message = json.loads(message)
event_time_jst = parser.parse(message['detail']['eventTime']).astimezone(timezone('Asia/Tokyo'))
send_to_slack(event_time_jst)
def send_to_slack(event_time_jst):
msg = f"""
```
Event Time: {event_time_jst}
```
"""
send_data = {
"username": "通知",
"icon_emoji": ":memo:",
"text": msg,
}
send_text = "payload=" + json.dumps(send_data)
req = urllib.request.Request(
"https://hooks.slack.com/services/~~~",
data=send_text.encode("utf-8"),
method="POST"
)
with urllib.request.urlopen(req) as res:
res_body = res.read().decode("utf-8")
print(res_body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment