0
2

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 5 years have passed since last update.

Lambdaでヘルスチェックするコードのメモ

Posted at

完全自分用メモですよ。

# -*- coding: utf-8 -*-
 
import commands
import os
import boto3
 
print('Loading function')

## SNS送信関数
def sendmsg():
    TOPIC_ARN = u'arn:aws:sns:ap-northeast-1:******'
 
    msg = "Please check for https://www.example.com/"
    subject = u'Site is down!!'
    client = boto3.client('sns')
 
    request = {
        'TopicArn': TOPIC_ARN,
        'Message': msg,
        'Subject': subject
    }
 
    response = client.publish(**request)

#Linuxのコマンド実行関数
def _(cmd):
    return commands.getoutput(cmd)
 
def lambda_handler(event, context):
    #ヘルスチェックで問題がない場合に表示する文字列
    ok_string = "ok"
     
    #ヘルスチェックで問題がある場合に表示する文字列
    ng_string = "ng"
     
    #実行するcurlコマンド
    curl_cmd = 'curl -LI https://www.example.com/ -o /dev/null -w \'%{http_code}\\n\' -s'

    #curlコマンドの実行
    response = _(curl_cmd)
     
    #レスポンスのログへの記録
    print("----------response body start.----------")
    print(response)
    print("----------response body end.------------")
     
    #ステータスコードの判定
    if response == ("200"):
        print("Status is 200")
        return {"result": ok_string}
    else:
        print("Status is not 200")
        sendmsg()
        return {"result": ng_string}
0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?