问题
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
回答1:
[Removed previous answer and update] Now, AWS Lambda provides a per minute frequency CloudWatch Events - Schedule as a trigger option.
回答2:
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.
回答3:
Using the boto module, you can have a lambda function run an invoke statement, invoking itself. The following will run every ~60 seconds. Of course make sure you assign an appropriate role with permissions. Also, note your limits.
import boto3,time
def lambda_handler(event, context):
#do something
time.sleep(60)
client = boto3.client('lambda')
response = client.invoke(FunctionName='YOUR-FUNCTION-NAME')
来源:https://stackoverflow.com/questions/35272697/invoke-aws-lambdas-regularly-every-1-minute