Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase AdminSDK in AWS Lambda (module initialisation error: Error) #247

Closed
kumarkeviv opened this issue Apr 3, 2018 · 19 comments
Closed

Comments

@kumarkeviv
Copy link

Describe your environment

  • Operating System version: AWS Lambda
  • Firebase SDK version: 5.11.0
  • Firebase Product: Firestore

Describe the problem

I have been trying to connect my lambda function with Firestore DB using Admin SDK but its throwing error "module initialisation error: Error"

Its clearly an issue with Firesbase admin sdk, as every-time I comment this component, the rest of the component works fine. Could you please let me know what is the issue?

Steps to reproduce:

What happened? How can we make the problem occur?
This could be a description, log/console output, etc.

Relevant Code:

'use strict';
const Alexa = require('alexa-sdk');
const firebase = require('firebase-admin');
const APP_ID = '---';

const SKILL_NAME = '----';
const HELP_MESSAGE = '---';
const HELP_REPROMPT = '----';
const STOP_MESSAGE = '---';

module.exports.Test = (event, context, callback) => {

    context.callbackWaitsForEmptyEventLoop = false;

    if (firebase.apps.length == 0) {
        firebase.initializeApp({
            credential: firebase.credential.cert('---'),
            databaseURL: "---"
        });
    }

    var db = firebase.firestore();
    var docRef = db.collection('users')
        .doc('alovelace');

    var setAda = docRef.set({
        first: 'Ada',
        last: 'Lovelace',
        born: 1815
    });
};

const handlers = {
    'LaunchRequest': function () {
        this.emit('BasicFunction');
    },
    //Help
    'AMAZON.HelpIntent': function () {
        const speechOutput = HELP_MESSAGE;
        const reprompt = HELP_REPROMPT;

        this.response.speak(speechOutput)
            .listen(reprompt);
        this.emit(':responseReady');
    },
    //Cancel
    'AMAZON.CancelIntent': function () {
        this.response.speak(STOP_MESSAGE);
        this.emit(':responseReady');
    },
    //Stop
    'AMAZON.StopIntent': function () {
        this.response.speak(STOP_MESSAGE);
        this.emit(':responseReady');
    },
};

exports.handler = function (event, context, callback) {
    const alexa = Alexa.handler(event, context, callback);
    alexa.APP_ID = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

####Logs from CloudWatch

module initialization error: Error
at FirebaseAppError.Error (native)
at FirebaseAppError.FirebaseError [as constructor] (/var/task/node_modules/firebase-admin/lib/utils/error.js:39:28)
at FirebaseAppError.PrefixedFirebaseError [as constructor] (/var/task/node_modules/firebase-admin/lib/utils/error.js:85:28)
at new FirebaseAppError (/var/task/node_modules/firebase-admin/lib/utils/error.js:119:28)
at Function.Certificate.fromPath (/var/task/node_modules/firebase-admin/lib/auth/credential.js:142:19)
at new CertCredential (/var/task/node_modules/firebase-admin/lib/auth/credential.js:192:25)
at Object.cert (/var/task/node_modules/firebase-admin/lib/firebase-namespace.js:224:58)
at Object.<anonymous> (/var/task/index.js:14:41)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
@google-oss-bot
Copy link

Hey there! I couldn't figure out what this issue is about, so I've labeled it for a human to triage. Hang tight.

@google-oss-bot
Copy link

Hmmm this issue does not seem to follow the issue template. Make sure you provide all the required information.

@hiranya911
Copy link
Contributor

Are you using Docker to deploy your function?

@kumarkeviv
Copy link
Author

No, I am installing the dependency in local folder along with my code. Later uploading the zipped folder to lambda function via console.

@hiranya911
Copy link
Contributor

You have to use Docker to use Firestore clients in AWS Lambda: https://medium.com/@samstern_58566/how-to-use-cloud-firestore-on-aws-lambda-4bf6d3a473d9

@kumarkeviv
Copy link
Author

kumarkeviv commented Apr 6, 2018

Hi @hiranya911

Thanks for the quick response, I am trying to use the suggested solution but it doesn't seems to be working.

Every-time, I follow the steps listed in blog, its resulting in empty node_modules folder, even though the code executes properly.

Also I tried to directly import the package as you mentioned here in #104
"I've published an RC at https://storage.googleapis.com/firebase-admin-node/firebase-admin-5.4.3-rc0.tgz"

But still it throws "Unable to import module 'index': Error" in cloud watch.

@kumarkeviv
Copy link
Author

@hiranya911 adding more asset and error log.

Unable to import module 'index': Error at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at Object.<anonymous> (/var/task/index.js:4:22) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3)

//Missing firebase_admin folder after executing the code.
missing_firebase_admin

Not sure, how to resolve this issue :(

@hiranya911
Copy link
Contributor

What's in your package.json?

@kumarkeviv
Copy link
Author

kumarkeviv commented Apr 9, 2018

I am trying to use Firebase admin for my Alexa app.
So, my final package.json is like:

{
  "name": "firestore-on-lambda",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^5.4.2"
    "alexa-sdk": "^1.0.25"
  }
}

But I also, tried below :

{
  "name": "firestore-on-lambda",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^5.4.2"
  }
}

Same result, no firebase_admin folder is being generated.

@kumarkeviv
Copy link
Author

Any help?

@hiranya911
Copy link
Contributor

@samtstern who may have some ideas.

@samtstern
Copy link
Contributor

samtstern commented Apr 12, 2018

@kumarkeviv check out this blog post I wrote about using Cloud Firestore on AWS Lambda, which will tell you how to get firebase-admin set up.

https://medium.com/@samstern_58566/how-to-use-cloud-firestore-on-aws-lambda-4bf6d3a473d9

@hiranya911
Copy link
Contributor

@samtstern I believe he's following that article, but running into this weird issue (@kumarkeviv can you confirm?)

@kumarkeviv
Copy link
Author

Yes, I am already following that blog article, but its not generating any firebase-admin folder. :(

@kumarkeviv
Copy link
Author

@samtstern I have also attached the snapshot for list of generated folders (missing firebase-admin) in previous comment.

I am using OS: windows 10 Enterprise and Docker Toolbox for installing the dependency.

@kumarkeviv
Copy link
Author

Can anyone help me on this? Its over a month.

@kushwaha03
Copy link

firebase-admin file is generate via node modules so write cmd on CLI
npm install firebase-admin
firstly go to ur directory folder then run

@albertvolkman
Copy link

Docker worked, but this works as well-
nvm exec 8.10 npm install

@tahmina8765
Copy link

Docker works only when I use javascript, but if I use serverless framework with nodejs-typescript, then docker does not work for me. But the following works -

https://medium.com/mtlserverless/firestore-the-serverless-framework-the-easy-way-262e574581d0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants