Synchronous Invocation

Synchronous 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.
Asynchronous Invocation

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
)
Poll-based Invocation (Event Source Mapping)

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;
Amazon DynamoDB
Amazon Kinesis
Amazon MQ
Amazon Managed Streaming for Apache Kafka
self-managed Apache Kafka
Amazon Simple Queue Service
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.