I'm trying to use node-postgres (PG) to connect to my localhost PostgreSQL database running on port 5432. To do this, I've setup ngrok to tunnel my request.
./ngrok tcp 5432
The code below works when running locally (even when tunneling using ngrok). It also works on lambda when I connect to an external database - in my case hosted by Heroku.
'use strict';
const PG = require('pg');
// These credentials work locally
var credentials = {
user: 'MyUsername',
host: 'tcp://0.tcp.ngrok.io',
database: 'MyDatabase',
password: 'MyPassword',
port: '12829',
ssl: true
};
const pool = new PG.Pool(credentials);
const connectionPromise = function(){
return new Promise(function (resolve, reject) {
pool.connect(function (err, client, done) {
if(err) console.log(err);
err ? reject(err) : resolve({
client: client,
done: done
})
})
});
};
exports.handler = Handler;
function Handler(event, context, callback) {
context.callbackWaitsForEmptyEventLoop = false;
console.log("Calling handler in Lambda");
return connectionPromise().then(function (conn) {
console.log("Success");
callback(null);
}).catch(function (err) {
console.log("Error");
callback(err);
});
};
// Uncomment this code to run locally.
// Handler(null, {}, function(){
// console.log("Exiting");
// process.exit();
// });
However, when I attempt to use node-postgres + Ngrok to connect to my localhost database via Lambda ...
Error: getaddrinfo ENOTFOUND tcp://0.tcp.ngrok.io tcp://0.tcp.ngrok.io:12829
Full Error Message
START RequestId: 3ac634ef-310e-41ab-b20f-14c86271b5d7 Version: $LATEST 2019-01-21T16:14:27.020Z 3ac634ef-310e-41ab-b20f-14c86271b5d7 Calling handler in Lambda 2019-01-21T16:14:27.117Z 3ac634ef-310e-41ab-b20f-14c86271b5d7 { Error: getaddrinfo ENOTFOUND tcp://0.tcp.ngrok.io tcp://0.tcp.ngrok.io:12829 at errnoException (dns.js:50:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26) code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo',
hostname: 'tcp://0.tcp.ngrok.io', host: 'tcp://0.tcp.ngrok.io',
port: 12829 } 2019-01-21T16:14:27.118Z 3ac634ef-310e-41ab-b20f-14c86271b5d7 Error 2019-01-21T16:14:27.155Z 3ac634ef-310e-41ab-b20f-14c86271b5d7 {"errorMessage":"getaddrinfo ENOTFOUND tcp://0.tcp.ngrok.io tcp://0.tcp.ngrok.io:12829","errorType":"Error","stackTrace":["errnoException (dns.js:50:10)","GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)"]} END RequestId: 3ac634ef-310e-41ab-b20f-14c86271b5d7 REPORT RequestId: 3ac634ef-310e-41ab-b20f-14c86271b5d7 Duration: 136.26 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 23 MB
Is lambda blocking ngrok?