Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass path parameters using lambda invoke to another lambda function?

I'm trying to call and get the response from another lambda function using lambda invoke. The problem is other lambda function needs the id to be sent as path parameters (or as a query string). But I do not see an option in lambda invoke for this. If I pass the id in payload the other function will receive it in the event body and not as path parameters. Is there an existing solution for this?

Here is a function inside a lambda function which calls another lambda function which receives the data as query string parameters

function getWisIqLink(data) {
  const payload = {
    queryStringParameters: {
      userId: data.userId,
      eventId: data.eventId,
    }
  };
  const param = {
    FunctionName: 'consult-rest-api-dev-WisiqClassGet',
    InvocationType: "RequestResponse",
    Payload: JSON.stringify(payload)
  }

  return new Promise((resolve, reject) => {
  // console.log(`Starting promiseInvoke InvokeAsync with ES6 promise wrapper - ${functionName}`);
   lambda.invoke(param,(err, data) => {
      if (err) {
        reject(err);
      } else {
        resolve(JSON.parse(data));
      }
    }
  );
});
}

Here is a bit of a lambda function which receives the data as query strings (Not the function which receives data as path parameters)

module.exports.get = async function (event, context, callback) {

  const data = {
    userId: event.queryStringParameters.userId,
    eventId: event.queryStringParameters.eventId,

  };
like image 985
Yasith Prabuddhaka Avatar asked May 15 '18 04:05

Yasith Prabuddhaka


People also ask

Can a lambda function invoke 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.

Is it bad practice to invoke a Lambda from another Lambda?

It depends. There's still an issue of efficiency to consider. If you're invoking another Lambda function synchronously (i.e. when InvocationType is RequestResponse ) then you're paying for extra invocation time and cost: There is latency overhead for calling the 2nd function, especially when a cold start is involved.

Can you pass parameters to lambda function?

To configure a REST API to pass query string parameters to a backend AWS Lambda function, use a Lambda custom integration. To pass query string parameters to an HTTP endpoint, use an HTTP custom integration. Important: Make sure that the input data is supplied as the integration request payload.

Can Lambda call another Lambda Python?

Let us now see how we can call one lambda from 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.


1 Answers

The input to the Lambda function from API Gateway proxy integration is as follows.

{
"resource": "Resource path",
"path": "Path parameter",
"httpMethod": "Incoming request's method name"
"headers": {Incoming request headers}
"queryStringParameters": {query string parameters }
"pathParameters":  {path parameters}
"stageVariables": {Applicable stage variables}
"requestContext": {Request context, including authorizer-returned key-value pairs}
"body": "A JSON string of the request payload."
"isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"}

This schema is defined in here.

Your requirement is to pass path parameters from one lambda function (let's say Lambda-A) to another lambda function (Let's say Lambda-B). This means your Lambda-A function has to act as the API gateway that sends a request with above format to Lambda-B.

Hence your Lambda-A function should create "payload" object (please see the code sample that you have attached) as below. And in your Lambda-B, you may access the path parameters using "event.pathParameters".

const payload = {
  pathParameters: data.consulteeId
}
like image 168
Denis Weerasiri Avatar answered Sep 30 '22 02:09

Denis Weerasiri