亚马逊Lambda到Firebase

use*_*299 16 lambda amazon firebase

当我尝试在Lambda中运行它时,我得到'找不到模块'firebase'(Node.js 4.3)

var Firebase = require('firebase');
Run Code Online (Sandbox Code Playgroud)

当我尝试上传包含node_modules/firebase的压缩包时,会发生同样的事情

有没有人有一个"从lambda写到firebase"的工作?

Jos*_*hoi 23

要在AWS Lambda(Nodejs 4.3)中安全使用firebase npm软件包(版本3.3.0),请执行以下操作:

'use strict';

var firebase = require("firebase");

exports.handler = (event, context, callback) => {
    context.callbackWaitsForEmptyEventLoop = false;  //<---Important

    var config = {
        apiKey: "<<apikey>>",
        authDomain: "<<app_id>>.firebaseapp.com",
        databaseURL: "https://<<app_id>>.firebaseio.com",
        storageBucket: "<<app_id>>.appspot.com",
    };

    if(firebase.apps.length == 0) {   // <---Important!!! In lambda, it will cause double initialization.
        firebase.initializeApp(config);
    }

    ...
    <Your Logic here...>
    ...
};
Run Code Online (Sandbox Code Playgroud)


use*_*299 9

我使用firebase REST api解决了我的问题

var https = require('https');

exports.handler = function(event, context, callback) {

    var body = JSON.stringify({
        foo: "bar"
    })

   var https = require('https');

var options = {
  host: 'project-XXXXX.firebaseio.com',
  port: 443,
  path: '/.json',
  method: 'POST'
};

var req = https.request(options, function(res) {
  console.log(res.statusCode);
  res.on('data', function(d) {
    process.stdout.write(d);
  });
});
req.end(body);

req.on('error', function(e) {
  console.error(e);
});

    callback(null, "some success message");

}
Run Code Online (Sandbox Code Playgroud)


ric*_*ter 9

这已经很晚了,但万一有人正在寻找:

压缩项目文件夹而不是项目文件夹的内容可能会导致此问题.压缩文件夹在解压缩时不应包含其中包含lambda文件的文件夹,但应包含index.js文件和根级别的node_modules文件夹.

lambda函数的一个工作示例是(使用最新闪亮的firebase内容*叹气*):

var firebase = require('firebase');

// Your service account details
var credentials = {
  "type": "service_account",
  "project_id": "project-123451234512345123",
  "private_key_id": "my1private2key3id",
  "private_key": "-----BEGIN PRIVATE KEY-----InsertKeyHere-----END PRIVATE KEY-----\n",
  "client_email": "projectname@project-123451234512345123.iam.gserviceaccount.com",
  "client_id": "1111222223333344444",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/projectname%40project-123451234512345123.iam.gserviceaccount.com"
};

firebase.initializeApp({
  serviceAccount: credentials,
  databaseURL: "https://project-123451234512345123.firebaseio.com"
});

exports.handler = function (event, context, callback) {

  // I use some data passed in from AWS API Gateway:
  if (!event.firebaseUid) {
    callback('Missing param for id');
  }

  firebase.database().ref().child('users').child(firebaseUid).child('at').set(newTokens.access_token).then(function (data) {
    console.log('Firebase data: ', data);
    firebase.database().goOffline();
    callback(null, 'Firebase data: ', data);
  }).catch(function (error) {
    callback('Database set error ' + error);
  });
 };
Run Code Online (Sandbox Code Playgroud)

现在请注意.我经历过这种情况,即使在firebase回调发生后,lambda函数也会超时,即.尽管返回了正确的数据,set函数似乎创建了一个保持lambda函数打开的监听器.

更新:调用firebase.database().goOffline()修复了我遇到的Lambda函数超时问题.

关于安全性未得到验证或适当的常见警告,以及通过使用此方法来停止空间和时间的可能性.

  • @josiah-choi 下面的回答为我解决了这个问题 - 请参阅`context.callbackWaitsForEmptyEventLoop = false` (2认同)