Daniele
Daniele

Reputation: 4343

Firebase Cloud Functions - Setting up a service account

I'm facing the following issue when trying to deploy a firebase function with a service account key.

Deployment error. Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded. Did you list all required modules in the package.json dependencies? Detailed stack trace: Error: Cannot find module '/Users/danielec/Desktop/storage_to_firestore/functions/key.json'

This is my code.

// setup firestore
import admin = require('firebase-admin');
var serviceAccount = require("/Users/danielec/Desktop/storage_to_firestore/functions/key.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://testapp.firebaseio.com"
});

I'm sure that the path to the service key is correct, in fact the problem seems to be occurring in the index.js file, basically when it gets compiled from typescript to javascript.

Any idea on how to fix this issue?

I'm living the full log here at the end:

✔  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (37.3 KB) for uploading
✔  functions: functions folder uploaded successfully
i  functions: updating Node.js 8 function image_from_storage_to_firestore(us-central1)...
⚠  functions[image_from_storage_to_firestore(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module '/Users/danielec/Desktop/storage_to_firestore/functions/key.json'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/srv/lib/index.js:9:22)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)

UPDATE:

I'm able to deploy the functions correctly if I change the above mentioned code as follows:

// setup firestore
import admin = require('firebase-admin');
admin.initializeApp({});

When I run it, in the firebase console's logs I get the following error:

Error: Identity and Access Management (IAM) API has not been used in project 291486585482 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/iam.googleapis.com/overview?project=291486585482 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry. at Gaxios.request (/srv/node_modules/gaxios/build/src/gaxios.js:70:23) at at process._tickDomainCallback (internal/process/next_tick.js:229:7)

and if I enable using the link specified in the error message, the IAM API I still get the following error which I haven't been able to solve.

Error: Permission iam.serviceAccounts.signBlob is required to perform this operation on service account [email protected]. at Gaxios.request (/srv/node_modules/gaxios/build/src/gaxios.js:70:23) at at process._tickDomainCallback (internal/process/next_tick.js:229:7)

All I need this function for is to basically create a firestore reference with a downloadURL and a couple more keys, every time a document gets uploaded to firebase storage

Upvotes: 1

Views: 2172

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

You can't reference files on your local machine in code deployed to Cloud Functions like this:

var serviceAccount = require("/Users/danielec/Desktop/storage_to_firestore/functions/key.json");

This isn't going to work at all. You need to deploy the key along with your code in the functions folder, and require it with a relative path instead of an absolute path.

It's even better if you're able to just use the default service account if possible, and give it the necessary permissions to do what it needs to do if necessary.

Upvotes: 3

Related Questions