繁体   English   中英

无法从 Firebase Cloud Functions 连接到 Google SQL PostgreSQL 实例

[英]Cannot connect from Firebase Cloud Functions to Google SQL PostgreSQL instance

更新:在底部添加了 IAM 设置的屏幕截图。

我检查了以下内容但没有成功:

我已经在一个新项目中设置了 Firebase 函数来运行一个快速服务器,该服务器连接到 PostgreSQL 数据库并为 API 提供端点(以允许简单地从数据库中读取记录)

我已授权数据库允许来自我的本地计算机的连接。 它在开发模式下完美运行。 (我运行 firebase serve 在本地模拟云功能。)

现在问题来了 - 我无法连接到生产中的数据库。 我知道为什么:因为 firebase 云功能在技术上没有特定的 IP 地址,我可以在数据库中将其列入白名单并允许来自这些地址的连接。

所以这就是我到目前为止所做的:

  1. 我已将在 Google 中部区域运行的所有 IP 的 IP 范围列入白名单:151.101.0.0/17(云功能在 https://[[remote-cloudfunctions-Nosuccessful]].cloudfunctions.net/app 上运行.

  2. 我尝试通过此文档https://cloud.google.com/sql/docs/postgres/connect-functions从 firebase 内部连接到数据。 这意味着创建一个服务帐户,提供适当的权限,并使用应用程序的内部实例名称作为主机:“optivois-prod:us-east1:[[postgres-instance-connection-name]]”。 这里也没有成功。 这是我的配置:

const Pool = require('pg').Pool
const pool = new Pool({ 
  user: '[[db_user]]',
  host: '/cloudsql/[[postgres-instance-connection-name]]/.s.PGSQL.5432',
  database: '[[db_name]]',
  password: '[[db_password]]'
})
  1. 我尝试为该应用程序创建一个私有 IP ,但这并没有以不同的方式工作。

想知道是否有人对此有想法、想法或问题。

以下是 Google Cloud IMA 成员的屏幕截图: 在此处输入图像描述

这是错误堆栈(我确保实例名称正确并且它可以访问数据库):

3:52:41.541 PM
app
Function execution started 
3:52:42.862 PM
app
CloudSQL warning: your action is needed to update your function and avoid potential disruptions. Please see https://cloud.google.com/functions/docs/sql#troubleshooting for additional details: ensure that the account has access to "[[postgres-instance-connection-name]]" (and make sure there's no typo in that name). Error during createEphemeral for [[postgres-instance-connection-name]]: googleapi: Error 403: The client is not authorized to make this request., notAuthorized 
3:52:43.117 PM
app
Failed to receive message from instance optivois-prod:us-east1:postgres12-dreamfactory: ZERO_APP::1: stream aborted: server handler returned error: ERROR_NOT_AUTHORIZED: end user google account not authorized to access instance: [[postgres-instance-connection-name]] 
3:52:43.130 PM
app
Error getting residents:  Error: connect ECONNREFUSED /cloudsql/[[postgres-instance-connection-name]]/.s.PGSQL.5432 
3:52:43.130 PM
app
    at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) 
3:52:43.130 PM
app
    at PipeConnectWrap.callbackTrampoline (internal/async_hooks.js:120:14) { 
3:52:43.130 PM
app
  errno: 'ECONNREFUSED', 
3:52:43.130 PM
app
  code: 'ECONNREFUSED', 
3:52:43.130 PM
app
  syscall: 'connect', 
3:52:43.130 PM
app
  address: '/cloudsql/[[postgres-instance-connection-name]]/.s.PGSQL.5432' 
3:52:43.130 PM
app
}
3:52:43.135 PM
app
TypeError: Cannot read property 'rows' of undefined
3:52:43.135 PM
app
at /workspace/queries.js:19:39
3:52:43.135 PM
app
at PendingItem.callback (/workspace/node_modules/pg-pool/index.js:352:16)
3:52:43.135 PM
app
at Client._connectionCallback (/workspace/node_modules/pg-pool/index.js:244:23)
3:52:43.135 PM
app
at Client._handleErrorWhileConnecting (/workspace/node_modules/pg/lib/client.js:305:19)
3:52:43.135 PM
app
at Client._handleErrorEvent (/workspace/node_modules/pg/lib/client.js:315:19)
3:52:43.135 PM
app
at Connection.emit (events.js:315:20)
3:52:43.135 PM
app
at Connection.EventEmitter.emit (domain.js:483:12)
3:52:43.135 PM
app
at Socket.reportStreamError (/workspace/node_modules/pg/lib/connection.js:53:12)
3:52:43.135 PM
app
at Socket.emit (events.js:315:20)
3:52:43.135 PM
app
at Socket.EventEmitter.emit (domain.js:483:12)
3:52:43.146 PM
app
Function execution took 1605 ms, finished with status: 'crash'

从图像中,我可以说您的 function 部署在 API 项目上,并且您在评论中提到您的 Cloud SQL 实例位于不同的项目上。

为了解决您的问题,go 到您的云 SQL 实例所在的项目和 select IAM 的项目。 您需要在那里添加您的 Cloud Function 正在使用的服务帐户,并将Cloud SQL Client角色放入其中。

我必须为

<project-id>@appspot.gserviceaccount.com并授予Cloud SQL Client解决此问题。

我还通过使用socketPath: "/cloudsql/<project-id>:<region>:<instance-name>"连接 sql

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM