Running a Lambda Function On A Schedule Using CloudWatch Events

aws Mar 18, 2019

Suppose you have a Lambda function that you need to execute on a schedule, e.g. every five minutes. Using the Hello Lambda function that we built previously, we can go on the AWS Console and set up a CloudWatch Event trigger.

First, we need to change the code so that our function can run without input.

Remove the input parameter from the FunctionHandler method, and change the SayHello method to return "Hello Lambda!". We also inserted a call to log the result before returning.

/// <summary>
/// A simple function that says hello
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public string FunctionHandler(ILambdaContext context)
	var result = SayHello();
    return result;

public string SayHello()
	return "Hello Lambda!";

Publish the code to AWS.

Next, go to the Lambda Management Console. Click on Functions, then search for HelloLambda. Click on HelloLambda in the results to configure the function.

On the Configuration tab in the next page, the available triggers are listed on the left column.

You can use other AWS services such as S3 or SQS to trigger execution of the function. Since we want to run on a schedule, we will add CloudWatch Events as a trigger.

Scroll down to the Configure triggers panel. We will create a new rule called ExecuteEvery5Minutes.

For the schedule expression, we can use a rate expression or a cron expression. Cron expressions are handy if your scheduling rules are more complex (e.g. every first Thursday of the month.) See the AWS docs for a full reference on writing schedule expressions.

Since the requirement is to run every five minutes, we have entered rate(5 minutes) in the Schedule expression field.

Untick Enable trigger if you don't want the trigger to be enabled immediately. Click Add to continue.

Back on the Configuration page, click Save on the top right corner to apply the changes.

And that's it! You can view the logs to verify that it is running as scheduled.

Click on the Monitoring tab , then View logs in CloudWatch button on the next page.

This will open up a window to CloudWatch logs for the HelloLambda function.

Check out the AWS documentation on Lambda functions to discover other cool features!

unsplash-logoVladimir Anikeev

Kristina Alberto

Software engineer from Sydney. I work at Domain Group.