Dmytro Hrimov

Senior software engineer

AWS Lambda: Invocation types

Introduction

With this blog post, I want to open a series of articles about AWS Lambda, small details you may notice later as you start working with Lambda. You will come to it when you face it and discover something fishy is happening.

With this series, I want to share my experience with AWS Lambda, tricky or not-so-obvious things, and explain certain things that may help you understand why these things happen in the first place.

I want to open this series with a short post related to AWS Lambda invocation types.

AWS Lambda: Invocation types

AWS Lambdas have three different invocation types:

  • Synchronous
  • Asynchronous
  • Poll-based

Now that we know all possible invocation types, let's dive into each in more detail.

AWS Lambda: Synchronous invocation

With synchronous invocation, a client waits for a Lambda to complete processing and return a response. If some process invokes a Lambda, this call will be a blocking call.

Here are some examples of synchronous Lambda invocation sources:

  • Amazon API Gateway
  • Amazon Elastic Load Balancing (Application Load Balancer)
  • AWS CLI
  • AWS SDK

Here is a small example of how AWS SDK invocation in Python using boto3 looks like:

import boto3

lambda_client = boto3.client("lambda")

if __name__ == "__main__":
    response = lambda_client.invoke(
        FunctionName='test'
    )
    response_body = response["Payload"].read().decode("utf-8")

AWS Lambda: Asynchronous invocation

When invoking asynchronously, the client starts an execution and immediately gets an answer without waiting for a Lambda to complete the processing and return a response. The processing is happening in the background asynchronously. The client call is not a blocking call.

Here are some examples of asynchronous Lambda invocation sources:

  • S3 events
  • CloudWatch events
  • EventBridge events
  • SNS notifications

Here is also an AWS SDK example of how you can start an asynchronous invocation of a Lambda:

import boto3

lambda_client = boto3.client("lambda")

if __name__ == "__main__":
    response = lambda_client.invoke(
        FunctionName='test',
        InvocationType='Event'
    )

Notice here that we have an `InvocationType` value passed in. The response will not contain a `Payload` field.

AWS Lambda: Poll-based invocation

With poll-based invocation, AWS has a process that continuously polls a data source for new events, and when new events happen, the process invokes a Lambda.

One of the most used flows is an SQS-triggered Lambda. When configuring a Lambda with an SQS trigger, the process continuously polls SQS for new messages. You can configure this trigger type to use batches and configure batch processing with a maximum batching window. The batching window is the time frame that the process will wait for a batch to complete; otherwise, the process will invoke a Lambda with an incomplete batch. Be aware that if you use batch without configuring the max batching window, AWS can still wait up to 20 seconds for a batch to complete. This implicit wait time may be crucial for time-sensitive processes. An additional option to help you with processing failures and retries is "Report Batch Item Failures". But it is not part of the topic for now.

Final words

That concludes the first topic of the series on AWS Lambdas. Please reach out if you have questions or think some information needs to be updated and corrected. Thank you so much for your attention!