قالب وردپرس درنا توس
Home / Tips and Tricks / How to Plan AWS Lambda Features with CloudWatch Events – CloudSavvy IT

How to Plan AWS Lambda Features with CloudWatch Events – CloudSavvy IT



AWS logo

If you want to run code regularly, like a cron job, you can use AWS CloudWatch events to automatically run serverless Lambda functions at fixed time intervals, without using any actual servers.

What are CloudWatch events?

CloudWatch is a service from AWS that monitors and responds to changes in your cloud infrastructure. It is most often used for logging and monitoring, but it works more generally as the glue that holds many applications together.

One of these features is CloudWatch events, a service that basically maps the cause of the impact. There are a bunch of different actions that can trigger events, such as punctual interruptions for EC2, bucket and object level operations for S3, etc. Then it can trigger other actions, such as running a lambda function, pub to SNS queue or many other actions.

The trick, in this case, is that the event does not have to be triggered by anything. There is another option for running events at regular intervals, either with a fixed time interval every five minutes, or by using cron syntax to define a specific period. You can bind this automatic event to the Lambda function so that it runs automatically.

Set a recurring function

Of course, you need a feature that you want to run. You can create these from the Lambda Management Console. If you only want a Hello World feature to test with CloudWatch events, you must log in to the event to have a record with a specific output.

'use strict';

exports.handler = (event, context, callback) => {
  console.log('LogScheduledEvent');
  console.log('Received event:', JSON.stringify(event, null, 2));
  callback(null, 'Finished');
};

Set up the function and test it with the built-in tools and see the logs and the latest inocations under the “Monitoring”

; tab.

lambda function

Go to the CloudWatch Management Console to create a new rule under Events> Sidebar Rules.

create a new rule

For the event source, switch it from “Event Pattern” to “Schedule” to set automatic events. You can use the fixed speed or use cron syntax to specify a more accurate time. You can read our guide to cron or use this online tool to help you with syntax, but the general format is:

minute hour day month weekday

event source

Add a new target for the target and select “Lambda function.” Select the name of the function you want to run.

Add a new goal, select

Click “Configure Details”, give it a name and create the rule. Your Lambda feature should start running and you can see the logs for it from Lambda’s monitoring tab or from CloudWatch logs.


Source link