0
votes

I am trying to use FireStore-admin in NodeJS in AWS Lambda using layers. For this, I have zipped the node_modules and serviceAccount.json file in the following hierarchy and uploaded it to the layer:

nodejs->

  • node_modules
  • package.json
  • package-lock.json
  • serviceAccount.json (contains credentials to connect to FireStore collection)

The code is as follows:

var admin = require('firebase-admin');
var serviceAccount = require('./serviceAccount.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

But I receive the following error when I run this lambda function:

"errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module './serviceAccount.json'

You can also find the image of the zip attached below:

Hierarchy

How do I access the json file and/or correct the hierarchy to get this working?

1
serviceAccount.json is in . relative to the layer file, but the layer could be extracted to a different path in the Lambda runtime. I have 0 knowledge on how JS require works, just throwing ideas around. Maybe have a look at github.com/awsdocs/aws-lambda-developer-guide/tree/master/… - ivica

1 Answers

-1
votes

I think this should work so long as your file where this code lies is in the same directory as serviceAccount.json

import * as serviceAccount from './serviceAccount.json'
var admin = require('firebase-admin');    
    
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});