Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda Scheduled Tasks

People also ask

Can AWS Lambda Functions be scheduled?

You can set up a rule to run an AWS Lambda function on a schedule. This tutorial shows how to use the AWS Management Console or the AWS CLI to create the rule. If you would like to use the AWS CLI but have not installed it, see the AWS Command Line Interface User Guide.

How do you trigger Lambda function every 24 hours?

To trigger a Lambda function once every 20 or 24 hours, we can schedule a trigger in CloudWatch Events. CloudWatch Events allows targets to be triggered using a Schedule Expression. A Schedule Expression can define a rate; for example, every 24 hours. Or can accept a standard cron job expression.


Native Support for Scheduled Events added October 8, 2015:

As announced in this AWS blog post, scheduling is now supported as an event source type (also called triggers) called "CloudWatch Events - Schedule", and can be expressed as a rate or a cron expression.

Add Scheduled Event to a new lambda

Navigate to the 'Configure triggers' step of creation, and specify the 'CloudWatch Event - Schedule' trigger. Example configuration below:

Image that shows configuration for creating a scheduled event at 5pm UTC.

Add Scheduled Event to an existing lambda

Navigate to the 'Triggers' tab of your lambda, select 'Add Trigger', and specify the 'CloudWatch Event - Schedule' trigger. Example screenshot where I have an existing lambda with an SNS trigger:

Image that shows how to navigate to add trigger UI from Lambda console.

Once loaded, the UI to configure this trigger is identical to the screenshot in the above "Add Scheduled Event to a new lambda" section above.

Discussion

For your example case, you'll want to use cron() instead of rate(). Cron expressions in lambda require all fields and are expressed in UTC. So to run a function every day at 5pm (UTC), use the following cron expression:

cron(0 17 * * ? *)

Further Resources

  • AWS Documentation - Schedule Expressions Using Rate or Cron
  • AWS Documentation - Run an AWS Lambda Function on a Schedule Using the AWS CLI
  • AWS Documentation - Tutorial: Using AWS Lambda with Scheduled Events
    • AWS has provided a sample "blueprint" that uses a cron expression called lambda-canary that can be selected during function creation from the AWS console.
    • This tutorial walks you through configuration of this blueprint.

Notes

  • The name of this event type has changed from "Scheduled Event" to "CloudWatch Events - Schedule" since this feature was first released.
  • Prior to the release of this feature, the recommended solution to this issue (per "Getting Started with AWS Lambda" at 42min 50secs) was to use SWF to create a timer, or to create a timer with an external application.
  • The Lambda UI has been overhauled since the scheduled event blog post came out, and the screenshots within are no longer exact. See my updated screenshots above from 3/10/2017 for latest revisions.

Since the time of this post, there seems to have risen another solution: Schedule Recurring AWS Lambda Invocations With The Unreliable Town Clock (UTC) in which the author proposes subscribing to the SNS topic Unreliable Town Clock. I've used neither SWF nor SNS, but it seems to me that the SNS solution is simpler. Here's an excerpt from the article

Unreliable Town Clock (UTC)

The Unreliable Town Clock (UTC) is a new, free, public SNS Topic (Amazon Simple Notification Service) that broadcasts a “chime” message every quarter hour to all subscribers. It can send the chimes to AWS Lambda functions, SQS queues, and email addresses.

You can use the chime attributes to run your code every fifteen minutes, or only run your code once an hour (e.g., when minute == "00") or once a day (e.g., when hour == "00" and minute == "00") or any other series of intervals.

You can even subscribe a function you only want to run only once at a specific time in the future: Have the function ignore all invocations until it’s after the time it wants. When it is time, it can perform its job, then unsubscribe itself from the SNS Topic.

Connecting your code to the Unreliable Town Clock is fast and easy. No application process or account creation is required


NEW SOLUTION: Lambda Scheduled Jobs

Werner Vogel has announced tonight (10/08) at re:Invent that AWS Lambda now has it's own scheduler.

Se the AWS Lambda release note on 2015-10-08 :

You can also set up AWS Lambda to invoke your code on a regular, scheduled basis using the AWS Lambda console. You can specify a fixed rate (number of hours, days, or weeks) or you can specify a cron expression. For an example, see Walkthrough 5: Using Lambda Functions to Process Scheduled Events (Python).


OLD SOLUTION: Scheduling with AWS Data Pipeline

You can use AWS Data Pipeline to schedule a task with a given period. The action can be any command when you configure your Pipeline with the ShellCommandActivity.

You can for example run an AWS CLI command to:

  • Put a message to SQS
  • or directly invoke a Lambda function (see invoke)

You can easily create the AWS Data Pipeline scheduled task directly within AWS console (e.g. with an AWS CLI command) :

enter image description here

You can also use the API to define your scheduling:

{
 "pipelineId": "df-0937003356ZJEXAMPLE",
 "pipelineObjects": [
    {
      "id": "Schedule",
      "name": "Schedule",
      "fields": [
        { "key": "startDateTime", "stringValue": "2012-12-12T00:00:00" }, 
        { "key": "type", "stringValue": "Schedule" }, 
        { "key": "period", "stringValue": "1 hour" }, 
        { "key": "endDateTime", "stringValue": "2012-12-21T18:00:00"  }
       ]
     }, {
      "id": "DoSomething",
      "name": "DoSomething",
      "fields": [
        { "key": "type", "stringValue": "ShellCommandActivity" },
        { "key": "command", "stringValue": "echo hello" },
        { "key": "schedule", "refValue": "Schedule" }
      ]
    }
  ]
}

Limits: Minimum scheduling interval is 15 minutes.
Pricing: About $1.00 per month.


Here is how I do it:

  • Create Lambda which:

    • purges given SQS
    • sends there message with delay 10 minutes
    • https://gist.github.com/mikeplavsky/5ffe7e33e0d70a248537
  • Create CloudWatch Alarm for: ApproximateNumberOfMessagesVisible > 0 for 1 minute

  • Subscribe SNS Topic to the Alarm
  • Subscribe Lambda to SNS Topic

Now you have a timer with approximately 15 minutes resolution.

Then other Lambda functions are subscribed to SNS Topic and called every 15 minutes.


Since it is now easily possible to trigger lambda functions over HTTP (e.g. using GET or curl) a simple solution is to use a managed CRON like easycron: https://www.easycron.com/ to trigger your lambda function into running.

We had the same problem and ended up running a cron service on Google App Engine in python since this allowed for more flexibility and complexity in the CRON job itself.


In the Function page, Add trigger, you can add a CloudWatch Events, and make it as a schedule type

enter image description here