blueskin February 2016

Invoke AWS Lambda(s) regularly every 1 minute

How can invoke a AWS Lambda regularly, specifically every 1 minute? The current functionality allows Lambdas to be setup with 5 minute trigger but I am looking for a much lesser time interval . I thought of running the Lambda for ever but looks like that can't be done since the Maximum execution duration per request 300 seconds

Answers


E.J. Brennan February 2016

There was a session at AWS Reinvent in 2015 that covered this exact topic, you can watch it here on youtube: https://www.youtube.com/watch?v=FhJxTIq81AU shows how to use lambda and cloudwatch to get that 1 minute frequency with no external dependencies.

Do you need to run an AWS Lambda function on a schedule, without an event to trigger the invocation? This session shows how to use an Amazon CloudWatch metric and CloudWatch alarms, Amazon SNS, and Lambda so that Lambda triggers itself every minuteā€”no external services required! From here, other Lambda jobs can be scheduled in crontab-like format, giving minute-level resolution to your Lambda scheduled tasks. During the session, we build this functionality up from scratch with a Lambda function, CloudWatch metric and alarms, sample triggers, and tasks.

I suspect that at some point AWS will allow the 1 minute interval without using this method, but this may hold you over in the mean time.


blueskin March 2016

The Cloudwatch metric and alarms based triggering mechanism isn't quite straight forward to setup and gets complicated when you have more than 1 Lambda requiring to be invoked every 1 minute. What I eventually ended up is

  1. Create a new Lambda called "minute_trigger" which invokes every 5 minutes and
  2. Set the timeout for Lambda to 5 minutes.
  3. Within the lambda, sleep(60), and after sleep, invoke the Lambda(s) that you want.

    from boto3 import client as bc
    lambda_client = bc('lambda')
    lambda_client.invoke(FunctionName=lambda_name, InvocationType='Event', Payload=msg)
    

Note, the "minute_trigger" should approximately cost 66$ per year as of today's pricing. Also remember to give the "minute_trigger" lambda the appropriate role to invoke other lambdas.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt123422567890",
      "Effect": "Allow",
      "Action": [
        "lambda:InvokeFunction"
      ],
      "Resource": "*"
    }
  ]
}

Post Status

Asked in February 2016
Viewed 1,761 times
Voted 11
Answered 2 times

Search




Leave an answer