Invoking a synchronous Lambda function
When you invoke a function synchronously, the Lambda service runs the function and waits for a response. In this example, an API request is received from a browser client to Amazon API Gateway. API Gateway invokes the Lambda function by calling the AWS Lambda service. While the function runs, it can optionally call other AWS services. When the function completes, the Lambda service returns the response from the function's code with additional data, such as the version of the function that was invoked. If the function encounters an error, the error is returned as the response. The Lambda service returns the response to API Gateway which in turn sends the response to the browser client.
Another option for a synchronous call to Lambda is to use a function URL. A function URL is a dedicated HTTP(S) endpoint for your Lambda function. In the example above, the URL request is sent directly to the Lambda service from a browser client. The Lambda service invokes the Lambda function which returns a URL response. The response is returned to the browser client.
When you create a function URL, Lambda automatically generates a unique URL endpoint for you. After you create a function URL, its URL endpoint never changes. Lambda function URLs use resource-based policies for security and access control. Function URLs also support cross-origin resource sharing (CORS) configuration options. CORS defines a way for client web applications that are loaded in one domain to interact with resources in a different domain.
While function URLs are simple and easy to set up, the browser application will require an update if the URL changes. When a function changes behind Amazon API Gateway, no changes are required to the browser application.
Invoking an asynchronous Lambda function
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. The Lambda service places the event in a queue and returns a success response without additional information. A separate process reads events from the queue and sends them to the function. You can configure how Lambda handles errors and can send invocation records to a downstream resource such as Amazon Simple Queue Service (Amazon SQS) or Amazon EventBridge to chain together components of your application.
Several AWS services, such as Amazon Simple Storage Service (Amazon S3) and Amazon Simple Notification Service (Amazon SNS), invoke functions asynchronously to process events. If scheduled events are required, you can use EventBridge as a scheduler to invoke Lambda functions. If an AWS service doesn’t have a direct integration with the Lambda service, EventBridge can serve as the event bus to route the request. An example of a scheduled event is daily reporting or any recurring process that should be activated.
Lambda event source mappings for queues and streams
An event source mapping is a Lambda resource that reads from an event source and invokes a Lambda function. You can use event source mappings to process items from a stream or queue in services that don't invoke Lambda functions directly. The Lambda service can poll AWS services like Amazon DynamoDB, Amazon Kinesis, Amazon SQS, and Amazon DocumentDB (with MongoDB compatibility).
In the above diagram, the target event source is a DynamoDB stream. When the Lambda service polls the DynamoDB stream for events, a number of events are returned to the Lambda service. The Lambda service batches records together in a single payload and invokes the Lambda function with the payload. You can configure the Lambda function to specify the maximum size of the batch window and the payload. The payload can’t exceed the Lambda function input limit of 6 MB.
An example scenario would be if a customer order changed status to delivery in progress in the DynamoDB orders table, then the Lambda function is invoked to send a notification to the customer and do some financial processing.
In the next article in this series we will have an example of a lambda functions, and will learns about lambda layers.
Top comments (0)