Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda function to connect to a Postgresql database

Does anyone know how I can connect to a PostgreSQL database through an AWS Lambda function. I searched it up online but I couldn't find anything about it. If you could tell me how to go about it that would be great.

If you can find something wrong with my code (node.js) that would be great otherwise can you tell me how to go about it?

    exports.handler = (event, context, callback) => {
    "use strict"
     const pg = require('pg');
     const connectionStr = 
        "postgres://username:password@host:port/db_name";
var client = new pg.Client(connectionStr);
client.connect(function(err){
    if(err) {
        callback(err)
    }
    callback(null, 'Connection established');
});
context.callbackWaitsForEmptyEventLoop = false;
};

The code throws an error: cannot find module 'pg'

I wrote it directly on AWS Lambda and didn't upload anything if that makes a difference.

like image 785
vsarunhah Avatar asked Jun 23 '17 05:06

vsarunhah


2 Answers

I wrote it directly on AWS Lambda and didn't upload anything if that makes a difference.

Yes this makes the difference! Lambda doesnt provide 3rd party libraries out of the box. As soon as you have a dependency on a 3rd party library you need to zip and upload your Lambda code manually or with the use of the API.

Fore more informations: Lambda Execution Environment and Available Libraries

like image 156
MaiKaY Avatar answered Sep 19 '22 20:09

MaiKaY


You need to refer Creating a Deployment Package (Node.js)

Simple scenario – If your custom code requires only the AWS SDK library, then you can use the inline editor in the AWS Lambda console. Using the console, you can edit and upload your code to AWS Lambda. The console will zip up your code with the relevant configuration information into a deployment package that the Lambda service can run.

and

Advanced scenario – If you are writing code that uses other resources, such as a graphics library for image processing, or you want to use the AWS CLI instead of the console, you need to first create the Lambda function deployment package, and then use the console or the CLI to upload the package.

Your case like mine falls under Advanced scenario. So we need to create a deployment package and then upload it. Here what I did -

  1. mkdir deployment
  2. cd deployment
  3. vi index.js
  4. write your lambda code in this file. Make sure your handler name is index.handler when you create it.
  5. npm install pg
  6. You should see node_modules directory created in deployment directory which has multiple modules in it
  7. Package the deployment directory into a zip file and upload to Lambda.
  8. You should be good then

NOTE : npm install will install node modules in same directory under node_modules directory unless it sees a node_module directory in parent directory. To be same first do npm init followed by npm install to ensure modules are installed in same directory for deployment.

like image 30
Aniket Thakur Avatar answered Sep 17 '22 20:09

Aniket Thakur