Search code examples
aws-lambdaangularfire2serverlessangular-universalaws-serverless

AWS Lambda + Serverless, where/how to deploy js module that couldn't be bundled?


I have an Angular 8 + Universal (SSR) + Serverless app. Unfortunately it's still using some firebase functionality. I can't bundle firebase when building my project, I have tu exclude it in webpack config here:

  externals: [
    /^firebase/
  ],

Now how do I provide/add this dependency so that my function works?

In its current state, the app:

  • works using ng serve
  • works as statically hosted
  • works served as SSR with the local server
  • works through serverless-offline plugin

It's only when actually deployed to lambda that I get the error:

{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'firebase/app'",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'firebase/app'",
        "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
        "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
        "    at Object.<anonymous> (/var/runtime/index.js:36:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:776:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)",
        "    at Module.load (internal/modules/cjs/loader.js:653:32)",
        "    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:585:3)",
        "    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)",
        "    at startup (internal/bootstrap/node.js:283:19)"
    ]
}

How could I go about adding this dependency in aws lambda?


Solution

  • I'm not sure why you can't bundle the Firebase module, but you can deploy a Lambda layer with all the required Firebase dependencies and add it to your function.

    You can see an example repo here