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
  };
};
24x7Programmer
  • 474
  • 5
  • 20
  • 2
    I am guessing it's due to your IAM role of the lambda function. The error should contain the action name for which the error occurred. Update the role and it should work. If you could share the full error log, it would be easier to understand. – Md khirul ashik Dec 22 '19 at 06:09
  • 2
    Yes, it was due to IAM Role. I could resolve the error. But now I found that the Lambda is not able to call the GRPC service. The reply from the lambda is always ''Go Serverless v1.0! Your function executed successfully!'' – 24x7Programmer Dec 23 '19 at 06:13
  • @24x7Programmer Have you figured out why grpc wasn't making call? I am having same issue – mamur Nov 26 '20 at 00:16

1 Answers1

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.

erik258
  • 14,701
  • 2
  • 25
  • 31