Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon Lambda to Firebase

I get 'Cannot find module 'firebase' when I try to run this in Lambda (Node.js 4.3)

var Firebase = require('firebase');

Same thing happens when I try to upload a zipped package that includes node_modules/firebase

Does anybody have a working 'write from lambda to firebase' implementation?

like image 552
user2676299 Avatar asked May 19 '16 14:05

user2676299


People also ask

Can Firebase work with AWS?

With Firebase consisting of proprietary services, APIs, and an SDK, a migration to AWS requires application refactoring – introducing a new architecture using AWS services, and rewriting parts of the codebase to use them accordingly.

What is AWS equivalent of Firebase?

What is the AWS equivalent of Firebase? AWS Amplify is the AWS equivalent of Firebase. It is a set of features and tools which lets the frontend web and mobile developers to develop applications on AWS. This is done with the ease to increase the bandwidth of AWS services with the evolution of use cases.

Is AWS faster than Firebase?

Firebase differs from AWS in that many of its services are free such as user authentication and the ability to enable push notifications. In building real-time applications, Firebase is faster and cheaper than AWS -- it immediately updates in real-time without much oversight on your part.


2 Answers

To safely use firebase npm package (version 3.3.0) in AWS Lambda (Nodejs 4.3), Please do the following:

'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...>
    ...
};
like image 64
Josiah Choi Avatar answered Dec 14 '22 12:12

Josiah Choi


I solved my problem by using 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");

}
like image 40
user2676299 Avatar answered Dec 14 '22 13:12

user2676299