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

血红的双手。 提交于 2019-12-21 20:06:22

问题


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,

  };

回答1:


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
}



回答2:


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


来源:https://stackoverflow.com/questions/50342504/can-i-pass-path-parameters-using-lambda-invoke-to-another-lambda-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!