10

I have this code in Lambda funcion:

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 run from EC2, it works fine. When I run from Lambda I get Error: Cannot find module 'pg'

2
  • 1
    Did you npm install before deploying the package? Commented Aug 30, 2015 at 10:52
  • Yes, but I packaged it the wrong way. Now I get: Unable to import module 'index': Error . Is my function must be called index.js ? Commented Aug 30, 2015 at 11:48

3 Answers 3

9

I am a super noob in Node JS, but I really wanted to try AWS Lambda. Here are the steps I took. I used a 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

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

7 Comments

Worked for me. I am on mac. I installed the node and npm from nodejs.org/en and it worked like a charm. Thanks a lot.
I've seen the "zip the contents not the folder" advice elsewhere, but I don't understand what that entails? In the Lamba UI you upload a single zip file...so I don't know how you get multiple files up there without zipping the folder :-( Any chance you could explain?
Okay I poked around and the missing steps are for "zip the contents not the folder": cd whateverfolder, zip -r deployment.zip ./*
@KatharineOsborne in the case I wrote in the answer I created a folder "the_function", you don't zip "the_function" folder, you create a zip (a single zip file) with the contents of "the_function"
for some reason lambda times out. Not sure why.
|
0

You can import easily only predifined libs to your lambda. For example You can use just boto3 and core for python, for java You can use just core. http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html You cannot import any additional libs in simple way. You can try to use "hard way". In this case You should save all necessary libraries to s3(or other place which You can access from lambda) and then copy to lambda environment (/tmp) and import it with the help of reflection.

Comments

0

Error: Cannot find module 'pg'

In my case I was just uploading index.js. We need to package 3rd party node modules as well.

  1. create index.js (name may vary based on your handler name)
  2. run npm install package
  3. It is better you create package.json will all dependencies you need and run mpn install
  4. Confirm node_modules folder is created in same directory.
  5. Zip these contents (index.js and node_modules folder) and upload the zip.
  6. You can upload directly or use S3.
  7. For more details read their offcial doc - Creating a Deployment Package (Node.js)

Now I get: Unable to import module 'index': Error . Is my function must be called index.js

In my case I was zipping the entire directory instead of it's contents. So you need to really do -

zip -r deploymentpkg.zip ./*

instead of

zip -r deploymentpkg.zip folder

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.