I cannot connect to EC2 Postgres DB from my lambda function
I have create a lambda function after S3 createAll event, In this lambda function, I need to update data in my DB. What I have done is I tested the DB connection at local. It works fine. However, after I published to lambda, every console.log inside client.connect function will not be triggered. I thought it would be permisson of my lambda role, So i gave administratorfullacess to this role. Also, in EC2 rule, I make incoming traffic open to all. and outgoing to all as well. 1. EC2 is ubuntu, Postgres as DB 2. Nodejs for Lambda function
const { Client } = require('pg');
exports.handler = async (event,context,callback) => {
context.callbackWaitsForEmptyEventLoop = true;
var client = new Client({
host:'example.com',
port:5432,
user:'postgres',
password:'examplepassword',
database:'db'
});
console.log('start connecting db : log client');
client.connect().then(() => {
console.log('DB is connected');
const text1 = 'SELECT * FROM unime.lecture_content';
const text = 'INSERT INTO uni.institute_type(name) VALUES($1)
RETURNING *';
const values = ['Test Data 2'];
callback('DB Connected')
}).catch(e => {console.error('connection error', e.stack)
callback('DB failure',e.stack)
})
};
My Package.json
{
"name": "node_postgres",
"version": "1.0.0",
"description": "node postgres api",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"deploy": " — zip-file fileb://Lambda-Deployment.zip",
"predeploy": "zip -r Lambda-Deployment.zip * -x *.zip *.log"
},
"keywords": [
"postgres"
],
"author": "JUNXILI",
"license": "ISC",
"dependencies": {
"pg": "^7.0.3"
}
}
I want to show all the log within client.connect function. please help me thanks
