AWS Lambda通过PG.js连接到RDS Postgres数据库(建立连接,没有超时,但是没有数据库?)

时间:2019-11-18 17:01:12

标签: aws-lambda amazon-rds serverless-framework

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

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

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

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

有什么下一步的想法吗?

# 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)}`)})
} 

1 个答案:

答案 0 :(得分:0)

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

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

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

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

希望这对某人有帮助。