如何从 Node.js 中的 Lambda 函数在 PostgreSQL RDS 上运行 SQL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32295686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run SQL on PostgreSQL RDS from Lambda function in Node.js?
提问by Eran Ben-Natan
I have this code in Lambda funcion:
我在 Lambda 函数中有这个代码:
sql="SELECT ...";
var pg = require('pg');
var connectionString = 'postgres://...';
var client = new pg.Client(connectionString);
client.connect();
var query = client.query(sql);
query.on('end', function() { client.end(); });
When I run from EC2, it works fine. When I run from Lambda I get Error: Cannot find module 'pg'
当我从 EC2 运行时,它工作正常。当我从 Lambda 运行时,出现错误:找不到模块“pg”
回答by imTachu
I am a super noob in Node JS, but I really wanted to try AWS Lambda. Here are the steps I took. I used a Ubuntu 14.04.
我是 Node JS 的超级菜鸟,但我真的很想尝试 AWS Lambda。这是我采取的步骤。我使用了 Ubuntu 14.04。
sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm
sudo ln -s /usr/bin/nodejs /usr/bin/node
mkdir the_function && cd the_function
mkdir node_modules
npm install pg
******Now create a file index.js and paste the content of your funcion.
console.log('Loading S2 Function');
var pg = require("pg");
exports.handler = function(event, context) {
var conn = "pg://user:password@host:5432/bd_name";
var client = new pg.Client(conn);
client.connect();
var query = client.query("SELECT * FROM BLA WHERE ID = 1");
query.on("row", function (row, result) {
result.addRow(row);
});
query.on("end", function (result) {
var jsonString = JSON.stringify(result.rows);
var jsonObj = JSON.parse(jsonString);
console.log(jsonString);
client.end();
context.succeed(jsonObj);
});
};
******Now zip the contents of the_function folder (not the_function folder itself)
You can check the official sample from this AWS link: http://docs.aws.amazon.com/lambda/latest/dg/walkthrough-s3-events-adminuser-create-test-function-create-function.html
您可以从此 AWS 链接查看官方示例:http: //docs.aws.amazon.com/lambda/latest/dg/walkthrough-s3-events-adminuser-create-test-function-create-function.html
回答by Dmytro Rakovskyi
You can import easily only predifined libs to your lambda. For example You can use just boto3 and core for python, for java You can use just core. http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.htmlYou cannot import any additional libs in simple way. You can try to use "hard way". In this case You should save all necessary libraries to s3(or other place which You can access from lambda) and then copy to lambda environment (/tmp) and import it with the help of reflection.
您可以轻松地将预定义的库导入您的 lambda。例如,对于python,您可以只使用boto3 和core,对于java,您可以只使用core。http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html您不能以简单的方式导入任何其他库。您可以尝试使用“硬方法”。在这种情况下,您应该将所有必要的库保存到 s3(或您可以从 lambda 访问的其他地方),然后复制到 lambda 环境 (/tmp) 并在反射的帮助下导入它。
回答by Aniket Thakur
Error: Cannot find module 'pg'
错误:找不到模块“pg”
In my case I was just uploading index.js. We need to package 3rd party node modules as well.
就我而言,我只是上传 index.js。我们还需要打包第 3 方节点模块。
- create index.js (name may vary based on your handler name)
- run
npm install package
- It is better you create
package.json
will all dependencies you need andrun mpn install
- Confirm
node_modules
folder is created in same directory. - Zip these contents (index.js and node_modules folder) and upload the zip.
- You can upload directly or use S3.
- For more details read their offcial doc - Creating a Deployment Package (Node.js)
- 创建 index.js(名称可能因您的处理程序名称而异)
- 跑
npm install package
- 最好创建
package.json
您需要的所有依赖项,并且run mpn install
- 确认
node_modules
文件夹在同一目录中创建。 - 压缩这些内容(index.js 和 node_modules 文件夹)并上传 zip。
- 您可以直接上传或使用S3。
- 有关更多详细信息,请阅读他们的官方文档 -创建部署包 (Node.js)
Now I get: Unable to import module 'index': Error . Is my function must be called index.js
现在我得到: Unable to import module 'index': Error 。是不是我的函数必须叫 index.js
In my case I was zipping the entire directory instead of it's contents. So you need to really do -
在我的情况下,我正在压缩整个目录而不是它的内容。所以你真的需要做——
zip -r deploymentpkg.zip ./*
instead of
代替
zip -r deploymentpkg.zip folder