AWS Lambda使用firebase-admin initializeApp超时 [英] AWS Lambda using firebase-admin initializeApp timeout
问题描述
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。
将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.
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屋!