One of the many advantages of AWS Lambda is its ability to scale on demand. However, there are scenarios where you do not want your Lambda function to execute immediately; rather, you need to defer or delay its execution. AWS Lambda is designed to execute as soon as it is invoked, but what if you need to delay the execution?

While there’s no built-in feature to defer the execution of Lambda functions, you can employ several practical workarounds. In this blog post, we will explore three different approaches to defer the execution of AWS Lambda functions.

1. AWS Step Functions

AWS Step Functions offer a fantastic way to build visual workflows and introduce delays within your serverless architecture. The Wait state in Step Functions allows you to delay for a specified time, after which the next state will be executed. You can set it to wait until a specific timestamp before transitioning to the next state. The timestamp must be a string in the ISO 8601 format, and in UTC time. Maximum state execution idle time is 1 year.

To defer a Lambda function execution, you can wrap the function with a Step Functions state machine and add a Wait state before invoking the function. This setup will result in the state machine waiting for the specified time before invoking your Lambda function.


{
  "StartAt": "WaitState",
  "States": {
    "WaitState": {
      "Type": "Wait",
      "Seconds": 60,
      "Next": "MyLambdaFunction"
    },
    "MyLambdaFunction": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
      "End": true
    }
  }
}

2. AWS EventBridge

EventBridge, formerly CloudWatch Events, allows you to set rules that trigger your Lambda function either on a fixed schedule (e.g., every X minutes/hours/days) or based on a cron expression.

To defer your Lambda function execution, you can schedule it using EventBridge, where you set the rule to trigger your function after a specific delay. This approach effectively delays the time before your Lambda function is invoked.

The cron or rate expressions in EventBridge inherently create recurring schedules for Lambda execution. But what if we want to execute a function only once at a specified future time? Deleting the rule from AWS Lambda execution can be one solution.

3. Amazon Simple Queue Service (SQS) with Delay Queues

Amazon SQS is a robust message queuing service for decoupling and scaling microservices, distributed systems, and serverless applications. Amazon SQS Delay Queues allow you to postpone the delivery of new messages to a queue for several seconds, up to a maximum of 900 seconds (15 minutes). If you create a delay queue, any messages that you send to the queue remain invisible to consumers for the duration of the delay period. The delay period begins at the moment you send the message and ends when the delay period is over.

  
sqs = boto3.client('sqs')

response = sqs.create_queue(
    QueueName=queue_name,
    Attributes={
        'DelaySeconds': str(delay_seconds)  # Delay all messages in a queue.
    }
)
 

For individual messages, the Amazon SQS Message Timer allows you to specify an initial invisibility period for a message. The period of invisibility can be anywhere between 1 second and 15 minutes, precisely like the delay queues.

  
sqs = boto3.client('sqs')
response = sqs.send_message(
    QueueUrl=queue_url,
    MessageBody='This is my test message.',
    DelaySeconds=10  # Delay message for 10 seconds
)
print(f'Successfully sent message with ID: {response["MessageId"]}')
 

In contrast, Visibility Timeout in Amazon SQS is the period during which Amazon SQS prevents other consuming components from receiving and processing the message. It’s essentially a lock mechanism that ensures a message is not processed multiple times. You can also use it as a hack to achieve the same.

Real Life Use Cases

Customer Email Engagement

Suppose you are developing a web application for an e-commerce platform and want to send email notifications to users about upcoming sales or new product releases. However, you don’t want to send these notifications instantly; rather, you’d prefer to delay them to a specific time to increase engagement. Here, you can use Amazon SQS delay queues to defer the execution of your Lambda function that sends emails. You push a message (containing user email information and the email body) to the Amazon SQS delay queue, and it will trigger your Lambda function after the specified delay period.

Scheduled Reporting

Imagine you are building an analytics dashboard for a company that requires weekly performance reports. These reports take a significant amount of time and resources to generate, so running them during business hours can affect the performance of your application. In this case, you can use AWS EventBridge to defer the execution of the Lambda function responsible for generating these reports. You can schedule the event rule to trigger the Lambda function during off-peak hours, ensuring minimal impact on your application’s performance.

Order Processing in E-commerce

For an e-commerce application, when a customer places an order, several steps must be followed in a particular order: validating the order, charging the customer, notifying the warehouse, etc. However, some steps require a delay, like waiting for authorization from the payment gateway. In this scenario, you can use AWS Step Functions to model your workflow. You can use the ‘Wait’ state to delay the execution of the Lambda function until the payment authorization is received. This state ensures that the function is only executed when necessary and resources aren’t wasted checking for payment authorization repeatedly.

Conclusion

In conclusion, while AWS Lambda is designed for immediate execution, you have several practical options to delay the invocation of your functions. If your delay requirement is less than 15 minutes and depends on the event, leveraging Amzon SQS delay queue will be a fitting approach. If your delays involve complex logic, need to exceed 15 minutes, and have conditional aspects, choosing AWS Step Functions will be the optimal solution. For scenarios that require scheduling, AWS EventBridge can be your preferred choice.

Be sure to check out more such insightful blogs in my Master Lambda: Demystifying Serverless Computing for Robust Application Development series, for a deeper dive into Lambda's best practices and advanced features. Stay tuned and keep learning!