AWS Lambda 使用 firebase-admin initializeApp 超时 [英] AWS Lambda using firebase-admin initializeApp timeout

查看:17
本文介绍了AWS Lambda 使用 firebase-admin initializeApp 超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use Lambda to Firebase message. I ref this. But the lambda function still timeout because it cannot connect to google server.

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".]

[Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "connect ETIMEDOUT 172.217.26.45:443".]

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

But I use same serviceAccount.json to run on my ec2 and work find. Does someone encounter this?

推荐答案

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

After a couple hours struggling, I finally find the reason. Because my Lambda using VPC to connect RDS and the network interface of VPC only have private IP.

AWS 文档:

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

When you add VPC configuration to a Lambda function, it can only access resources in that VPC. If a Lambda function needs to access both VPC resources and the public Internet, the VPC needs to have a Network Address Translation (NAT) instance inside the VPC.

所以我需要在 VPC 内创建 NAT.我关注这个 博客 问题解决了.

So I need to create NAT inside the VPC. I follow this Blog and problem solved.

这篇关于AWS Lambda 使用 firebase-admin initializeApp 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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