My colleague, Ryan Manikowski, recently wrote a great blog giving an overview of AWS Lambda. His article mentioned that AWS Lambda Functions can be triggered by a number of potential events and services. In this 4-part blog series I am going to cover the Lambda “Scheduled Event” in-depth.
FYI, here is the official AWS documentation on using Scheduled Events to drive Lambda functions.
Lambda was originally designed to be an “event driven” service, meaning that it was reactionary in nature. Lambda functions were intended to be written to handle some event (e.g. SNS notification, Kinesis, DynamoDB) and perform some unit of work based on that event. But what if you want to use Lambda in a non-reactionary way? What if you want to invoke your Lambda function at some regular interval to perform whatever task you see fit? At AWS re:Invent in October 2015, AWS announced (at the same time they announced Python function support. Woohoo!) that they were adding support for Scheduled Events to Lambda.
So you can now create Lambda Scheduled Events, via CloudWatch Events Schedule Rules, using either a fixed rate or a cron expression to define your schedule. Pretty sweet.
Some Technical Details
As I just mentioned above, you can use either of two methods to define a schedule for invoking a Lambda function – rate and cron expressions.
The rate expression uses the form: rate(Value Unit)
- The Value must be a positive integer, and the Unit must be either minute(s), hour(s), or day(s).
- The rate frequency must be at least 1 minute.
- A singular value (e.g. 1) must use the singular tense of the unit and, likewise, plural values (e.g. 2 or more) must use the plural tense of the unit.
ex: rate(1 hour) would trigger a Lambda function every 1 hour (perpetually)
ex: rate(5 minutes) would trigger a function every 5 minutes (perpetually)
The cron expression uses the form: cron(Minutes Hours Day-of-month Month Day-of-week Year)
- All time is referenced against UTC.
- All fields are required.
- One of the day-of-month or day-of-week values must be a question mark (?)
ex: cron(0/15 * * * ? *) would trigger a Lambda function at 0, 15, 30, and 45 minutes past the hour, every hour of every day.
ex: cron(0 23 ? * MON-FRI *) would trigger a Lambda function at 11:00PM UTC Monday through Friday.
If you need your function to execute at very specific points in time (e.g. every 15 minutes starting at the top of the hour), use the cron schedule expression. The rate schedule expression will start when you create the Scheduled Event rule and then run at the rate defined thereafter. Meaning if you used rate(15 minutes) as your schedule expression and create that Scheduled Event at 9:53AM, it would start at 9:53 and then kick-off every 15 minutes after that (10:08, 10:23, 10:38, …). That may or may not be an issue depending on your use-case.
Complete detailed information and specifications on scheduled expressions can be found here.
Input data (i.e. parameters)
Along with the rate expression, a scheduled event can also provide a Lambda function with Input data (in a JSON formatted string), which will be handed to the event handler function as a data object (a dictionary in the case of python). This can be useful if you need to pass your function parameters or arguments, as opposed to “hard-coding” values in your function. I’ll include an example of this below. While supplying Input data is not necessarily required, it is something that can and should be used in a number of applications.
Check back next week for part 2 of this blog series – how to set up scheduled events.
-Ryan Kennedy, Sr Cloud Consultant