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-Admin does not deploy properly to Lambda #876

Closed
Ali762 opened this issue Jan 16, 2021 · 2 comments
Closed

Firebase-Admin does not deploy properly to Lambda #876

Ali762 opened this issue Jan 16, 2021 · 2 comments

Comments

@Ali762
Copy link

Ali762 commented Jan 16, 2021

Describe the bug

One of my /api routes uses the firebase-admin library to make updates to my database. This works fine in dev and when served locally, but gives a 503 when deployed to AWS.

Actual behavior

POST to /api/submit returns a cloudfront 503 error

Expected behavior

POST should behave as expected, as it does locally after next build/next serve.

Screenshots/Code/Logs

/lib/firebaseAdmin.js

import * as admin from "firebase-admin";
import creds from "../firebase_secret_credentials.json";

export function db() {
  if (!admin.apps.length) {
    admin.initializeApp({
      credential: admin.credential.cert(creds),
      databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
    });
  }
  return admin.firestore();
}

*On an /api route:

import { db } from "lib/firebaseAdmin"; 
const ref= await db()
      .collection("ratings")
      .where("uid", "==", rating.uid)
      .limit(1)
      .get();

Cloudfront errors with:
ERROR (node:8) Warning: protos.json not found in any of the include paths /protos,/protos
2021-01-16T11:27:49.440Z 3d3da11d-716d-443b-a38a-7f92b5e4fe91 ERROR Error: ENOENT: no such file or directory, open 'protos.json' at Object.openSync (fs.js:462:3) at Object.readFileSync (fs.js:364:35) at fetch (/var/task/pages/api/submitrating.js:21121:34) at Root.load (/var/task/pages/api/submitrating.js:21155:13) at Root.loadSync (/var/task/pages/api/submitrating.js:21196:17) at Object.loadSync (/var/task/pages/api/submitrating.js:23114:29) at GrpcClient.loadFromProto (/var/task/pages/api/submitrating.js:119731:44) at GrpcClient.loadProto (/var/task/pages/api/submitrating.js:119763:21) at new FirestoreClient (/var/task/pages/api/submitrating.js:114235:38) at ClientPool.clientFactory (/var/task/pages/api/submitrating.js:123135:26)Caused by: Error at Query._get (/var/task/pages/api/submitrating.js:183355:23) at Query.get (/var/task/pages/api/submitrating.js:183344:21) at /var/task/pages/api/submitrating.js:88316:138 at processTicksAndRejections (internal/process/task_queues.js:97:5) at async apiResolver (/var/task/pages/api/submitrating.js:86749:1) at async Module.module.exports.tXxc.__webpack_exports__.default (/var/task/pages/api/submitrating.js:188327:11) { errno: -2, syscall: 'open', code: 'ENOENT', path: 'protos.json'}

Versions


myNextApplication:
  component: "@sls-next/[email protected]" # it is recommended you pin the latest stable version of serverless-next.js
  inputs:
    runtime:
      defaultLambda: "nodejs12.x"
      apiLambda: "nodejs12.x"
      imageLambda: "nodejs12.x" # Note that the sharp image library is built for NodeJS 12.x
    memory:
      defaultLambda: 1024
      apiLambda: 2048
      imageLambda: 2048
    build: true
    publicDirectoryCache:
      value: public,  max-age=0, s-maxage=2678400, must-revalidate
      test: /\.(gif|jpe?g|png|txt|xml|woff2|css|webp)$/i
    domain: "<my.domain.here>"
    cloudfront:
      defaults:
        forward:
          headers:
            [
              CloudFront-Is-Desktop-Viewer,
              CloudFront-Is-Mobile-Viewer,
              CloudFront-Is-Tablet-Viewer,
            ]

@ghost
Copy link

ghost commented Feb 11, 2021

I'm facing exactly the same problem (except deploying to Vercel) — an api route using firebase-admin works fine in dev but when deployed to Vercel gives that same error Error: ENOENT: no such file or directory, open 'protos.json'. Haven't been able to find a solution yet..

UPDATE: Fixed by setting target: "experimental-serverless-trace" in next.config.js (see this issue/comment)

@dphang
Copy link
Collaborator

dphang commented Aug 1, 2021

Closing old issue, since there is the workaround. But I'm not sure if it still works in latest Next.js versions as they reworked the chunking logic (and no more serverless trace, I think). If there is still an issue, let us know and we may be able to add support for it (perhaps it may need copying some files into the lambda, etc.)

@dphang dphang closed this as completed Aug 1, 2021
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

2 participants