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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[AWS Lambda] Scheduling Events with AWS Lambda (a.k.a. Lambda cron jobs)相关的知识,希望对你有一定的参考价值。

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)的主要内容,如果未能解决你的问题,请参考以下文章

错误代码:AccessDeniedException。用户:arn:aws:iam::xxx:user/xxx 无权执行:lambda:CreateEventSourceMapping on reso

AWS - SQS 批量大小和 Lambda 方法

读取 aws lambda 包中的打包文件

AWS Cloud9:一次只部署一个 Lambda 函数

SQL服务器连接在lambda容器中工作正常,但是在将zip上传到aws lambda后失败

AWS lambda 和 AWS Lambda@EDGE 有啥区别?