Invoking one Lambda function inside another (Sync-Async)

Mahfuzul Alam
Sep 11 · 3 min read

Consider a transaction with two parties (Sender & Receiver). Once the transaction ends it is required to notify both sender and receiver via Email and SMS about the activity. While developing serverless application we need to keep in mind that a Lambda should have a unique responsibility. In accordance with that, in case of our scenario, we can create three separate Lambda functions to perform all these tasks. For example —

  1. sendMoneyFunction — lambda function that will take care of the transaction.
  2. sendEmailFunction — lambda function that will take care of sending the email
  3. sendSmsFunction — lambda function that will take care of sending SMS

An additional sampleSyncFunction to demonstrate synchronous invocation— a sample lambda function that will be invoked synchronously which will return a response and we will wait for response and log that in calling function for further processing.

Fig: Invoking one Lambda function inside another (Sync-Async)

Inside sendMoneyFunction we can call sendEmailFunction and sendSmsFunction synchronously or asynchronously.

In case of synchronous invocation, Lambda runs the function and waits for a response. When the function execution ends, Lambda returns the response from the function’s code with additional data such as the version of the function that was executed. With asynchronous invocation, Lambda queues the event for processing and returns a response immediately.

In our scenario, it’s preferable to invoke sendEmailFunction and sendSmsFunction asynchronously from sendMoneyFunction.

To enable a lambda function to invoke other lambda function we need to create a role and attach necessary AWS Lambda permission with that role. Jump to IAM console, create a role e.g. LambdaCanInvokeAnotherLambda (select Lambda as service type), click Next:Permissions , Click Create Policy, Click JSON tab and paste below permissions required to invoke one lambda inside another and logs permission.

Click Review Policy, give that policy and name (e.g. LambdaCanInvokeAnotherLambdaPolicy) and description(optional) and then click Create policy. Finally attach this policy to newly created role i.e. LambdaCanInvokeAnotherLambda. That’s all we need to do in IAM console.

Now let’s proceed with Lambda functions. Go to Lambda console and create a lambda function sendMoneyFunction with Python 3.7 runtime. Attach new created LambdaCanInvokeAnotherLambda in earlier steps to this function. For the sake of simplicity paste below code snippet into the function body —

Create a sendEmailFunction lambda function with basic lambda execution role and paste below code snippet in function body -

Again create a sendSmsFunction lambda function with basic lambda execution permission and then paste below code in function body —

If you check sendMoneyFunction code snippet you’ll see that we used Event as InvocationType meaning that all four invocations are asynchronous and don’t wait for response from the called lambda function.

If we want synchronous invocation we need to use RequestResponse as invocation type and save invocation response for further processing as required. Here, to demonstrate synchronous invocation let’s create another lambda function, give it a name e.g. sampleSyncFunction, choose Python 3.7 as runtime with basic lambda execution permission and paste below sample code in editor. And then click Save.

This function will return a dummy response based on received data from calling function which in turn will be saved or logged by the calling function for further processing.

Create a dummy test event and test sendMoneyFunction. If everything configured correctly sendMoneyFunction will do four invocations, two to sendEmailFunction and two to sendSmsFunction. Check respective Cloudwatch logs to verify these results.

Happy Serverless Computing!

Mahfuzul Alam

Written by

Software Analyst | AWS Certified Solutions Architect

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade