{"id":219,"date":"2015-11-16T12:02:05","date_gmt":"2015-11-16T03:02:05","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/11\/16\/try-lambda-scheduled-event-job"},"modified":"2015-11-16T12:02:05","modified_gmt":"2015-11-16T03:02:05","slug":"try-lambda-scheduled-event-job","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/11\/16\/try-lambda-scheduled-event-job\/","title":{"rendered":"AWS Lambda\u306eScheduled Event\u3092\u4f7f\u3063\u3066\u30d0\u30c3\u30c1\u5b9f\u884c\u3057\u3066\u307f\u305f"},"content":{"rendered":"
\u5148\u65e5\u30cf\u30fc\u30c9\u30b3\u30a2\u306e\u30e9\u30a4\u30d6\u306b\u884c\u3063\u3066\u304d\u3066\u3001\u8033\u304c\u307e\u3060\u304a\u304b\u3057\u3044\u4e0b\u689d\u3067\u3059\u3002\u3084\u306f\u308a\u8033\u6813\u306f\u5fc5\u9808\u3067\u3059\u3002<\/p>\n
\u3055\u3066\u3001\u8a71\u306f\u5909\u308f\u308b\u304c\u3001\u6700\u8fd1\u79c1\u304c\u95a2\u308f\u3063\u3066\u3044\u308b\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u306f\u3001\u6bd4\u8f03\u7684\u5358\u7d14\u306aWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u3082\u65e5\u6b21\u306a\u3069\u3067\u7c21\u5358\u306a\u30d0\u30c3\u30c1\u51e6\u7406\u3092\u3057\u305f\u3044\u3068\u3044\u3046\u72b6\u6cc1\u306f\u610f\u5916\u3068\u591a\u3044\u3002<\/p>\n
\u5f0a\u793e\u3067\u306f\u3001\u3053\u308c\u307e\u3067\u30d0\u30c3\u30c1\u306e\u5b9a\u6642\u5b9f\u884c\u306b\u306fRundeck\u306a\u3069\u306e\u30b8\u30e7\u30d6\u7ba1\u7406\u30c4\u30fc\u30eb\u3084\u3001\u5834\u5408\u306b\u3088\u3063\u3066\u306fcron\u3092\u4f7f\u3063\u3066\u304d\u305f\u3002\u3057\u304b\u3057\u3053\u308c\u3089\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u554f\u984c\u304c\u3042\u308b\u3002<\/p>\n
\u305d\u3053\u3067\u3001\u4ee5\u4e0b\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u3088\u3046\u30d0\u30c3\u30c1\u306e\u5b9f\u884c\u65b9\u6cd5\u306b\u3064\u3044\u3066\u691c\u8a0e\u3092\u884c\u3044\u3001\u6700\u8fd1\u516c\u958b\u3055\u308c\u305fAWS Lambda\u306eScheduled Event\u3092\u5229\u7528\u3057\u305f\u4ed5\u7d44\u307f\u3092\u4f5c\u3063\u3066\u307f\u305f\u3002\u306a\u304a\u3001\u6bd4\u8f03\u7684\u7c21\u5358\u306aWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u3001\u30a2\u30d7\u30ea\u30b5\u30fc\u30d0\u4e0a\u3067\u30d0\u30c3\u30c1\u3092\u5b9f\u884c\u3057\u3001\u30d0\u30c3\u30c1\u5c02\u7528\u30b5\u30fc\u30d0\u306a\u3069\u306f\u7528\u610f\u3057\u306a\u3044\u524d\u63d0\u3067\u3042\u308b\u3002<\/p>\n
\u5177\u4f53\u7684\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u4ed5\u7d44\u307f\u3068\u3057\u305f\u3002<\/p>\n
Lambda\u5074\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u3056\u3063\u304f\u308a\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u3042\u308b\u3002\u305b\u3063\u304b\u304f\u306a\u306e\u3067Python\u306b\u3057\u305f\u3002
\n\u306a\u304a\u3001slackweb\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067Lambda\u5074\u306b\u5165\u3063\u3066\u3044\u306a\u3044\u305f\u3081\u3001\u30ed\u30fc\u30ab\u30eb\u3067\u4f5c\u696d\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3001lambda_function.py\u3068\u542b\u3081\u3066zip\u30d5\u30a1\u30a4\u30eb\u306b\u5727\u7e2e\u3057\u3066\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n
# -*- coding: utf-8 -*-\nimport urllib\nimport urllib2\nimport slackweb\n\nSLACK_URL="Slack\u306eWebHook URL"\nBATCH_URL="\u30d0\u30c3\u30c1\u5b9f\u884c\u7528\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8"\nBATCH_TOKEN="\u30d0\u30c3\u30c1\u5b9f\u884c\u7528\u8a8d\u8a3c\u30c8\u30fc\u30af\u30f3"\n\ndef lambda_handler(event, context):\n try:\n values = { 'token': BATCH_TOKEN }\n data = urllib.urlencode(values)\n req = urllib2.Request(BATCH_URL, data)\n response = urllib2.urlopen(req)\n result = response.read()\n\n except:\n slack = slackweb.Slack(url=SLACK_URL)\n slack.notify(text='batch api call error')\n return\n\n slack = slackweb.Slack(url=SLACK_URL)\n slack.notify(text=result)<\/code><\/pre>\nWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u5074\u3067\u306f\u30d0\u30c3\u30c1\u306e\u5b9f\u884c\u7d50\u679c\u3092HTTP\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u306b\u683c\u7d0d\u3057\u3001\u305d\u308c\u3092Lambda\u304b\u3089\u305d\u306e\u307e\u307eSlack\u306b\u901a\u77e5\u3057\u3066\u304a\u308a\u3001\u6bce\u65e5\u7d50\u679c\u306e\u76e3\u8996\u304c\u3067\u304d\u308b\u3002<\/p>\n
\u305f\u3060\u3057\u3001AWS Lambda\u306e\u95a2\u6570\u5b9f\u884c\u6642\u9593\u306f\u6700\u95775\u5206\u3067\u3042\u308a\u30015\u5206\u4ee5\u4e0a\u304b\u304b\u308b\u3088\u3046\u306a\u30d0\u30c3\u30c1\u3067\u306f\u4e0a\u8a18\u306e\u65b9\u6cd5\u306f\u305d\u306e\u307e\u307e\u306f\u63a1\u7528\u3067\u304d\u306a\u3044\u3002\u307e\u305f\u3001\u8907\u96d1\u306a\u4f9d\u5b58\u95a2\u4fc2\u306a\u3069\u3092\u6301\u3063\u305f\u30d0\u30c3\u30c1\u51e6\u7406\u3067\u306f\u3001\u3084\u306f\u308a\u30b8\u30e7\u30d6\u7ba1\u7406\u30c4\u30fc\u30eb\u306e\u5c0e\u5165\u306f\u5fc5\u8981\u306b\u306a\u308b\u3068\u601d\u3046\u3002
\n\u305f\u3060\u3001\u7c21\u5358\u306a\u30d0\u30c3\u30c1\u3067\u3042\u308c\u3070\u4e0a\u8a18\u306e\u3088\u3046\u306a\u4ed5\u7d44\u307f\u3067\u5b9f\u73fe\u3067\u304d\u305f\u3002<\/p>\n
AWS Lambda\u3092\u4f7f\u3063\u3066\u307f\u3066<\/h2>\n
\u79c1\u306fAWS Lambda\u3092\u304d\u3061\u3093\u3068\u89e6\u3063\u305f\u306e\u306f\u521d\u3081\u3066\u3060\u3063\u305f\u306e\u3060\u304c\u3001\u500b\u4eba\u3068\u3057\u3066\u306e\u611f\u60f3\u3092\u5c11\u3057\u66f8\u3044\u3066\u307f\u308b\u3002<\/p>\n
\u6700\u8fd1\u3001\u30b5\u30fc\u30d0\u30ec\u30b9\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u3068\u3044\u3046\u8a00\u8449\u3092\u8033\u306b\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u304d\u305f\u3002\u305d\u306e\u80cc\u666f\u3068\u3057\u3066\u306fAWS Lambda\u306e\u767b\u5834\u304c\u5927\u304d\u304f\u3001AWS\u306e\u5404\u7a2e\u30d5\u30eb\u30de\u30cd\u30fc\u30b8\u30c9\u30b5\u30fc\u30d3\u30b9\u3092AWS Lambda\u3067\u3064\u306a\u304e\u8fbc\u3080\u3053\u3068\u3067\u3001\u81ea\u524d\u3067\u30b5\u30fc\u30d0\u3092\u4e00\u5207\u7ba1\u7406\u3059\u308b\u3053\u3068\u306a\u3057\u306b\u304b\u306a\u308a\u69d8\u3005\u306a\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u3002<\/p>\n
AWS Lambda\u3092\u89e6\u3063\u3066\u307f\u3066\u3001\u305d\u306e\u6577\u5c45\u306f\u3042\u307e\u308a\u9ad8\u304f\u306a\u3044\u3068\u611f\u3058\u305f\u3057\u3001\u3055\u304f\u3063\u3068\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u66f8\u3044\u3066\u30b5\u30fc\u30d0\u30ec\u30b9\u3067\u69d8\u3005\u306a\u3053\u3068\u304c\u3067\u304d\u308b\u624b\u8efd\u3055\u30fb\u67d4\u8edf\u3055\u306f\u975e\u5e38\u306b\u9b45\u529b\u7684\u3060\u3068\u601d\u3063\u305f\u3002\u958b\u767a\u8005\u3068\u3057\u3066\u3082\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u3059\u308b\u306e\u3068\u306f\u307e\u305f\u3072\u3068\u5473\u9055\u3046\u697d\u3057\u307f\u65b9\u3082\u3067\u304d\u308b\u3002\u3053\u306e\u30b5\u30fc\u30d3\u30b9\u3068\u3053\u306e\u30b5\u30fc\u30d3\u30b9\u3092\u7d44\u307f\u5408\u308f\u305b\u308c\u3070\u3053\u3093\u306a\u3053\u3068\u3082\u3067\u304d\u308b\u3058\u3083\u306a\u3044\u304b\u3068\u3044\u3063\u305f\u30d1\u30ba\u30eb\u306e\u3088\u3046\u306a\u697d\u3057\u307f\u3067\u3042\u308b\u3002<\/p>\n
\u3057\u304b\u3057\u4e00\u65b9\u3067\u3001\u91cd\u8981\u306a\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u304c\u5165\u3063\u3066\u3044\u308b\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092AWS Lambda\u3092\u4e2d\u5fc3\u3068\u3057\u305f\u30d5\u30eb\u30de\u30cd\u30fc\u30b8\u30c9\u30b5\u30fc\u30d3\u30b9\u3092\u6838\u306b\u3057\u3066\u5b9f\u73fe\u3059\u308b\u3068\u306a\u308b\u3068\u3001\u30d9\u30f3\u30c0\u30fc\u30ed\u30c3\u30af\u30a4\u30f3\u306e\u61f8\u5ff5\u304c\u767a\u751f\u3057\u3066\u304f\u308b\u306e\u3082\u78ba\u304b\u3067\u3042\u308b\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5b9f\u969b\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u304a\u3044\u3066\u3001AWS Lambda(\u304a\u3088\u3073\u30d5\u30eb\u30de\u30cd\u30fc\u30b8\u30c9\u30b5\u30fc\u30d3\u30b9)\u3092\u3069\u3053\u307e\u3067\u4f7f\u3046\u304b\u306f\u3001\u6642\u3068\u5834\u5408\u306b\u5fdc\u3058\u3066\u5224\u65ad\u3057\u3066\u3044\u304f\u3053\u3068\u306b\u306a\u308b\u3068\u601d\u3046\u3002\u4f8b\u3048\u3070\u3001\u4eca\u56de\u3054\u7d39\u4ecb\u3057\u305f\u30d0\u30c3\u30c1\u5b9f\u884c\u30b7\u30b9\u30c6\u30e0\u306e\u3088\u3046\u306a\u672c\u8cea\u7684\u306b\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u3068\u95a2\u4fc2\u306a\u304f\u30d9\u30f3\u30c0\u30fc\u30ed\u30c3\u30af\u30a4\u30f3\u306e\u61f8\u5ff5\u304c\u5c0f\u3055\u3044\u7b87\u6240\u3060\u3063\u305f\u308a\u3001\u3042\u307e\u308a\u9577\u671f\u904b\u7528\u3057\u306a\u3044\u30b5\u30fc\u30d3\u30b9\u3084\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u306a\u3069\u3092\u7d20\u65e9\u304f\u4f5c\u308a\u305f\u3044\u3068\u3044\u3063\u305f\u5834\u5408\u3001AWS Lambda\u306f\u975e\u5e38\u306b\u6709\u52b9\u306a\u30c4\u30fc\u30eb\u306b\u306a\u308b\u3068\u601d\u3063\u305f\u3002<\/p>\n
\u304a\u5ba2\u69d8\u306b\u3068\u3063\u3066\u6700\u9069\u306a\u5f62\u306e\u30b7\u30b9\u30c6\u30e0\u3092\u30b9\u30d4\u30fc\u30c7\u30a3\u30fc\u306b\u3054\u63d0\u4f9b\u3059\u308b\u305f\u3081\u3001\u4f7f\u3044\u6240\u3092\u8003\u3048\u306a\u304c\u3089\u6709\u52b9\u6d3b\u7528\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u8003\u3048\u3066\u3044\u308b\u3002<\/p>\n