Getting " {"errorType":"Error","errorMessage":"ENOENT: no such file or directory, open" error on linking local file path to `GoogleAuth` class 'keyFile' property of googleapis

0

I am trying to invoke googleapis through AWS lambda. I am using a google service account to generate server-server authentication. we have stored the service account details in a JSON file name 'config.json'. Inorder to invoke API for google, we need to create auth object using the 'GoogleAuth' class and pass the config file path as a value to the 'keyfile' property. Though we have provided the correct path location of the file, lambda could not recognize the path and throw error.

For path, I have tried absolute, and relative paths, using Path package with '__dirname", process.env.cwd(), used environment variable etc. I even tried with assets too. I am using AWS cdk to form cloud formation using nodejs. My intention is to invoke google apis using service account credentials with AWS lambda.

import {GoogleAuth} from 'google-auth-library'
 const auth = new GoogleAuth({
    keyFile: 'path/to/file',
    scope: SCOPES
})
undefined	ERROR	Uncaught Exception 	{"errorType":"Error","errorMessage":"ENOENT: no such file or directory, open '/keys/config.json'","code":"ENOENT","errno":-2,"syscall":"open","path":"/keys/config.json","stack":["Error: ENOENT: no such file or directory, open '/keys/config.json'","    at Object.openSync (node:fs:601:3)","    at Object.readFileSync (node:fs:469:35)","    at Object.<anonymous> (/var/task/index.js:533512:28)","    at Module._compile (node:internal/modules/cjs/loader:1254:14)","    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)","    at Module.load (node:internal/modules/cjs/loader:1117:32)","    at Module._load (node:internal/modules/cjs/loader:958:12)","    at Module.require (node:internal/modules/cjs/loader:1141:19)","    at require (node:internal/modules/cjs/helpers:110:18)","    at _tryRequireFile (file:///var/runtime/index.mjs:912:37)"]}
1 Answer
0

As mentioned in the NodeJS API error documentation: [+] https://nodejs.org/api/errors.html ENOENT (No such file or directory): Commonly raised by fs operations to indicate that a component of the specified pathname does not exist. No entity (file or directory) could be found by the given path.

Taking a deeper look at the error, it seems that the error originates from fs readFileSync() function. I am able to replicate this error using fs : const path = require('path'); const fs = require('fs'); exports.handler = async (event) => { return fs.readFileSync('/keys/config.json') };

Assuming that the function has the below directory structure:

  • Function-name
    • index.js
    • keys/config.json

The path specified is /key/config.json which will not work as the actual path of the file in the above directory structure is /var/task/key/config.json in lambda execution environment. We need to provide 'keys/config.json' as shown in the link below: const path = require('path'); const fs = require('fs'); exports.handler = async (event) => { return fs.readFileSync('keys/config.json')}; // or /var/task/keys/config.json

My config.json file contains text 'ABC' and the function returns '65 66 67' (ASCII values) as expected.

profile picture
Manav_B
answered a year ago
  • I am not getting above one. Since the KeyFIle prop of GoogleAuth requires filePath to json file. How can I reference the same? That prop is not required content instead file Path. Though I reference the correct file path, that was not recognized by the Lambda environment.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions