title: Firebase Cloud Functions - Setting up a service account
body: <p>I'm facing the following issue when trying to deploy a firebase function with a service account key.</p>
  
  <blockquote>
    <p>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'</p>
  </blockquote>
  
  <p>This is my code. </p>
  
  <pre><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"
  });
  </code></pre>
  
  <p>I'm sure that the path to the service key is correct, in fact the problem seems to be occurring in the <code>index.js</code> file, basically when it gets compiled from typescript to javascript. </p>
  
  <p>Any idea on how to fix this issue?</p>
  
  <p>I'm living the full log here at the end:</p>
  
  <pre><code>✔  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.&lt;anonymous&gt; (/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)
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I'm able to deploy the functions correctly if I change the above mentioned code as follows:</p>
  
  <pre><code>// setup firestore
  import admin = require('firebase-admin');
  admin.initializeApp({});
  </code></pre>
  
  <p>When I run it, in the firebase console's logs I get the following error:</p>
  
  <blockquote>
    <p>Error: Identity and Access Management (IAM) API has not been used in
    project 291486585482 before or it is disabled. Enable it by visiting
    <a href="https://console.developers.google.com/apis/api/iam.googleapis.com/overview?project=291486585482" rel="nofollow noreferrer">https://console.developers.google.com/apis/api/iam.googleapis.com/overview?project=291486585482</a>
    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)</p>
  </blockquote>
  
  <p>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.</p>
  
  <blockquote>
    <p>Error: Permission iam.serviceAccounts.signBlob is required to perform
    this operation on service account
    testestest@appspot.gserviceaccount.com.
        at Gaxios.request (/srv/node_modules/gaxios/build/src/gaxios.js:70:23)
        at 
        at process._tickDomainCallback (internal/process/next_tick.js:229:7)</p>
  </blockquote>
  
  <p>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</p>