使用 Ngrok 从 AWS Lamba 连接到本地 PostgreSQL

Wil*_*ett 5 lambda node-postgres ngrok

我正在尝试使用node-postgres (PG)连接到在端口5432上运行的 localhost PostgreSQL 数据库。为此,我设置了ngrok来隧道传输我的请求。

./ngrok tcp 5432
Run Code Online (Sandbox Code Playgroud)

下面的代码在本地运行时有效(即使在使用 ngrok 进行隧道传输时)。当我连接到外部数据库时,它也适用于 lambda - 在我的情况下由 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();
// });
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用 node-postgres + Ngrok 通过 Lambda 连接到我的本地主机数据库时......

错误:getaddrinfo ENOTFOUND tcp://0.tcp.ngrok.io tcp://0.tcp.ngrok.io:12829

完整的错误信息

START RequestId: 3ac634ef-310e-41ab-b20f-14c86271b5d7 版本: $LATEST 2019-01-21T16:14:27.020Z 3ac634ef-310e-41ab-b20f-114c8107216T 2019-01-21T16:14:27.020200f-114c8162162016014c862700000f-114c816216160000000000 -310e-41ab-b20f-14c86271b5d7 { 错误:getaddrinfo ENOTFOUND tcp://0.tcp.ngrok.io tcp://0.tcp.ngrok.io:12829 at errnoException (dns.js:50:10) at GetrapAddrInfoReqW .onlookup [as oncomplete] (dns.js:92:26) 代码:'ENOTFOUND',errno:'ENOTFOUND',系统调用:'getaddrinfo',
主机名:'tcp://0.tcp.ngrok.io',主机: 'tcp://0.tcp.ngrok.io',
端口:12829 } 2019-01-21T16:14:27.118Z 3ac634ef-310e-41ab-b20f-14c86271b5d7 错误 2019-01-21T16:14:27.135Z:27.135Z:27.135Z:27.135Z"errorb40c1616c100000" 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-461b161buration: 3ac634ef-310e-461b71buration ms 计费持续时间:200 ms 内存大小:128 MB 已用最大内存:23 MB

lambda 会阻塞 ngrok 吗?

小智 5

tcp://从 ngrok 主机名中删除:

var credentials = {
    user: 'MyUsername',
    host: '0.tcp.ngrok.io',
    database: 'MyDatabase',
    password: 'MyPassword',
    port: '12829',
    ssl: true
};
Run Code Online (Sandbox Code Playgroud)