AWS Lambda is one of the first services that comes to mind when thinking about serverless applications. By default, it comes up with auto-scaling, monitoring, logging and many more features. One of the main things to understand while working with Lambda is how we invoke our functions. Join me and let’s focus on Lambda invocation types.
Lambda functions can be invoked manually by using the Lambda API, or Lambda polls the supported services on your behalf and runs your functions.
Using the Lambda API, we can invoke our Lambda functions asynchronously and synchronously. Let's check how we do this by invoking our sample Lambda function, which we deployed through an SAM, using both AWS CLI and the boto3 library.
You can check out the project's GitHub repo for the full source code.
This example contains the different ways to invoke AWS Lambda functions with using Python and boto3.
github.com/sufleio/aws-lambda-invocation-types-examplesSynchronous invocation is the most straightforward way to invoke an AWS Lambda function. Once a function is invoked this way, it runs immediately and Lambda returns the response after completion, with additional data if specified. Here is an example;
Setting –invocation-type flag as “RequestResponse” or not at all indicates that the invocation will be synchronized.
aws lambda invoke --function-name
lambda-invocations-test-HelloWorldFunction-fj7C3Qud6zEx --invocation-type
RequestResponse outfile.txt
Here’s the CLI invocation response below, StatusCode of the response of synchronous invocations is 200 for successful executions. We should determine by ourselves if the function failed and retrying to invoke is required when performing a synchronous invocation.
{
"ExecutedVersion": "$LATEST",
"StatusCode": 200
}
We specified in our CLI invocation that the response to be written to outfile.txt. The next figure shows the contents of this file.
Now let’s see how to invoke the same function with Python SDK. We use the boto3 package to create the client and invoke the function.
client = boto3.client('lambda')
response = client.invoke(
FunctionName=args.name,
InvocationType="RequestResponse",
Payload=payload
)
print(response['Payload'].read())
python test_lambda.py --name
lambda-invocations-test-HelloWorldFunction-fj7C3Qud6zEx --type sync
We can see the response of the Lambda function immediately by reading the content of the payload.
When a Lambda function is wanted to be invoked asynchronously, an invocation request takes place in the event queue. Unlike the Synchronous invocation method, it returns only the status code. If the function returns an error, AWS will try to invoke twice more with a time gap between. It’s one minute between the first and the second attempts, two minute between the second and third attempts. For Asynchronous Invocation, —invocation-type flag must be “Event” in the CLI command. Check below for a sample;
aws lambda invoke --function-name
lambda-invocations-test-HelloWorldFunction-fj7C3Qud6zEx
--invocation-type Event async.txt
StatusCode of the response for the asynchronous invocations is 202 for successful executions.
{
"StatusCode": 202
}
Similarly to synchronous, we invoke the Lambda function asynchronously by using the boto3 package. The only difference is the InvocationType parameter as mentioned before.
response = client.invoke(
FunctionName=args.name,
InvocationType="Event",
Payload=payload
)
AWS Lambda can be integrated with supported services, so Lambda can read events from services to invoke the functions. As long as events are available, Lambda performs Synchronous invokes and waits for response. If the function succeeds, Lambda keeps polling and invoking repeatedly as long as there are events available in the stream after polling.
Event source mappings read events in batches of configurable size. Maximum batch size is specific for each service.
Supported services are;
Let’s see how to use this type of invocation with DynamoDB Stream. This is how we define the DynamoDB table and stream to poll in our YAML file.
ProcessDynamoDBStream:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_dynamodb_handler
Runtime: python3.8
Policies: AWSLambdaDynamoDBExecutionRole
Events:
Stream:
Type: DynamoDB
Properties:
Stream: !GetAtt DynamoDBTable.StreamArn
BatchSize: 100
StartingPosition: TRIM_HORIZON
DynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: email
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
StreamSpecification:
StreamViewType: NEW_IMAGE
Let’s create some dummy events in a file named records.json as if they were written to DynamoDB and invoke our Lambda function with these events using Python SDK for testing purposes.
python test_lambda.py --name
lambda-invocations-test-ProcessDynamoDBStream-c4Qzt7lQY6Zi --payload records.json --type sync
We can see how our function processed from the SAM logs by running this command;
sam logs -n lambda-invocations-test-ProcessDynamoDBStream-c4Qzt7lQY6Zi
Finally, let’s see how Lambda does poll the stream and invokes the function by itself by adding a new item to the DynamoDB table from AWS Console.
As you can see from the figure below, the orange marked logs are the logs of the Lambda function invoked after deleting an item from the table. The pink ones are the logs after adding data to the table.
You can also check our previous blog post on processing SQS messages with AWS Lambda to learn how to use Poll-based invocation on Amazon SQS.
Learn how to process SQS messages with AWS Lambda in minutes and create scalable event-driven systems without provisioning or managing servers.
www.sufle.io/blog/process-sqs-messages-with-aws-lambdaAn enthusiastic developer, Beyza is eager to follow and leverage next-gen technologies for modern application development. Her interest in adopting transforming practices for excellent applications follows her passion for learning and sharing skills and emerging technologies.
Subscribe to Our Newsletter
Our Service
Specialties
Copyright © 2018-2024 Sufle
We use cookies to offer you a better experience with personalized content.
Cookies are small files that are sent to and stored in your computer by the websites you visit. Next time you visit the site, your browser will read the cookie and relay the information back to the website or element that originally set the cookie.
Cookies allow us to recognize you automatically whenever you visit our site so that we can personalize your experience and provide you with better service.