Amazon Lambda to Firebase

前端 未结 7 1468
甜味超标
甜味超标 2020-11-30 03:42

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

var Firebase = require(\'firebase\');

Same thing hap

相关标签:
7条回答
  • 2020-11-30 04:12

    This is late, but in case someone else is looking:

    Zipping your project folder instead of the contents of the project folder can cause this. The zipped folder, when extracted, should not contain a folder with the lambda files in it, but should have the index.js file and the node_modules folder at root level.

    A working example of a lambda function is (using latest shiny firebase stuff *sigh*):

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

    Now for the caveat. I have experienced this causing the lambda function to timeout even after the firebase callback has happened, ie. the set function seems to create a listener that holds the lambda function open despite return of correct data.

    Update: Calling firebase.database().goOffline() fixes the Lambda function timeout issue i was experiencing.

    The usual cautions about security not being verified or appropriate, and the possibilities of halting space and time by using this apply.

    0 讨论(0)
提交回复
热议问题