Invoke an AWS.Lambda function from another lambda function

We can use the AWS SDK to invoke another lambda function and get the execution response.

When we have multiple lambda functions which are dependent on one another then we may need the execution output of another lambda function in the current lambda function inorder to process it.

In such cases, we need to call/invoke the dependent lambda function from the current lambda function.

Let’s say we have two lambda functions

  1. process PDF contents - process_pdf_invoice
  2. update PDF contents in the database - save_invoice_info

We will be using nodejs to implement this scenario.

Function - process_pdf_invoice

  • Let’s write the code for process_pdf_invoice lambda function.

process_pdf_invoice.js

exports.handler = function(event, context) {
  // Let's return some dummy data
  const invoice = {
    "DueDate": "2013-02-15",
    "Balance": 1990.19,
    "DocNumber": "SAMP001",
    "Status": "Payable",
    "Line": [
      {
        "Description": "Sample Expense",
        "Amount": 500,
        "DetailType": "ExpenseDetail",
        "Customer": "ABC123 (Sample Customer)",
        "Ref": "DEF234 (Sample Construction)",
        "Account": "EFG345 (Fuel)",
        "LineStatus": "Billable"
      }
    ],
    "TotalAmt": 1990.19
  }
  return invoice

}

Function - save_invoice_info

  • Let’s write the code for lambda function save_invoice_info
  • To invoke the lambda function we need AWS SDK (i.e aws-sdk).
  • By default it will be available in AWS lambda if not we need to install it as a layer.

save_invoice_info.js

const AWS = require('aws-sdk');
const lambda = new AWS.Lambda();

async function getInvoiceInfo(){
  // params to send to lambda
  const params = {
    FunctionName: 'process_pdf_invoice',
    InvocationType: 'RequestResponse',
    LogType: 'None',
    Payload: '{}',
  };
  const response = await lambda.invoke(params).promise();
  if(response.StatusCode !== 200){
    throw new Error('Failed to get response from lambda function')
  }
  return JSON.parse(response.Payload);
}

exports.handler = async function(event, context) {
  // invoke and get info from `process_pdf_invoice`
  const invoice = await getInvoiceInfo();
  console.log('invoice', invoice);
  // now write the code to save data into database
  return {'status': 'saved'}
}
  • The code lambda.invoke(params).promise() will invoke lambda function and returns the response.
  • If the invokation is success then it will return 200 otherwise it will return 5XX code.
  • response.Payload will give the response returned by the lambda function.

References

  • https://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html
  • https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#invoke-property

Thank you for reading the Agiliq blog. This article was written by Anjaneyulu Batta on May 29, 2022 in AWSAWS LambdaNodejs .

You can subscribe ⚛ to our blog.

We love building amazing apps for web and mobile for our clients. If you are looking for development help, contact us today ✉.

Would you like to download 10+ free Django and Python books? Get them here