最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I pass path parameters using lambda invoke to another lambda function? - Stack Overflow

programmeradmin0浏览0评论

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));
      }
    }
  );
});
}

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,

  };

Share Improve this question edited Apr 26, 2019 at 5:12 Yasith Prabuddhaka asked May 15, 2018 at 4:47 Yasith PrabuddhakaYasith Prabuddhaka 9171 gold badge12 silver badges18 bronze badges 2
  • 1 Lambda functions only take two inputs, event and context -- not "path parameters" -- so it isn't clear what you are saying the second function needs. Can you show us that code? – Michael - sqlbot Commented May 15, 2018 at 9:17
  • Sure. I'll edit the question to include the second function. Yes the Lambda functions only take event and context. The problem is with the way how the function decodes the event. For a function which receives event data by path parameters it has to decode it like module.exports.get = async function (event, context, callback) { const data = { id: event.pathParameters.id }; – Yasith Prabuddhaka Commented May 16, 2018 at 8:13
Add a comment  | 

2 Answers 2

Reset to default 14

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
}

You can invoke the Lambda as ApiGatewayRequest. The ApiGatewayRequest has body and header. You can pass the parameters in the header. Following is the code.

 public ApiGatewayProxyResponse invokeLambda(LambdaService lambda, Object data, Map<String, String> headers) 
{
     ApiGatewayRequest request = new ApiGatewayRequest();
     request.setBody(data);
     request.setHeaders(headers);
     ApiGatewayProxyResponse response = lambda.execute(request);
     return response.getBody();
}
发布评论

评论列表(0)

  1. 暂无评论