4

I have a grpc service, written in python, deployed on one EC2 instance. I have written a nodejs application and using the application, I am able to call the grpc service from my local machine, and also from another EC2 instance. However when I deployed the same application to Lambda (using serverless deployment), it is not able to call the same grpc service. I have added below line in the scripts section in the package.json so the code is able to deployed to lambda properly.

"postinstall": "npm rebuild grpc --target=12.x --target_arch=x64 --target_platform=linux --target_libc=glibc"

Initially the lambda was executing without any error, just that it was not calling the grpc service. After that I added VPC endpoint configuration in my serverless.yml file, it is returning Internal Server Error and logging error "EACCES: permission denied, open '/var/task/handler.js'" in cloudwatch.

Update: I updated the IAM roles, and now there is no error logged, but the lambda always respond "Go Serverless v1.0! Your function executed successfully!". None of the messages that I am trying to log in the callback to utterQuery method is logged in cloudwatch logs.

What could be wrong here.

Here is the serverless.yml file:

service: myservice

provider:
  name: aws
  runtime: nodejs12.x
  vpc:
    securityGroupIds:
      - securityGroupid1
    subnetIds:
      - subnetId1
    stage: dev
  region: us-east-1
  stackTags:
    owner: me

functions:
  sendMessage:
    handler: handler.sendMessage
    events:
      - http:
          path: sendMessage
          method: post

Here is the lambda function code:

'use strict';
const AWS = require('aws-sdk');
const grpcClient = require('./grpcClient');

module.exports.sendMessage = async (event, context) => {
  const timestamp = new Date().getTime();
  console.log(event, timestamp);
  console.log(event.body);

  const message = event.body;

  let reply = 'Go Serverless v1.0! Your function executed successfully!';


     grpcClient.utterQuery({
       query: message,
       user_id: 10101,
       session_id: 321
     }, (error, riaReply) => {
         if (error) {
           console.error(error)
         } else {
           console.log('successfully queried grpc service.')
           console.log(riaReply.response)
         }
     });

  return {
    statusCode: 200,
    body: reply
  };
};
4

1 回答 1

1

Since you're calling return at the end of the handler, the lambda invocation is ending before the utterQuery() call has a chance to complete and invoke is callback.

https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html suggests that instead you return a promise, and the node.js lambda runtime will know to wait for that promise to be fulfilled before ending the lambda.

于 2019-12-26T22:06:08.607 回答