4

I have wrote a simple query call, My hello handler is this

'use strict';

const pg = require('pg');
const conn = 'pg://postgres:user:pass@rds_host:5432/database_name';

module.exports.hello = (event, context, callback) => {
  const client = new pg.Client(conn);
  client.connect();

  client.query('SELECT column_a FROM table_b', function(err, result) {
    client.end();
    if (err) {
      callback(null, {error: err});
    } else {
      const response = {
        statusCode: 200,
        body: JSON.stringify({
          data: result.rows
        }),
      };

      callback(null, response);
    }
  });

  // Use this code if you don't use the http event with the LAMBDA-PROXY integration
  // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
};

I have executed this script on my local by manual calling

const handler = require('../server/handler');

handler.hello({}, {}, function(err, response) {
  console.log(err, response);
});

and works, when I call

$ serverless invoke local -f hello -l

also works, but calling lambda always fails,

$ SLS_DEBUG=* serverless invoke -f hello -l 


{
    "errorMessage": "2017-04-21T01:11:19.580Z 697e69bc-262f-11e7-8fee-0331cc761e9a Task timed out after 6.00 seconds"
}
--------------------------------------------------------------------
START RequestId: 697e69bc-262f-11e7-8fee-0331cc761e9a Version: $LATEST
END RequestId: 697e69bc-262f-11e7-8fee-0331cc761e9a
REPORT RequestId: 697e69bc-262f-11e7-8fee-0331cc761e9a  Duration: 6000.71 ms    Billed Duration: 6000 ms        Memory Size: 1024 MB    Max Memory Used: 20 MB  
2017-04-21T01:11:19.580Z 697e69bc-262f-11e7-8fee-0331cc761e9a Task timed out after 6.00 seconds



  Error --------------------------------------------------

     Invoked function failed

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Stack Trace --------------------------------------------

Error: Invoked function failed
    at AwsInvoke.log (/home/rkmax/my-project/node_modules/serverless/lib/plugins/aws/invoke/index.js:122:31)
From previous event:
    at Object.invoke:invoke [as fn] (/home/rkmax/my-project/node_modules/serverless/lib/plugins/aws/invoke/index.js:22:10)
    at BbPromise.reduce (/home/rkmax/my-project/node_modules/serverless/lib/classes/PluginManager.js:210:55)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)
From previous event:
    at PluginManager.invoke (/home/rkmax/my-project/node_modules/serverless/lib/classes/PluginManager.js:210:22)
    at PluginManager.run (/home/rkmax/my-project/node_modules/serverless/lib/classes/PluginManager.js:225:17)
    at Serverless.run (/home/rkmax/my-project/node_modules/serverless/lib/Serverless.js:97:31)
    at serverless.init.then (/home/rkmax/my-project/node_modules/serverless/bin/serverless:23:50)

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Forums:        forum.serverless.com
     Chat:          gitter.im/serverless/serverless

  Your Environment Information -----------------------------
     OS:                 linux
     Node Version:       7.9.0
     Serverless Version: 1.11.0
1
  • I really can't figure out why this not working on lambda, tryied a lot now I'm using a query baed on events anything works fine on local but not on lamba even trying different examples from pg page doesn't work Commented Apr 21, 2017 at 5:01

2 Answers 2

1

Is your lambda in the same VPC and subnet as your Postgres database? If you created the lambda and you didn't explicitly state which subnet it belongs to, then it is effectively "public", meaning that it can access internet resources, DynamoDB, SNS, S3, ..., but it cannot talk to private RDS instances. To add your lambda to the VPC where the database lives go to the tab Configuration->Advanced Settings and set up something similar to the following with rules that show traffic enabled within the VPC... Advanced Settings with VPC & Subnets & Security Groups configured

Sign up to request clarification or add additional context in comments.

4 Comments

RDS is public I can access it from my laptop
Does your lambda have the correct IAM permissions? Maybe elevate it to FullAdmin and see if the query returns, then trim it down later.
Yes it have AdministratorAccess policy Full acess all resources
Add some console.log(...) lines in between each call to see how far the lambda is getting before timing out.
1

Add the following line at the beginning of your lambda.

exports.handler = function ( event, context, callback ) {
    //Instruct the lambda to exit immediately
    //and not wait for node event loop to be empty.
    context.callbackWaitsForEmptyEventLoop = false;
    /* Your code here */
};

For some reason querying databases causes the lambda to just hang until it times out. This setting tells the lambda to just stop when you call the callback.

We had this issue happen when we were querying to MySQL and had to escalate to Amazon Support before we got our answer.

7 Comments

Why are you passing back a success callback when your get an error from the database? Also how much memory have you given your lambda to run with?
I just trying to figure out the error, but there's no error is like lambda keep running like when you dont call process.exit()
Lambda is supposed to return when you invoke callback either as an error or success. My next suggestion is put console.log statements everywhere and work out where it's getting stuck
after put console.logs I found get execute all excep the code inside the client.query callback, also try wrapping on try/catch
Your query isn't returning. I'm not sure why.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.