【问题标题】:AWS Lambda using firebase-admin initializeApp timeoutAWS Lambda 使用 firebase-admin initializeApp 超时
【发布时间】:2017-02-18 07:36:54
【问题描述】:

我使用 Lambda 到 Firebase 消息。我参考this。但是 lambda 函数仍然超时,因为它无法连接到谷歌服务器。

Handler.js

/ [START imports]
const firebase = require('firebase-admin');
const serviceAccount = require("../serviceAccount.json");

module.exports.message = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;  
  const registrationToken = "xxxxxxx";

  const payload = {
    data: {
      score: "850",
      time: "2:45"
    }
  };

  // [START initialize]
  if(firebase.apps.length == 0) {   // <---Important!!! In lambda, it will cause double initialization.
    firebase.initializeApp({
      credential: firebase.credential.cert(serviceAccount),
      databaseURL: 'https://messaging-xxxxx.firebaseio.com'
    });
  }

  // Send a message to the device corresponding to the provided
  // registration token.
  firebase.messaging().sendToDevice(registrationToken, payload)
    .then(function(response) {
      // See the MessagingDevicesResponse reference documentation for
      // the contents of response.
      console.log("Successfully sent message:", response);
      callback(null, {
        statusCode: 200,
        body: JSON.stringify("Successful!"),
      });
    })
    .catch(function(error) {
      console.log("Error sending message:", error);
      callback(null, {
        statusCode: 500,
        body: JSON.stringify({
          "status": "error",
          "message": error
        })
      })
    });
};

云观察

[错误:通过“credential”属性提供给 initializeApp() 的凭据实现未能获取有效的 Google OAuth2 访问令牌,并出现以下错误:“connect ETIMEDOUT 172.217.26.45:443”。]

但我使用相同的 serviceAccount.json 在我的 ec2 上运行并找到工作。 有人遇到过吗?

【问题讨论】:

  • 您是如何添加您的serviceAccount.json 文件的?我假设你上传了一个 zip 到 Lambda,它不仅仅是内联代码?
  • 这个帖子有用吗? stackoverflow.com/questions/36508974/…
  • @Deif 我使用无服务器上传我的 serviceAccount.json 文件。
  • @jacobawenger 谢谢你的链接。你给了我正确的方向!

标签: amazon-web-services firebase aws-lambda firebase-cloud-messaging firebase-admin


【解决方案1】:

经过几个小时的挣扎,我终于找到了原因。 因为我的 Lambda 使用 VPC 连接 RDS,而 VPC 的网络接口只有私有 IP。

AWS document:

当您将 VPC 配置添加到 Lambda 函数时,它只能访问该 VPC 中的资源。如果 Lambda 函数需要访问 VPC 资源和公共 Internet,则 VPC 需要在 VPC 内部具有网络地址转换 (NAT) 实例。

所以我需要在 VPC 内创建 NAT。 我关注这个Blog 并解决了问题。

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 2017-07-14
    • 2021-05-23
    • 1970-01-01
    • 2021-07-24
    • 2021-12-11
    • 2019-04-03
    • 2021-06-08
    相关资源
    最近更新 更多