All the different ways to invoke an AWS Lambda
Tags: aws, lambda, serverless
Published: 2022-11-11 21:15:00

In this post, we’ll go through the different ways to invoke an AWS Lambda function.
AWS Lambda can be invoked synchronously, asynchronously, or through event source mapping. Let’s explore each of them.

### Synchronous invocation
With synchronous invocation, the caller waits for the function to process the event and return a response. Details about the function response, including errors, are included in the response body and headers. When invoked synchronously, Lambda sends the request directly to the function and then the function’s response is sent back to the caller. When you invoke a function directly, you are responsible to examine the response, determining whether an error has happened and whether to retry.

You can invoke Lambda functions directly using the Lambda console, a function URL HTTP(S) endpoint, the Lambda API, an AWS SDK, the AWS Command Line Interface (AWS CLI), and AWS toolkits. You can also configure other AWS services to invoke your function synchronously, like API Gateway, CloudFront (Lambda@Edge), Elastic Load Balancer, etc.

![Sync invocation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdfxven4g0wey6f1tg75.png)

### Asynchronous invocation
When you invoke a function asynchronously, you don’t wait for a response from the function code. You hand off the event to Lambda and Lambda handles the rest. Lambda handles retries and can send invocation records to a destination.

With asynchronous invocation, Lambda queues the event for processing and returns a response immediately. Lambda places the event in a queue and returns a successful response without additional information. A separate process reads events from the queue and sends them to your function. If an error occurs, AWS will automatically retry the invoke.

Several services invoke Lambda function asynchronously: Simple Storage Service (S3), Simple Notification Service (SNS), Simple Email Service, Event Bridge, CloudWatch Logs, etc.

![Async invocation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6f5j5bee8hpcj9lpbgwz.png)

### Event source mapping
Lambda uses event source mapping to process items from a stream or queue. This invocation type is also known as poll-based invocation. You can use event source mappings to process items from a stream or queue in services that don’t invoke Lambda functions directly.

An event source mapping is a Lambda resource that reads events from an event source and sends them to your function in batches. Each event that your function processes can contain hundreds or thousands of items. You can configure the batching behavior by setting a batching window and batch size. A batching window is the maximum amount of time to gather resources into a single batch. Batch size is the maximum number of events in a single batch.

Lambda event source mappings process events at least once due to the distributed nature of its pollers. As a result, in rare situations, your Lambda function may receive duplicate events.

Lambda provides event source mappings for the following services: DynamoDb, Simple Queue Service (SQS), Kinesis, etc.

![Event Source Mapping](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p3n262cdxgnvwe1zeweo.png)

### Conclusion
Depending on who invokes your function and how it’s invoked, scaling behavior and the types of errors that occur can vary.