简体   繁体   English

AWS Lambda函数用于连接Postgresql数据库

[英]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 - 在这里我做了什么 -

  1. mkdir deployment mkdir部署
  2. cd deployment cd部署
  3. vi index.js vi index.js
  4. write your lambda code in this file. 在这个文件中写下你的lambda代码。 Make sure your handler name is index.handler when you create it. 创建时,请确保您的处理程序名称为index.handler。
  5. npm install pg npm install pg
  6. You should see node_modules directory created in deployment directory which has multiple modules in it 您应该看到在部署目录中创建的node_modules目录,其中包含多个模块
  7. Package the deployment directory into a zip file and upload to Lambda. 将部署目录打包为zip文件并上传到Lambda。
  8. You should be good then 你应该好的

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.

相关问题 使用 AWS Lambda function 连接到 kubernetes - Connect to kubernetes with AWS Lambda function AWS Lambda function:在查询 ZE4728F444B24839E3F80ADF3829BCBA9 时返回 null - AWS Lambda function: returns null on querying postgresql AWS Lambda NodeJS连接到RDS Postgres数据库 - AWS Lambda NodeJS Connect to RDS Postgres Database AWS Lambda上的节点功能无法使用node-pg连接到数据库 - Node function on AWS Lambda can't connect to database with node-pg 使用 Z3B2819DD4C24EDA2FAF2052EEF449555Z 从 AWS Lambda function 连接到 MySql 数据库 - Connecting to MySql database from AWS Lambda function using Node.js, no connect callback 从Lambda函数(节点)连接到MySQL数据库 - Connect to MySQL database from Lambda function (Node) 如何通过AWS RDS在没有“数据库”名称的情况下连接到PostgreSQL DB - How to connect to PostgreSQL DB without a “database” name from AWS RDS 我应该如何从AWS Lambda函数连接到Redis实例? - How should I connect to a Redis instance from an AWS Lambda function? 如何在 AWS Lambda function 中连接 node-imap 模块 - How to connect node-imap module inside AWS Lambda function PreSignUP 在 AWS Lambda function 中出现错误连接 ECONNREFUSED 127.0.0.1:443 失败 - PreSignUP failed with error connect ECONNREFUSED 127.0.0.1:443 in AWS Lambda function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM