Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有Node.js示例说明如何通过Lambda连接到AWS Aurora无服务器PostgreSQL_Node.js_Aws Lambda_Aws Aurora Serverless - Fatal编程技术网

是否有Node.js示例说明如何通过Lambda连接到AWS Aurora无服务器PostgreSQL

是否有Node.js示例说明如何通过Lambda连接到AWS Aurora无服务器PostgreSQL,node.js,aws-lambda,aws-aurora-serverless,Node.js,Aws Lambda,Aws Aurora Serverless,我已经建立了一个AWS Aurora无服务器PostgreSQL数据库。我还让API网关运行Lambda函数的端点。现在Lambda函数正在连接到DynamoDB,但是RDS在我的用例中会工作得更好 我在互联网站上搜索了几个小时,但似乎找不到如何通过Lambda with Node.js访问我的Aurora无服务器数据库的示例。我不确定我的函数需要什么样的导入,我也很难在API中找到正确的方法 只要一个基本的Node.js示例就可以让我开始学习,这将非常有帮助 提前感谢。我在AWS开发者论坛上得

我已经建立了一个AWS Aurora无服务器PostgreSQL数据库。我还让API网关运行Lambda函数的端点。现在Lambda函数正在连接到DynamoDB,但是RDS在我的用例中会工作得更好

我在互联网站上搜索了几个小时,但似乎找不到如何通过Lambda with Node.js访问我的Aurora无服务器数据库的示例。我不确定我的函数需要什么样的导入,我也很难在API中找到正确的方法

只要一个基本的Node.js示例就可以让我开始学习,这将非常有帮助


提前感谢。

我在AWS开发者论坛上得到了回复,这正是我开始工作所需要的

显然,要使用PostgreSQL连接器,您必须在本地构建函数并导入,而不是使用在线Lambda控制台

以下是MrK提供的示例代码:

//这会将postgres连接器导入到文件中,以便可以使用它
const{Client}=require('pg');
//实例化客户端以连接到数据库,然后传入连接设置
const client=新客户端({
用户:“”,
主机:“”,
数据库:“”,
密码:“”,
港口:5432
});
//lambda函数码
exports.handler=async(事件、上下文、回调)=>{
试一试{
等待client.connect();
回调(空,“连接成功”);
//你的代码在这里
}捕捉(错误){
回调(null,“连接成功失败”);
犯错误;
//错误消息
}
client.end();
};

您是否看过pg-promise(),或者您会在AWS SDK中使用
RDSDataService
客户端这不是连接到Aurora Serverless的方式。这适用于连接到普通RDS实例。然而,这不是一个好的解决方案,因为它会在每次调用中创建一个连接,如果有很多用户,这会很慢很快地使数据库耗尽可用的连接。要连接到Aurora Serverless,请使用AWS SDK中RDSDataService的数据API,如Mark B在对问题本身的评论中所述。
//this imports the postgres connector into the file so it can be used
const { Client } = require('pg');

//instantiates a client to connect to the database, connection settings are passed in
const client = new Client({
    user: '<your db username>',
    host: '<your endpoint>',
    database: '<your database name>',
    password: '<your database password>',
    port: 5432
});

//the lambda funtion code
exports.handler = async (event, context, callback) => {

    try {

        await client.connect();
        callback(null, "Connected Successfully");
        //your code here

    } catch (err) {

        callback(null, "Failed to Connect Successfully");
        throw err;
        //error message
    }

    client.end();

};