1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

コピペでできるCloudWatch LogsからSlackに通知するコード

1
Posted at

Node.jsだと、レイヤーを導入するのが面倒なので、今回はPythonで。

  1. 環境変数WEBHOOK_URLにSlackから生成したWebhook URLを設定してください。
  2. トリガーのソースは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,
        }
    )

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?