Invoking a lambda from a lambda

AWS lambda synchronous and asynchronous invocation using python

Dinesh Kumar K B
CodeX
2 min readJul 13, 2020

--

Photo by Ian Battaglia on Unsplash

Note: For non-members, this article is also available at https://dineshkumarkb.com/tech/invoking-a-lambda-from-a-lambda/.

We will discuss here how to invoke a second lambda function from a lambda function using python and pass the data to the same.

Below is the boto3 method signature to invoke a second lambda function from an existing lambda function.

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.invoke

You can invoke an aws lambda function either

  1. synchronously (and wait for the response) or
  2. asynchronously.

To invoke a function asynchronously, set the InvocationType to Event.

Event — Invoke the function asynchronously. The API response only includes a status code.

When the invocation type is set to Event, the invoking lambda does not expect a response from the invoked lambda.It just triggers and proceeds with other parts of code. This can be used when the second lambda executes some time consuming operations such as creating a aws sagemaker notebook instance and waits for it’s status to become InService which requires 2 minutes approximately.

To invoke a function synchronously, set RequestResponse to Event.

RequestResponse (default) — Invoke the function synchronously. Keep the connection open until the function returns a response or times out. The API response includes the function response and additional data.

The lambda invocation can be synchronous when the invoking lambda requires an acknowledgement from the invoked lambda.If the action is successful, the service sends back the following HTTP response.

For the RequestResponse invocation type, this status code is 200. For the Event invocation type, this status code is 202. For the DryRun invocation type, the status code is 204.

The Payload contains the data to be shared from lambda 1 to lambda 2. The payload should be of type json object which can be retrieved from lambda 2 as shown here.

https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html

--

--