How to run SQL on PostgreSQL RDS from a Lambda function in Node.js?

I have this code in a Lambda function:

sql="SELECT ..."; var pg = require('pg'); var connectionString = 'postgres://...'; var client = new pg.Client(connectionString); client.connect(); var query = client.query(sql); query.on('end', function() { client.end(); }); 

When I start from EC2, it works fine. When I run from Lambda, I get an error: I can not find the module "pg"

+7
lambda postgresql
source share
3 answers

I am a super noob in Node JS, but I really wanted to try AWS Lambda. Here are the steps I took. I used Ubuntu 14.04.

 sudo apt-get update sudo apt-get install nodejs sudo apt-get install npm sudo ln -s /usr/bin/nodejs /usr/bin/node mkdir the_function && cd the_function mkdir node_modules npm install pg ******Now create a file index.js and paste the content of your funcion. console.log('Loading S2 Function'); var pg = require("pg"); exports.handler = function(event, context) { var conn = "pg://user: password@host :5432/bd_name"; var client = new pg.Client(conn); client.connect(); var query = client.query("SELECT * FROM BLA WHERE ID = 1"); query.on("row", function (row, result) { result.addRow(row); }); query.on("end", function (result) { var jsonString = JSON.stringify(result.rows); var jsonObj = JSON.parse(jsonString); console.log(jsonString); client.end(); context.succeed(jsonObj); }); }; ******Now zip the contents of the_function folder (not the_function folder itself) 

You can check the official sample from this AWS link: http://docs.aws.amazon.com/lambda/latest/dg/walkthrough-s3-events-adminuser-create-test-function-create-function.html

+8
source share

You can easily import only predefined libraries into your lambda. For example, you can use only boto3 and core for python, for java. You can only use the kernel. http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html You cannot import any additional libraries in a simple way. You can try to use the "hard way". In this case, you must save all the necessary libraries in s3 (or another place that you can access from the lambda), and then copy it to the lambda environment (/ tmp) and import it using reflection.

0
source share

Error: cannot find module 'pg'

In my case, I just downloaded index.js. We also need to pack third-party node modules.

  • create index.js (the name may differ depending on your handler name)
  • run npm install package
  • You better create package.json all the necessary dependencies and run mpn install
  • The confirm node_modules folder is created in the same directory.
  • Close this content (index.js and node_modules folder) and download zip.
  • You can download directly or use S3.
  • Read their offcial doc for details - Creating a Deployment Package (Node.js)

Now I get: Failed to import module 'index': Error. Is my function named index.js

In my case, I zipped the whole directory, not its contents. So you really need to -

 zip -r deploymentpkg.zip ./* 

instead

 zip -r deploymentpkg.zip folder 
0
source share

All Articles