AWS Lambda retry after certain time

Viewed 433

I have a lambda that performs, alongside with other things, a GET request, every day, at 5am, on some service, triggered by CloudWatchEvents.

This service may or may not have the data I need by the time queried.

Therefore, if the data is not there, I need to re-invoke the lambda, let's say, 6am. If it' still not there, again at 7am, and so on.

How can I accomplish that using AWS infrastructure?

2 Answers

This seems like a very good use case for Step Functions.

Step functions allow you to create a workflow with AWS Services including Lambda that allow for decision branches and wait loops.

For example you could create a workflow that is invoked daily at 5am where you invoke the lambda, the lambda can return whether it could process the data or that it needs to wait more. The step function will inspect the results and either end the workflow since the data was processed or go into a wait state for an hour and then retry the function.

Check out this article that includes code samples for a workflow that is similar to yours.

I had a similar situation, my lambda needed to change schedule on weekends and this is how I solved it.

def lambda_handler(event, context):
    reschedule_event()
    keep_working()

REGULAR_SCHEDULE = 'rate(20 minutes)'
WEEKEND_SHEDULE = 'rate(1 hour)'
RULE_NAME = 'My Rule'

def reschedule_event():
    """
    Cambia la planificación de la lambda, para que descanse los findes :D
    """
    sched = boto3.client('events')
    current = sched.describe_rule(Name=RULE_NAME)
    if is_weekend() and 'minutes' in current['ScheduleExpression']:
        sched.put_rule(
            Name=RULE_NAME,
            ScheduleExpression=WEEKEND_SCHEDULE,
        )
    if not is_weekend and 'hour' in current['ScheduleExpression']:
        sched.put_rule(
            Name=RULE_NAME,
            ScheduleExpression=REGULAR_SCHEDULE,
        )

I agree there must be some proper way to do this, but time was short at the moment and that lambda needed to go into production. You could do something alike to reschedule yours when there's no data to be retrieved and then go back to the original schedule.

Related