{"id":2305,"date":"2015-12-15T00:00:51","date_gmt":"2015-12-14T15:00:51","guid":{"rendered":"http:\/\/www.magtranetwork.com\/?p=2305"},"modified":"2017-02-23T10:57:38","modified_gmt":"2017-02-23T01:57:38","slug":"aws_lambda_python_health_check","status":"publish","type":"post","link":"https:\/\/www.magtranetwork.com\/aws\/aws_lambda_python_health_check.html","title":{"rendered":"AWS Lambda Python\u3067API\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af(Health Check)\u3092\u884c\u3046\u30b7\u30a7\u30eb\u30b9\u30af\u30ea\u30d7\u30c8(\u30d0\u30c3\u30c1\u51e6\u7406\u30d7\u30ed\u30b0\u30e9\u30e0) \u301cRoute53\u306e\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u3067\u306f\u3067\u304d\u306a\u3044EC2\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3001Amazon API Gateway\u306a\u3069\u3078\u306ePOST\u30e1\u30bd\u30c3\u30c9\u306e\u30d1\u30e9\u30e1\u30bf\u9001\u4fe1\u306b\u3082\u5bfe\u5fdc\u301c"},"content":{"rendered":"
Amazon API Gateway, AWS Lambda Python\u306e\u767b\u5834\u306b\u3088\u3063\u3066\u30d5\u30eb\u30de\u30cd\u30fc\u30b8\u30c9\u306a\u30b5\u30fc\u30d0\u74b0\u5883\u3067iPhone, Android\u306a\u3069\u306e\u30b9\u30de\u30fc\u30c8\u30d5\u30a9\u30f3\u30fb\u30e2\u30d0\u30a4\u30eb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306eAPI\u3092\u7c21\u5358\u306b\u4f5c\u6210\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n
\u4e00\u65b9\u3067\u4f5c\u6210\u3057\u305fAPI\u304c\u6b63\u5e38\u306b\u52d5\u4f5c\u3057\u7d9a\u3051\u3066\u3044\u308b\u304b\u306e\u76e3\u8996\u904b\u7528\u306b\u3064\u3044\u3066\u306fAmazon API Gateway, AWS Lambda Python\u306b\u3064\u3044\u3066CloudWatch\u3067\u76e3\u8996\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n
\u305f\u3060\u3001Amazon API Gateway, AWS Lambda Python\u306e\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u306bDynamoDB\u306a\u3069\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u914d\u7f6e\u3057\u3066\u3044\u308b\u5834\u5408\u3001API\u3078\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u5168\u3066\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u7d4c\u7531\u3057\u3066\u6b63\u3057\u304f\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u8fd4\u3063\u3066\u304f\u308b\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n
\u3053\u306e\u3088\u3046\u306a\u5834\u5408\u306fRoute53\u306e\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u6a5f\u80fd\u3092\u7528\u3044\u3066HTTP, HTTPS, TCP\u3092\u30c1\u30a7\u30c3\u30af\u3053\u3068\u304c\u4e00\u822c\u7684\u3067\u3059\u304c\u3001Route53\u306e\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u306fPOST\u30e1\u30bd\u30c3\u30c9\u3092\u7528\u3044\u305f\u30d1\u30e9\u30e1\u30bf\u9001\u4fe1\u306e\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u306f\u73fe\u5728\u306e\u3068\u3053\u308d\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u305b\u3093\u3002<\/p>\n
\u305d\u306e\u305f\u3081\u3001\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u6a5f\u80fd\u3092\u5225\u9014\u7528\u610f\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4eca\u56de\u306fAWS Lambda Python\u3092\u5bfe\u8c61\u306eAPI\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u306f\u5225\u30ea\u30fc\u30b8\u30e7\u30f3\u306b\u7528\u610f\u3059\u308b\u3053\u3068\u3067\u30ea\u30fc\u30b8\u30e7\u30f3\u969c\u5bb3\u3092\u8003\u616e\u3057\u305fAPI\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5099\u5fd8\u9332\u3068\u3057\u3066\u8a18\u8f09\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n
\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u306e\u65b9\u5f0f\u3068\u3057\u3066\u306fPython\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u4f9d\u5b58\u3057\u306a\u3044curl\u30b3\u30de\u30f3\u30c9\u3092\u7528\u3044\u3066\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n
curl\u30b3\u30de\u30f3\u30c9\u306fGET\u30e1\u30bd\u30c3\u30c9\u306f\u3082\u3061\u308d\u3093POST\u30e1\u30bd\u30c3\u30c9\u3067\u306e\u30d1\u30e9\u30e1\u30bf\u9001\u4fe1\u306b\u3082\u5bfe\u5fdc\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u3001Content-type\u306a\u3069\u306e\u30d8\u30c3\u30c0\u306e\u5185\u5bb9\u3082\u8a2d\u5b9a\u3067\u304d\u308b\u305f\u3081Amazon API Gateway\u306eAPI\u30ad\u30fc\u8a8d\u8a3c\u3082\u53ef\u80fd\u306a\u6c4e\u7528\u7684\u3067\u5927\u5909\u4fbf\u5229\u306a\u30b3\u30de\u30f3\u30c9\u3067\u3059\u3002<\/p>\n
\u3053\u3053\u3067\u306f\u3053\u306ecurl\u30b3\u30de\u30f3\u30c9\u3092\u7528\u3044\u3066POST\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u308b\u30d1\u30e9\u30e1\u30bf\u9001\u4fe1\u306b\u5bfe\u5fdc\u3057\u305fAPI\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u30d7\u30ed\u30b0\u30e9\u30e0\u672c\u4f53\u3068API\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u30d1\u30e9\u30e1\u30bf\u3092\u6e21\u3057\u3066\u547c\u3073\u51fa\u3059\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4f8b\u3092\u8a18\u8f09\u3057\u307e\u3059\u3002<\/p>\n
\r\n# -*- coding: utf-8 -*-\r\n\r\nimport commands\r\nimport os\r\n\r\nprint('Loading function')\r\n\r\nsearch_keyword = ""\r\n\r\n#Linux\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\r\ndef _(cmd):\r\n return commands.getoutput(cmd)\r\n\r\ndef lambda_handler(event, context):\r\n print "event:", event\r\n \r\n #\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u3067\u554f\u984c\u304c\u306a\u3044\u5834\u5408\u306b\u8868\u793a\u3059\u308b\u6587\u5b57\u5217\r\n ok_string = event["ok_string"]\r\n \r\n #\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u3067\u554f\u984c\u304c\u3042\u308b\u5834\u5408\u306b\u8868\u793a\u3059\u308b\u6587\u5b57\u5217\r\n ng_string = event["ng_string"]\r\n \r\n #\u5b9f\u884c\u3059\u308bcurl\u30b3\u30de\u30f3\u30c9\r\n curl_cmd = event["curl_cmd"]\r\n \r\n #curl\u30b3\u30de\u30f3\u30c9\u306e\u5b9f\u884c\u7d50\u679c\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u5185\u3092\u691c\u7d22\u3059\u308b\u30ad\u30fc\u30ef\u30fc\u30c9\r\n search_keyword = event["search_keyword"]\r\n \r\n #curl\u30b3\u30de\u30f3\u30c9\u306b\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u4ed8\u52a0\u3057\u305f\u5f62\u3067\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u53d7\u3051\u308b\r\n curl_cmd += ' -w "%{http_code}"'\r\n \r\n #curl\u30b3\u30de\u30f3\u30c9\u306e\u5b9f\u884c\r\n response = _(curl_cmd)\r\n \r\n #\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30ed\u30b0\u3078\u306e\u8a18\u9332\r\n print "----------response body start.----------"\r\n print response\r\n print "----------response body end.------------"\r\n \r\n \r\n if search_keyword == "":\r\n #\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\u304c\u306a\u3044\u5834\u5408\u306f\u30b9\u30c6\u30fc\u30bf\u30b9\u5224\u65ad\u306e\u307f\u884c\u3046\u3002\r\n if response.endswith("200"):\r\n print "Status is 200"\r\n return {"result": ok_string}\r\n else:\r\n print "Status is not 200"\r\n return {"result": ng_string}\r\n else:\r\n #\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\u304c\u3042\u308b\u5834\u5408\u306f\u30b9\u30c6\u30fc\u30bf\u30b9\u5224\u65ad\u3068\u30ec\u30b9\u30dd\u30f3\u30b9\u306b\u30ad\u30fc\u30ef\u30fc\u30c9\u304c\u542b\u307e\u308c\u308b\u304b\u3092\u5224\u65ad\u3059\u308b\u3002\r\n if response.endswith("200"):\r\n print "Status is 200"\r\n \r\n #\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30b9\u30c6\u30fc\u30bf\u30b9\u90e8\u5206\u3092\u524a\u9664\u3059\u308b\r\n body = response[0:len(response)-len("200")]\r\n \r\n #\u30ec\u30b9\u30dd\u30f3\u30b9\u30dc\u30c7\u30a3\u30fc\u306e\u4e2d\u3092\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\u3067\u691c\u7d22\u3059\u308b\r\n if search_keyword in body:\r\n print "Keyword is found."\r\n return {"result": ok_string}\r\n else:\r\n print "Keyword is not found."\r\n return {"result": ng_string}\r\n else:\r\n print "Status is not 200"\r\n return {"result": ng_string}\r\n<\/pre>\nAPI\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u30d1\u30e9\u30e1\u30bf\u3092\u6e21\u3057\u3066\u547c\u3073\u51fa\u3059\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4f8b<\/h3>\n
\u4e0a\u8a18\u306eAPI\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u30d7\u30ed\u30b0\u30e9\u30e0\u672c\u4f53\u3092HealthCheckByCurl\u3068\u3044\u3046Lambda\u95a2\u6570\u540d\u306b\u3057\u305f\u3068\u3057\u307e\u3059\u3002
\nLambda\u5185\u304b\u3089Lambda\u95a2\u6570\u3092\u547c\u3073\u51fa\u3059\u306b\u306flambda\u306e\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3067invoke\u3092\u4f7f\u7528\u3057\u307e\u3059\u306e\u3067Lambda Role\u306e\u30dd\u30ea\u30b7\u30fc\u306b\u306fLambda\u95a2\u6570\u3092\u5b9f\u884c\u3059\u308b\u6a29\u9650\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n\u3053\u306e\u3088\u3046\u306aAPI\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u30d1\u30e9\u30e1\u30bf\u3092\u6e21\u3057\u3066\u547c\u3073\u51fa\u3059\u30d7\u30ed\u30b0\u30e9\u30e0\u3092AWS Lambda\u306eScheduled Event\u3067\u5b9a\u671f\u7684\u306b\u52d5\u4f5c\u3055\u305b\u3001NG\u767a\u751f\u6642\u306b\u203b1\u306e\u7b87\u6240\u306bSNS\u3084SES\u306e\u30e1\u30fc\u30eb\u306a\u3069\u3067\u30a2\u30e9\u30fc\u30c8\u3092\u9001\u4fe1\u3059\u308b\u4ed5\u7d44\u307f\u3092\u7d44\u307f\u8fbc\u3081\u3070\u3088\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
\r\n# -*- coding: utf-8 -*-\r\n\r\nimport boto3\r\nimport base64\r\nimport json\r\n\r\nresult_ok = "healthcheck-ok"\r\nresult_ng = "healthcheck-ng"\r\n\r\ndef lambda_handler(event, context):\r\n try:\r\n client = boto3.client('lambda')\r\n \r\n #Lambda\u95a2\u6570HealthCheckByCurl\u306b\u30d1\u30e9\u30e1\u30bf\u3092Payload\u306b\u6e21\u3057\u3066\u5b9f\u884c\u3059\u308b\u3002\r\n response = client.invoke(\r\n FunctionName='HealthCheckByCurl',\r\n InvocationType='RequestResponse',\r\n LogType='Tail',\r\n Payload=b'{ \\\r\n "curl_cmd": "curl -s -S -F \\\\"data=sample\\\\" https:\/\/www.magtranetwork.com\/", \\\r\n "search_keyword": "magtranetwork", \\\r\n "ok_string": "healthcheck-ok", \\\r\n "ng_string": "healthcheck-ng" \\\r\n }'\r\n )\r\n \r\n #print "response:"\r\n #print response\r\n \r\n #\u30ec\u30b9\u30dd\u30f3\u30b9\u306ePayload\u3092\u8aad\u307f\u8fbc\u307fUTF-8\u3067\u30c7\u30b3\u30fc\u30c9\u3059\u308b\u3002\r\n response_result = response["Payload"].read().decode('utf-8')\r\n \r\n print(type(response_result))\r\n \r\n print "response_result:%s" % response_result\r\n\r\n #JSON\u5f62\u5f0f\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u5185\u5bb9\u3092\u53d6\u5f97\u3059\u308b\u3002\r\n result = json.loads(response_result)\r\n \r\n if result.has_key("result"):\r\n if str(result["result"]) != result_ok:\r\n #\u7d50\u679c\u304cOK\u306a\u3044\u5834\u5408\u306fLogResult\u3092base64\u3067\u30c7\u30b3\u30fc\u30c9\u3057\u3066\u30ed\u30b0\u5185\u5bb9\u3092\u51fa\u529b\u3059\u308b\u3002\r\n #\u203b1\u30a2\u30e9\u30fc\u30c8\u3092\u51fa\u3059\u5834\u5408\u306f\u3053\u3053\u306bSNS\u3084SES\u3067\u30e1\u30fc\u30eb\u3092\u9001\u4fe1\u3059\u308b\u30b3\u30fc\u30c9\u3092\u8a18\u8f09\u3059\u308b\u3002\r\n print "HealthCheck is NG."\r\n result_log = base64.b64decode(response["LogResult"])\r\n print "----------invokeHealthCheck result_log Start----------"\r\n print result_log\r\n print "----------invokeHealthCheck result_log End------------"\r\n else:\r\n print "HealthCheck is OK."\r\n \r\n print "result:%s" % result\r\n return result\r\n except Exception as ex:\r\n print "Exception Occurred:"\r\n print ex\r\n return "Exception Occurred: %s" % ex.message\r\n return {"result":"Exception Occurred: %s" % ex.message}\r\n finally:\r\n print "End HealthCheck."\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"Amazon API Gateway, AWS Lambda Python\u306e\u767b\u5834\u306b\u3088\u3063\u3066\u30d5\u30eb\u30de\u30cd\u30fc\u30b8\u30c9\u306a\u30b5\u30fc\u30d0\u74b0\u5883\u3067iPhone, Android\u306a\u3069\u306e\u30b9\u30de\u30fc\u30c8\u30d5\u30a9\u30f3\u30fb\u30e2\u30d0\u30a4\u30eb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306eAPI\u3092\u7c21\u5358\u306b\u4f5c\u6210\u3067\u304d\u308b\u3088 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[88,3,22,31,79,80,82],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/posts\/2305"}],"collection":[{"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/comments?post=2305"}],"version-history":[{"count":9,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/posts\/2305\/revisions"}],"predecessor-version":[{"id":5330,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/posts\/2305\/revisions\/5330"}],"wp:attachment":[{"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/media?parent=2305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/categories?post=2305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/tags?post=2305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}