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

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

问题描述

我使用Lambda到Firebase消息。我参考这个。但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;
$ b const payload = {
data:{
score:850,
time:2:45
}
} ;

// [START初始化]
if(firebase.apps.length == 0){//< --- Important !!!在lambda中,它会导致双重初始化。
firebase.initializeApp({
credential:firebase.credential.cert(serviceAccount),
databaseURL:'https://messaging-xxxxx.firebaseio.com'
});
}

//发送一条消息到对应于提供的
//注册令牌的设备。
firebase.messaging()。sendToDevice(registrationToken,payload)
.then(function(response){
//参见
的MessagingDevicesResponse参考文档//响应的内容
console.log(成功发送消息:,响应);
回调(null,{
statusCode:200,
body:JSON.stringify(Successful! ),
));
})
.catch(function(error){
console.log(Error sending message:error,error);
callback (null,{
statusCode:500,
body:JSON.stringify({
status:error,
message:error
))
})
});
};

CloudWatch


<错误:通过凭据属性提供给initializeApp()的凭据实现无法获取有效的Google OAuth2访问令牌,并显示以下错误:connect ETIMEDOUT 172.217.26.45:443。]

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

解决方案

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

AWS文档


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


所以我需要在VPC内创建NAT。
我遵循这个博客,问题解决了。

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
        })
      })
    });
};

CloudWatch

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

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

解决方案

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 document:

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.

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

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

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