我已经建立了一个AWS Aurora无服务器PostgreSQL数据库。我还让API Gateway运行到Lambda函数的端点。现在Lambda函数正在连接到DynamoDB,但是RDS在我的用例中会工作得更好。
我在互联网上搜索了几个小时,但似乎找不到一个如何通过Lambda和Node.js访问我的Aurora Serverless DB的例子。我不确定我的函数需要什么导入,而且我也很难在API中找到正确的方法。
仅仅是一个基本的Node.js示例来帮助我入门,将会非常有帮助。
提前谢谢。
发布于 2019-10-14 10:23:55
我在AWS开发者论坛上得到了一个回复,这正是我开始需要的。
显然,要使用PostgreSQL连接器,您必须在本地构建函数并导入,而不是使用在线Lambda控制台。
下面是由MrK提供的示例代码:https://forums.aws.amazon.com/message.jspa?messageID=919394
//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();
};
发布于 2021-06-22 23:55:21
您应该能够使用与连接到PostgreSQL数据库所用的工具/客户端相同的工具/客户端连接到Aurora实例。例如,当您导航到群集的Connectivity and security选项卡时,您将看到一个端点名称-您可以在任何脚本的连接字符串中使用此名称和端口号。
Connecting to an Amazon Aurora PostgreSQL DB cluster Creating a DB cluster and connecting to a database on an Aurora PostgreSQL DB cluster
https://stackoverflow.com/questions/58326192
复制相似问题