Learn how to create AWS LAMBDA functions This execute on a scheduled interval and much like a cron job would. In this lesson we'll create a LAMBDA function that checks for a string of text on a website to verify the website are up and operational. The lambda function logs to CloudWatch Metrics and sends a notification e-mail using an SNS queue if the check fails.
Create a lambda function: (WebTest)
Exports.handler =function(event, context) {varHTTP = require (' http '); varOptions ={host:' Staticsite.willbutton.co ' } varregex =/hello world/;//which'll falid varCB =function(response) {varstr = ""; 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" and "SNS":
Create a new topic:give the name "Webstiedown".
Then "Create a new Subscription":
It'll 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'll run the lambda function every 5 mins to check whether the website are in good condition. If anything wrong, it'll fire SNS-e-mail to send you a email.
[AWS Lambda] scheduling Events with AWS Lambda (a.k.a. Lambda cron jobs)