AWS Lambda通过PG.js连接到RDS Postgres数据库(建立连接,没有超时,但是没有数据库?) [英] AWS Lambda connect via PG.js to RDS Postgres database (connection made, no timeout, but no database?)

查看:160
本文介绍了AWS Lambda通过PG.js连接到RDS Postgres数据库(建立连接,没有超时,但是没有数据库?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的lambda函数正在尝试连接到RDS PostGreSQL DB.由于我使用 https://serverless.com/来部署功能(设置了我的Cloudfront),因此放置LF在与RDS数据库不同的VPC中.

I have my lambda function is trying to connect to an RDS PostGreSQL DB. Since I use https://serverless.com/ to deploy the function (sets up my cloudfront) it puts the LF in a separate VPC from the RDS DB.

不是大问题.如果您阅读: https://docs.aws. amazon.com/lambda/latest/dg/services-rds-tutorial.html 您会看到可以使用子网和安全组ID设置serverless.yml文件(如下所示),然后为具有AWSLambdaVPCAccessExecutionRole的Lambda函数(我为VPC和Lambda提供完整的角色)发挥作用.如果您不这样做,则会得到ECONNREFUESED.

Not a big issue. If you read: https://docs.aws.amazon.com/lambda/latest/dg/services-rds-tutorial.html you see you can setup the serverless.yml file (as below) with the subnet, and security Group IDs, and then give a role to the Lambda Function that has AWSLambdaVPCAccessExecutionRole (I gave it full for the VPC and for Lambda). If you don't do this you will get a ECONNREFUESED.

但是即使执行此操作,我仍然收到3D00错误,该错误表示找不到名为"ysgdb"的数据库.但是在RDS中,我看到它在那里并且是公开的.

But even after doing this I get an 3D00 error, which says that the db with name "ysgdb" is not found. But in RDS I see it is there and is public.

当数据库设置为本地PostGreSQL时,代码可以正常工作.

The code works fine when the DB is set to a local PostGreSQL.

有什么下一步的想法吗?

Any ideas where to go next?

# serverless.yml

service: lambdadb
provider:
  name: aws
  stage: dev
  region: us-east-2
  runtime: nodejs10.x
functions:
  dbConn:
    # this is formatted as <FILENAME>.<HANDLER>
    handler: main.handler
    vpc:
      securityGroupIds:
        - sg-a1e6f4c3
      subnetIds:
        - subnet-53b45038
        - subnet-4a2a7830
        - subnet-1469d358
    events:
    - http:
        path: lambdadb
        method: post
        cors: true
    - http:
        path: lambdadb
        method: get
        cors: true


回复

{
  "statusCode": 200,
  "headers": {
    "Content-Type": "application/json"
  },
  "body": {
    "name": "error",
    "length": 90,
    "severity": "FATAL",
    "code": "3D000",
    "file": "postinit.c",
    "line": "880",
    "routine": "InitPostgres"
  },
  "isBase64Encoded": false
}


server.js

const aws = {
    user: "postgres",
    host: "ysgdb.cxeokcheapqj.us-east-2.rds.amazonaws.com",
    database: "ysgdb",
    password: "***",
    port: 5432
}

console.log(`PostgreSQL GET Function`)

const { Client } = require('pg')

const local = {
    user: "postgres",
    host: "localhost",
    database: "m3_db",
    password: "xxxx",
    port: 5432
}

const aws = {
    user: "postgres",
    host: "ysgdb.cxeokcheapqj.us-east-2.rds.amazonaws.com",
    database: "ysgdb",
    password: "xxxx",
    port: 5432
}

let response = {
    "statusCode": 200,
    "headers": {
        "Content-Type": "application/json"
    },
    "body": 'none',
    "isBase64Encoded": false
}

exports.handler = async (event, context, callback) => {
    const c = aws // switch to local for the local DB
    console.log(`aws credentials: ${JSON.stringify(c)}`)
    const client = new Client({
        user: c.user,
        host: c.host,
        database: c.database,
        password: c.password,
        port: c.port
    })
    try {
        await client.connect();
        console.log(`DB connected`)
    } catch (err) {
        console.error(`DB Connect Failed: ${JSON.stringify(err)}`)
        response.body = err
        callback(null, response)
    }

    client.query('SELECT NOW()', (err, res) => {
        if (err) {
            console.log('Database ' + err)
            response.body = err
            callback(null, response)
        } else {
            response.body = res
            callback(null, response)
        }
        client.end()
    })
}


if (process.env.USERNAME == 'ysg4206') {
    this.handler(null, null, (_, txt) => {console.log(`callback: ${JSON.stringify(txt)}`)})
} 

推荐答案

我找到了答案.尽管这显示了我的愚蠢,但我想将其发布在这里,以防其他人遇到相同的问题.

I found the answer. While it shows my stupidity, I wanted to post it here in case others have the same issue.

当我创建RDS(自定义)PostGreSQL时,我指定了数据库名称.我几乎不知道表单中的大约20个字段,还有第二个您指定dbname的位置.如果不这样做,它会说它不会创建数据库,并且不会分配任何名称(控制台显示-"作为名称.

When I created the RDS (custom) PostGreSQL I specified I database name. Little did I know that about 20 fields down in the form, there is a second location you specify the dbname. If you do not, it says it does not create the db and assigns no name (the console shows "-" as the name.

名字是端点的第一部分.第二次指定名称是针对数据库本身的.

The first name is for the first part of the endpoint. The second time you specify the name is for the database itself.

设置RDS时,我正在使用PostGreSQL进行安全性/登录.

I am using the PostGreSQL for the security/login when I setup the RDS.

希望这对某人有帮助.

这篇关于AWS Lambda通过PG.js连接到RDS Postgres数据库(建立连接,没有超时,但是没有数据库?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆