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?
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 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.
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.
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...>
...
};
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With