AWS Lambda 通过 PG.js 连接到 RDS Postgres 数据库(已建立连接,没有超时,但没有数据库?)
AWS Lambda connect via PG.js to RDS Postgres database (connection made, no timeout, but no database?)
我的 lambda 函数正在尝试连接到 RDS PostGreSQL 数据库。因为我使用 https://serverless.com/ 来部署函数(设置我的云端),所以它将 LF 放在与 RDS 数据库分开的 VPC 中。
问题不大。如果您阅读:https://docs.aws.amazon.com/lambda/latest/dg/services-rds-tutorial.html
您会看到,您可以使用子网和安全组 ID 设置 serverless.yml 文件(如下所示),然后为具有 AWSLambdaVPCAccessExecutionRole 的 Lambda 函数赋予一个角色(我为 VPC 和 Lambda 赋予了它完整的角色)。如果您不这样做,您将获得 ECONNREFUESED。
但即使在执行此操作后,我仍收到 3D00 错误,它表示未找到名称为 "ysgdb" 的数据库。但在 RDS 中,我看到它在那里并且是 public.
当数据库设置为本地 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)}`)})
}
我找到了答案。虽然它显示了我的愚蠢,但我想在这里 post 以防其他人遇到同样的问题。
当我创建 RDS(自定义)PostGreSQL 时,我指定了数据库名称。我几乎不知道在表单中大约 20 个字段下,还有一个指定 dbname 的位置。如果你不这样做,它说它不会创建数据库并且没有分配名称(控制台显示“-”作为名称。
名字是端点的第一部分。第二次指定名称是针对数据库本身。
我在设置 RDS 时将 PostGreSQL 用于 security/login。
希望这对某人有所帮助。
我的 lambda 函数正在尝试连接到 RDS PostGreSQL 数据库。因为我使用 https://serverless.com/ 来部署函数(设置我的云端),所以它将 LF 放在与 RDS 数据库分开的 VPC 中。
问题不大。如果您阅读:https://docs.aws.amazon.com/lambda/latest/dg/services-rds-tutorial.html 您会看到,您可以使用子网和安全组 ID 设置 serverless.yml 文件(如下所示),然后为具有 AWSLambdaVPCAccessExecutionRole 的 Lambda 函数赋予一个角色(我为 VPC 和 Lambda 赋予了它完整的角色)。如果您不这样做,您将获得 ECONNREFUESED。
但即使在执行此操作后,我仍收到 3D00 错误,它表示未找到名称为 "ysgdb" 的数据库。但在 RDS 中,我看到它在那里并且是 public.
当数据库设置为本地 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)}`)})
}
我找到了答案。虽然它显示了我的愚蠢,但我想在这里 post 以防其他人遇到同样的问题。
当我创建 RDS(自定义)PostGreSQL 时,我指定了数据库名称。我几乎不知道在表单中大约 20 个字段下,还有一个指定 dbname 的位置。如果你不这样做,它说它不会创建数据库并且没有分配名称(控制台显示“-”作为名称。
名字是端点的第一部分。第二次指定名称是针对数据库本身。
我在设置 RDS 时将 PostGreSQL 用于 security/login。
希望这对某人有所帮助。