Aar*_*tin 5 node.js aws-lambda aws-aurora-serverless
我已经设置了一个 AWS Aurora Serverless PostgreSQL 数据库。我还有 API Gateway 运行 Lambda 函数的端点。现在,Lambda 函数正在连接到 DynamoDB,但 RDS 将更适合我的用例。
我已经在互联网上搜索了几个小时,但似乎找不到关于如何使用 Node.js 通过 Lambda 访问我的 Aurora Serverless DB 的示例。我不确定我的函数中需要哪些导入,而且我也很难在 API 中找到正确的方法。
只是一个让我入门的基本 Node.js 示例会非常有帮助。
提前致谢。
我在 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();
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4802 次 |
| 最近记录: |