avatar
Configure nodejs lambda connect amazon RDS postgres RDS

> Follow the link here to create Amazon RDS Postgres for practicing. Here is sample configuration for Amazon RDS Postgres.

HOST: powerbi.bcclinf78ccs.ap-southeast-1.rds.amazonaws.com
DATABASE: powerbi
USERNAME and PASSWORD: flagtickpostgres / qqi881&^pLVJ

> Let create JavaScript & Node.js applications in your local machine and be ready to deploy AWS Lambda. Follow up the link here.

serverless create --template aws-nodejs --path postgres-service --name PostgresService

> Configure file serverless.yml as below:

# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
#    docs.serverless.com
#
# Happy Coding!

service: PostgresService
# app and org for use with dashboard.serverless.com
#app: your-app-name
#org: your-org-name

# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs14.x
  region: ap-southeast-1

functions:
  hello:
    handler: handler.index
    name: getPowerBiConnection
    description: Get PowerBI Connection from Amazon RDS

> Insert node_modules, package.json and package-lock.json into the project. That avoid issues happening upon use external libraries to handler in handler.js. These extra files and folders can wrap as zip folder and deploy as layer in Lambda function when use in AWS cloud.

Run npm install to update dependencies in node_modules and type nul > run.ts to create a test script to run lambda function locally.

npm install
type nul > run.ts

> We will update handler.js as below

const pg = require('pg');

exports.handler = async (event, context) => {
    
  var dbConfig = {
      user: "flagtickpostgres",
      password: "qqi881&^pLVJ",
      database: "powerbi",
      host: "powerbi.bcclinf78ccs.ap-southeast-1.rds.amazonaws.com",
      port: '5432'
  };
  const pool = new pg.Pool(dbConfig);
  let client = await pool.connect();
  
  let queryExample = "SELECT * FROM \"questions\"";

  let res = await new Promise((resolve) => {
     client.query(queryExample, (err, res) => {
        if (err !== null) {
            resolve(err);
        } else {
            resolve(res);
        }
        client.end();
      }); 
  });
  
  const response = {
      statusCode: 200,
      body: JSON.stringify(res),
  };
  return response;
};

> We use run.ts to make configure as running locally for test lambda function.

import { handler } from './handler';

const main = async () => {
    const res = await handler({} as any);
    console.log(res);
}

main();

> First and foremost, you need to install esbuild-register and observe package.json as below:

{
  "name": "pg",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "pg": "^8.7.1"
  },
  "devDependencies": {
    "esbuild": "^0.16.8",
    "esbuild-register": "^3.4.2"
  }
}

> How to run script in run.ts as below:

node -r esbuild-register run.ts

> Observe Batch Mode upon run command line

node -r esbuild-register run.ts
{
  statusCode: 200,
  body: '<Your Data>'
}
24
PostgreSQL list all tables postgreSQL query table insert record postgres create AWS Postgres SQL RDS Data Loading to PostgreSQL using PGAdmin Insert Data in PostgreSQL in PGAdmin
You need to login to do this manipulation!