Invoke a Lambda from another Lambda | aQb Solutions

Invoke a Lambda from another Lambda

In the past series of articles on AWS Lambda, you have seen the following :

  • How to set up Lambda on Amazon using Python
  • How to use your Lambda function to call your External API
  • How to call your Lambda function from a PHP application launched on an EC2 instance.

 

In this article, you will see how a lambda function can be called from another lambda function.

You will also see some use cases which illustrate the reasons for calling a lambda from another lambda.

 

Use cases where you call one lambda from another lambda

There are several occasions when you would like to do some tasks, by calling one lambda from another. Some of the use cases in which a lambda function can be used to call another lambda function are explained below:

 

Use Case 1: Using a Cron Lambda to call another lambda that does some job

There are use cases where you may want to set up one Lambda function as a cron task. This lambda function, in turn, could launch multiple instances of another lambda function that does some job.

Now, in this particular use case, we can also have one cron lambda function that is used to fire a non-lambda function, multiple times. However, in this case, the limitation is that the non-lambda function that performs the action cannot be consumed by other clients.

If, however, you use a cron lambda to call another lambda function to do the job, you could also allow other clients to call/invoke the action lambda.

 

Use case 2 : Dividing large jobs into multiple lambdas

AWS Lambda service comes with a number of limitations which can be kept in mind while architecting good solutions.

For instance, there are some hard limitations for the runtime environment; the disk space is limited to 512 MB, memory can vary from 128 MB to 1536 MB, and the execution timeout for a function can be maximized in 5 minutes.

Thus, due to the size and runtime restrictions imposed on Lambda’s, large jobs can be divided into multiple lambdas where one lambda function could be used to call another lambda function.

 

Steps that illustrate how a Lambda can be used to invoke another Lambda

Let us now see how we can call one lambda from another lambda.

 

Step 1: Configure your role so that a lambda function can use it to call another lambda

While writing a Lambda function the invokes another lambda, you will need a Role that is configured with the required policies that allow the invocation of another lambda.

In this case, the role to which the policies are attached is lambda_basic_execution. You can, however, create your own roles and attach the required policies as shown below.

Login to the AWS Management console and Choose IAM option under Security, Identity & Compliance. This will take you to the “Welcome to identity and Access Management” page.

 

 

In this page Click on Roles. This will take you to a page which allows you to create a new role or search for an existing role.

 

 

If you want to create a new role and attach required policies to it, then click on Create role button and continue further by attaching the required policies to it . In this case, however, we would be using the existing role i.e “ lambda_basic_execution”.

 

 

If you want to create a new role and attach required policies to it, then click on Create role button and continue further by attaching the required policies to it . In this case, however, we would be using the existing role i.e “ lambda_basic_execution”.

 

 

 

You need to ensure that  your role , lambda_basic_execution role must have the AWS Lambda Role policy attached to it as shown below. This policy allows the lambda function to invoke another lambda function.

If your lambda function does not use S3 you need not use AWS Lambda Execute policy.

AWS Lambda Basic Execution Role policy grants permissions including permissions for Amazon cloud watch actions that your lambda function needs.

 

Step 2: Create a Lambda function that calls another lambda

In this step, you will see how a lambda function can be created that invokes another lambda function can be created.

In the Services menu, click on Lambda in the Compute section. This will take you to a page that allows you to create a lambda function and search for existing lambda functions.

In this page, click on Create function button, to start creating a new lambda function.

 

 

This page allows you to provide a name for your Lambda function, choose your coding language, and choose a role that defines permissions for our function.

In this case, we have provided the name of the lambda function as call_lambda_lambda, used Python 3.6 as the coding language, and used an existing role i.e  lambda_basic_execution which contained the required permissions to invoke another lambda.

Click on Create function button to successfully create your lambda function, as shown below.

 

 

In the Function Code section, you can write code for your lambda function that invokes another lambda function.

Code :

 

 

Code : call_lambda_lambda
The following code shows the code written for call_lambda_lambda function that calls api call function.

[source language="code"]#import libraries import boto3 import json def lambda_handler(event, context): #Define object of lambda #Here service name is lambda and the region_name is the name of the region where the called function "api call" resides invoke Lam = boto3.client("lambda",region_name="us-east-1") #Define a payload that needs to be passed with a message payload = {"message": "Hi, You have been invoked"} #Call the lambda function "apical" by using the invoke method of the invokeLam object resp = invoke Lam.invoke(Function Name="apical", Invocation Type="Event", Payload=json.dumps(payload)) #resp = invoke Lam.invoke(Function Name="apical", Invocation Type="Request Response", Payload=json.dumps(payload)) print(resp) return "hello from lambda"[/source code]

 

<strong><em><b><i>In the above code, if you use the Event(asynchronous ) invocation option, the function will be invoked at least once in repose to an event, and the function must be idempotent to handle this.</i></b></em></strong>

<strong><em><b><i> </i></b></em></strong>

<strong><em><b><i>If, however, you use the Request Response(synchronous) invocation option, the function will be invoked only once.</i></b></em></strong>

<strong><em><b><i> </i></b></em></strong>

<strong><b>Code : apical</b></strong>

The following code shows the code written for call_lambda_lambda function that calls api call function.

#import libraries import boto3 import json def lambda_handler(event, context): #Define object of lambda #Here service name is lambda and the region_name is the name of the region where the called function “api call” resides invoke Lam = boto3.client(“lambda”,region_name=”us-east-1″) #Define a payload that needs to be passed with a message payload = {“message”: “Hi, You have been invoked”} #Call the lambda function “apical” by using the invoke method of the invokeLam object resp = invoke Lam.invoke(Function Name=”apical”, Invocation Type=”Event”, Payload=json.dumps(payload)) #resp = invoke Lam.invoke(Function Name=”apical”, Invocation Type=”Request Response”, Payload=json.dumps(payload)) print(resp) return “hello from lambda”[/source code]

 

In “apicall” function, the event will contain the payload of the lambda function “call_lambda_lambda”.

 

Step 3: Test your Lambda Function

Once, you have, written the code for your lambda function, click on Save button to save the code.

You can now test your lambda function by clicking on the Test button.

This will prompt you to configure your test event. Enter a name for the test event and click on Create
to create a new test event.

 

If your lambda function executes successfully without any errors, it will indicate the success message as shown below.

 

 

 

You can check the invidual log of call_lambda_lambda function to check if it was successfully executed. You can also check the individual log of apicall to check if it was successfully invoked by call_lambda_lambda.

 

Log : call_lambda_lambda

 image021

 

Log : api call

 


Hope, by now, you know how and why one lambda function is invoked by another lambda function.

Keep watching this space for more articles on AWS services.

 

Reference url’s:

 

https://lorenstewart.me/2017/10/02/serverless-framework-lambdas-invoking-lambdas/

 

https://www.oreilly.com/learning/how-do-i-invoke-a-lambda-from-another-lambda-in-aws

 

https://www.youtube.com/watch?v=5QwrseYLwNM&t=33s

 

https://www.youtube.com/watch?v=5QwrseYLwNM

 

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

 

https://serverfault.com/questions/703994/awslambdaexecute-policy-definition

 

https://www.youtube.com/watch?v=sb14OvPrtOk