Aveek Das
Child Function Executed

Calling an AWS Lambda function from another Lambda function

June 9, 2020 by

In this article, I am going to explain how to create an AWS Lambda function and then call this function from another Lambda function within the same region. This is a useful scenario in which we may need to execute a second lambda function based on the outcome of some previous logic. Another scenario may be to execute a second lambda function several times by using different parameters.

For the sake of this article, we will consider a typical retailer application, in which we can purchase different products from a retailer site using a lambda function.

Architecture Diagram

Figure 1 – Architecture Diagram

If you consider the above architecture diagram, you can see that we have an AWS lambda function – the ParentFunction, which assumes a specific role from the IAM (Invoke Other Lambda Function) and then calls another lambda function – the ChildFunction with a payload. Once the execution of the ChildFunction is completed, it returns a response, which is then passed on to the ParentFunction. The ParentFunction receives the response and handles the job accordingly.

As in this example, let us assume that the ParentFunction is going to call the ChildFunction with a payload of ProductName, Quantity, and the UnitPrice of that product. The ChildFunction, in turn, will process this payload, calculate the total sales amount, generate a transaction reference ID, and return this information to the ParentFunction.

Creating the first AWS Lambda Function – ChildFunction

Let us first go ahead and create the ChildFunction, which will process the input payload and return the results to the ParentFunction.

Head over to https://console.aws.amazon.com/ and login with your credentials. Once you are inside the console, start searching for “Lambda” and click on the first result that appears from the drop-down.

Search AWS Lambda Function

Figure 2 – Search AWS Lambda Function

This will take you to the Lambda Function homepage, where you can create a new lambda function by hitting the “Create Function” button.

Create AWS Lambda Function

Figure 3 – Create the AWS Lambda Function

Let the name of this function be – “ChildFunction” and select Python 3.8 as the runtime.

AWS Lambda Function Name

Figure 4 – Function Name

Select the option to Create a new role with basic lambda permissions and click on Create Function.

Selecting the Role

Figure 5 – Selecting the Role

A new lambda function will be created where you can write your code and test it.

AWS Lambda Function Created

Figure 6 -AWS Lambda Function Created

Let us now head over to Visual Studio Code and start writing our code for the ChildFunction as follows. This is a very simple application that is going to perform the following steps:

  1. Read data from the ParentFunction
  2. Generate the Transaction Reference ID
  3. Calculate the business information
  4. Return the result to the Parent Function

Child Function Code

Figure 7 – Child Function Code

Once the code is written and tested in the local machine, you can copy and paste the script to the lambda function to test it in AWS.

Copying code to Lambda Function

Figure 8 – Copying code to Lambda Function

In order to test the ChildFunction, you need to create the Test Event, in which you can pass the payload information that we will be using to call from the ParentFunction. In order to configure test events, click on Test Event, and select Configure.

Configure Test Events

Figure 9 – Configure Test Event

Give the test event a name and specify the payload information here to test it.

Configure Test Event

Figure 10 – Configure Test Event

Hit on Test to execute the ChildFunction with the payload information.

Testing the Function

Figure 11 – Testing the Function

If the execution is successful, you will get a successful return with the calculations completed as follows. As you can see in the figure below, the function returns the following items.

  1. Transaction ID
  2. Product Name
  3. Amount

This information will also be visible to the ParentFunction when it calls the ChildFunction.

Child Function Executed

Figure 12 – Child Function Executed

Also, copy the ARN of the Child Function, which can be used later to apply for policies and roles upon.

Copy ARN

Figure 13 – Copy ARN

Setting up the Policy for ParentFunction

In order to allow the ParentFunction to call the ChildFunction, we need to provide the ParentFunction with specific rights to call another lambda function. This can be done by adding specific policies to a role and then assign that role to the lambda function.

Head over to the IAM module inside the AWS portal and select Policies. Click on Create Policy to create a new one.

Create Policy

Figure 14 – Create Policy

In the Create Policy page, select the JSON tab and add the following policy summary to it as follows. Remember to update the URL for the Resource which you have copied in the previous step. Give the policy a suitable name and create it. I am going to name this policy as – “InvokeOtherLambdaPolicy”.

Adding Policy JSON

Figure 15 – Adding Policy JSON

Navigate to the Roles and click on Create role.

Create Role

Figure 16 – Create Role

Select Lambda as the use case and click on Next to add the required permissions.

Choose Use Case

Figure 17 – Choose Use Case

Add the following two policies to this role and create the role.

  1. AWSLambdaBasicExecutionRole
  2. InvokeOtherLambdaPolicy

Adding AWS Lambda Basic Execution Role

Figure 18 – Adding AWS Lambda Basic Execution Role

Adding Invoke Other Lambda Policy

Figure 19 – Adding Invoke Other Lambda Policy

Click on Next and proceed forward and create the role. Give this role a suitable name, for example, “InvokeOtherLambdaRole”.

Creating the AWS Lambda Function – ParentFunction

Head over to the Lambda Function page and click on Create New Lambda function. I am calling this lambda function – “ParentFunction”. Choose the run time as Python 3.8 and assign the InvokeOtherLambdaRole role that we just created in the previous step.

Creating the Parent Function

Figure 20 – Creating the Parent Function

Let us now again head over to Visual Studio Code to write the code and then copy-paste it back to the lambda editor. Since we are going to use an AWS resource in this function, we need to use the Boto3 python library to use the AWS resources. This library can be used to interact with other AWS resources as and when required.

Code for the Parent Function

Figure 21 – Code for the Parent Function

As you can see in the above code, we have created a boto client to interact with the lambda function. Also, we have created a payload that can be passed on to the ChildFunction when calling it. And once the ChildFunction is executed, it will return the response, which will be stored in the “response” variable.

Finally, we can parse the payload information from the response and use it according to our needs. In this case, we are just going to print it on the screen. Copy the code from VS Code to the lambda editor.

Parent Function

Figure 22 – Parent Function

Create a sample test event for this function since we are not going to pass any payload for this Parent Function here. Save the event and click on the Test.

Configuring Test Event

Figure 23 – Configuring Test Event

Once you execute the ParentFunction, it will pass the payload information to the ChildFunction, where the result will be calculated, and then the final response will be sent back from the ChildFunction to the ParentFunction. You can see the execution logs and confirm this as follows.

Parent Function Executed

Figure 24 – Parent Function Executed

Conclusion

In this article, I have explained how we can call or execute an AWS Lambda function from another lambda function within the same region. Using the AWS Lambda function, you can easily write serverless applications without having to worry about the infrastructure running behind it. Often, it becomes necessary that we might need to call different AWS Lambda functions from within another lambda due to the handling of complex business logic or something like that.

Aveek Das
AWS, AWS RDS, Functions

About Aveek Das

Aveek is an experienced Data and Analytics Engineer, currently working in Dublin, Ireland. His main areas of technical interest include SQL Server, SSIS/ETL, SSAS, Python, Big Data tools like Apache Spark, Kafka, and cloud technologies such as AWS/Amazon and Azure. He is a prolific author, with over 100 articles published on various technical blogs, including his own blog, and a frequent contributor to different technical forums. In his leisure time, he enjoys amateur photography mostly street imagery and still life. Some glimpses of his work can be found on Instagram. You can also find him on LinkedIn View all posts by Aveek Das

168 Views