Athenaの実行結果を通知をLambdaから実行するときにやって良かった通知機構


概要

EventBridgeでAthenaの結果を受けとり、StateがFAILEDだった場合にSlackに確認できるコマンドの通知を行っています。

こちらの記事で書いたものと一緒に運用したのですが、以下のようなSlack通知をすると便利ですよ、という話です。

コード

だいたいこんな感じのメソッドです。

(略)

if currentState == 'FAILED' and previousState =='RUNNING' :
    post_slack("query_execution_id:"+query_execution_id+" is got FAILED. Please check with the following command [ aws athena get-query-execution --query-execution-id "+query_execution_id+" ]")


def post_slack(msg):
    message = msg
    # 弊チームでは :squirrel: が人気です。
    send_data = {
        "username": "EventBridgeから来たエラー",
        "icon_emoji": ":squirrel:",
        "text": message,
    }
    send_text = "payload=" + json.dumps(send_data)
    request = urllib.request.Request(
        "https://hooks.slack.com/services/XXXXXXXXXXXXXX", 
        data=send_text.encode("utf-8"), 
        method="POST"
    )
    with urllib.request.urlopen(request) as response:
        response_body = response.read().decode("utf-8")

特徴

FAILEDになるとSlackの専用チャンネルに

aws athena get-query-execution --query-execution-id xxxxxxxx

が来ます。

特にどういったクエリが実行されるか分からない中で Query exhausted resources at this scale factor を把握していけるのは、クエリ改善において大きいです。

余談

EventBridgeで受け取れる情報だけで実現しているため上記のようになっていますが、ここで更にGetQueryExecutionを使って、直接エラー内容をSlackに貼りつつ、RDSなどに成功、失敗ともに結果を格納していく、みたいなこともやりたいなと思っています。