码迷,mamicode.com
首页 > 其他好文 > 详细

[AWS Lambda] Scheduling Events with AWS Lambda (a.k.a. Lambda cron jobs)

时间:2016-05-19 20:59:50      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

Learn how to create AWS Lambda functions that execute on a scheduled interval, much like a cron job would. In this lesson we will create a Lambda function that checks for a string of text on a website to verify the website is up and operational. The lambda function logs to CloudWatch Metrics and sends a notification email using an SNS queue if the check fails.

 

Create a lambda function: (WebTest)

exports.handler = function(event, context){
    var http = require(‘http‘);
    var options = {
        host: ‘staticsite.willbutton.co‘
    }
    
    var regex = /Hello World/; // which will falid
    
    var cb = function(response){
        
        var str = "";
        response.on(‘data‘, function(chunk){
            str += chunk;
        });
        
        response.on(‘end‘, function(){
            if(regex.test(str)){
                console.log("WEBSITE PASSED");
                context.succeed(‘Yech!‘);
            }else{
                console.log("WEBSITE FAILED");
                context.fail(‘Boo‘);
            }
        })
    }
    
    http.request(options, cb).end();

 

Then go to "Service" --> "SNS":

Create a new Topic: give the name "Webstiedown".
Then "Create a new Subscription":

技术分享

 

It will send you a confrim email.

 

Then go to the "Service" --> "Cloud Watch"

"Create a alarm" --> "select Lambda metrics" --> config options, select error type

技术分享

 

技术分享

Save it.

 

Then Later we need CloudWatch event source, so "create new rule": point to the lambda function

技术分享

Save it.

 

Go the lambda function, add a new event source:

技术分享

 

Now it will run the lambda function every 5 mins to check whether the website is in good condition. If anything wrong, it will fire SNS -> email to send you a email.

[AWS Lambda] Scheduling Events with AWS Lambda (a.k.a. Lambda cron jobs)

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5509778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!