Node.jsだと、レイヤーを導入するのが面倒なので、今回はPythonで。
- 環境変数
WEBHOOK_URL
にSlackから生成したWebhook URLを設定してください。 - トリガーのソースはCloudWatch Logsで、ロググループを指定しておきます。
従来のSlack Appを使わないIncoming Webhookはレガシーな方法のようです。1Webhook/チャンネルになったんですね。
なので、今回はSlack Appを使うことを前提にしたコードです。
CloudWatch LogsのPayloadを全て出力するようになってます。
#!/usr/bin/python3.9
import urllib3
import json
import base64
import gzip
import os
http = urllib3.PoolManager()
def lambda_handler(event, context):
decoded_data = base64.b64decode(event['awslogs']['data'])
json_data = json.loads(gzip.decompress(decoded_data))
url = os.environ["WEBHOOK_URL"]
msg = {
"text": json.dumps(json_data)
}
encoded_msg = json.dumps(msg).encode("utf-8")
resp = http.request("POST", url, body=encoded_msg)
print(
{
"status_code": resp.status,
"response": resp.data,
}
)
参考