[英]AWS Lambda function to connect to a Postgresql database
Does anyone know how I can connect to a PostgreSQL database through an AWS Lambda function. 有谁知道如何通过AWS Lambda函数连接到PostgreSQL数据库。 I searched it up online but I couldn't find anything about it.
我在网上查了一下,但我找不到任何关于它的信息。 If you could tell me how to go about it that would be great.
如果你能告诉我如何去做那件事会很棒。
If you can find something wrong with my code (node.js) that would be great otherwise can you tell me how to go about it? 如果你发现我的代码(node.js)出了问题会很好,否则你能告诉我怎么去吗?
exports.handler = (event, context, callback) => {
"use strict"
const pg = require('pg');
const connectionStr =
"postgres://username:password@host:port/db_name";
var client = new pg.Client(connectionStr);
client.connect(function(err){
if(err) {
callback(err)
}
callback(null, 'Connection established');
});
context.callbackWaitsForEmptyEventLoop = false;
};
The code throws an error: cannot find module 'pg' 代码抛出一个错误:找不到模块'pg'
I wrote it directly on AWS Lambda and didn't upload anything if that makes a difference. 我直接在AWS Lambda上写了它,如果有所不同,就不会上传任何内容。
I wrote it directly on AWS Lambda and didn't upload anything if that makes a difference.
我直接在AWS Lambda上写了它,如果有所不同,就不会上传任何内容。
Yes this makes the difference! 是的,这有所不同! Lambda doesnt provide 3rd party libraries out of the box.
Lambda并不提供开箱即用的第三方库。 As soon as you have a dependency on a 3rd party library you need to zip and upload your Lambda code manually or with the use of the API.
只要您依赖第三方库,就需要手动压缩或上传Lambda代码或使用API。
Fore more informations: Lambda Execution Environment and Available Libraries 更多信息: Lambda执行环境和可用库
You need to refer Creating a Deployment Package (Node.js) 您需要参考创建部署包(Node.js)
Simple scenario – If your custom code requires only the AWS SDK library, then you can use the inline editor in the AWS Lambda console.
简单方案 - 如果您的自定义代码仅需要AWS SDK库,那么您可以使用AWS Lambda控制台中的内联编辑器。 Using the console, you can edit and upload your code to AWS Lambda.
使用控制台,您可以编辑代码并将其上载到AWS Lambda。 The console will zip up your code with the relevant configuration information into a deployment package that the Lambda service can run.
控制台将使用相关配置信息将代码压缩到Lambda服务可以运行的部署包中。
and 和
Advanced scenario – If you are writing code that uses other resources, such as a graphics library for image processing, or you want to use the AWS CLI instead of the console, you need to first create the Lambda function deployment package, and then use the console or the CLI to upload the package.
高级方案 - 如果您正在编写使用其他资源的代码,例如用于图像处理的图形库,或者您希望使用AWS CLI而不是控制台,则需要首先创建Lambda函数部署包,然后使用控制台或CLI上传包。
Your case like mine falls under Advanced scenario. 像我这样的情况属于高级方案。 So we need to create a deployment package and then upload it.
所以我们需要创建一个部署包然后上传它。 Here what I did -
在这里我做了什么 -
NOTE : npm install will install node modules in same directory under node_modules directory unless it sees a node_module directory in parent directory. 注意:npm install会将节点模块安装在node_modules目录下的同一目录中,除非它在父目录中看到node_module目录。 To be same first do
npm init
followed by npm install
to ensure modules are installed in same directory for deployment. 首先执行
npm init
然后执行npm install
以确保模块安装在同一目录中以进行部署。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.