这里有有关如何通过Lambda连接到AWS Aurora Serverless PostgreSQL的Node.js示例
我已经建立了一个AWS Aurora无服务器PostgreSQL数据库。我还拥有运行Lambda函数端点的API Gateway。目前,Lambda函数正在连接到DynamoDB,但是RDS在我的用例中会更好地工作。
我已经搜寻了几个小时的互连网,但似乎找不到如何使用Lambda和Node.js访问我的Aurora Serverless数据库的示例。我不确定我的函数中需要导入什么,也很难在API中找到正确的方法。
仅仅是一个基本的Node.js示例,可以帮助我入门。
提前感谢。
回答如下:我在AWS开发人员论坛上得到的回复正是我开始所需要的。
显然,要使用PostgreSQL连接器,您必须在本地构建功能并导入而不是使用在线Lambda控制台。
这是MrK提供的示例代码:https://forums.aws.amazon/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();
};
这里有有关如何通过Lambda连接到AWS Aurora Serverless PostgreSQL的Node.js示例
我已经建立了一个AWS Aurora无服务器PostgreSQL数据库。我还拥有运行Lambda函数端点的API Gateway。目前,Lambda函数正在连接到DynamoDB,但是RDS在我的用例中会更好地工作。
我已经搜寻了几个小时的互连网,但似乎找不到如何使用Lambda和Node.js访问我的Aurora Serverless数据库的示例。我不确定我的函数中需要导入什么,也很难在API中找到正确的方法。
仅仅是一个基本的Node.js示例,可以帮助我入门。
提前感谢。
回答如下:我在AWS开发人员论坛上得到的回复正是我开始所需要的。
显然,要使用PostgreSQL连接器,您必须在本地构建功能并导入而不是使用在线Lambda控制台。
这是MrK提供的示例代码:https://forums.aws.amazon/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();
};