3

In my serverless.yml file, I’m trying to add an environment variable called GOOGLE_APPLICATION_CREDENTIALS which points to my service account credentials JSON file but when added to the serverless file I'm getting an error Environment variable GOOGLE_APPLICATION_CREDENTIALS must contain string

I tried adding the environment variable GOOGLE_APPLICATION_CREDENTIALS using AWS CLI and it worked fine. But I want to add the environment variable from serverless file.

I’ve tried the below methods but none of the method seem to work

environment: GOOGLE_APPLICATION_CREDENTIALS: ‘${file(./serviceAccountCreds.json)}’

environment: GOOGLE_APPLICATION_CREDENTIALS: “${file(./serviceAccountCreds.json)}”

environment: GOOGLE_APPLICATION_CREDENTIALS: ${file(./serviceAccountCreds.json)}

My use case is I need to load the google application credentials to call the GCP APIs from the AWS lambda. I’ve read answers regarding support for google cloud functions for setting the environment variable but doesn’t seem to help with the AWS functions. Not sure the support in generic one or only to GCP functions.

Edited : Tried setting the environment variable at the run time process.env.GOOGLE_APPLICATION_CREDENTIALS as well and worked. But this still leaves me with a question whether the serverless has support of setting env.variables to JSON files as a whole.

Links I followed:

1 Answers1

0

Try setting a variable like this:

GOOGLE_APPLICATION_CREDENTIALS=$(cat ./serviceAccountCreds.json)

wchich will set the value of the variable to whatever content is in your credentials JSON file.

If the value has to contain only the path to a json file then try this:

GOOGLE_APPLICATION_CREDENTIALS=./serviceAccountCreds.json

You may also find this question interesting (very simillar case). And here's some discussion on how to pass a variable from a file in Bash. Lastly - some very basic examples on how to work with variables.

Wojtek_B
  • 4,245
  • 1
  • 7
  • 21
  • I've tried setting the environment variable as **GOOGLE_APPLICATION_CREDENTIALS: $(cat /serviceAccountCreds.json)**. But surprisingly, the path for the file is resolving to `/home/a/b/c/d/$(cat ` and therefore getting into Error: The file at **$(cat /serviceAccountCreds.json) does not exist, or it is not a file. ENOENT: no such file or directory, lstat '/home/a/b/c/d/$(cat '** – Akhil Vadrevu Dec 22 '20 at 17:01
  • 1
    Try using the equal sign "=" instead of the colon ":" in your command; like so: `GOOGLE_APPLICATION_CREDENTIALS=./serviceAccountCreds.json` – Wojtek_B Dec 23 '20 at 10:14
  • I'm trying to set the environment variable in the serverless.yml file and setting the env,variable as `GOOGLE_APPLICATION_CREDENTIALS=./serviceAccountCreds.json` throws YAML exception message as `can not read an implicit mapping pair; a colon is missed in`. Settling with setting the env.variable process.env.GOOGLE_APPLICATION_CREDENTIALS at the run time. This still leaves me with a question whether serverless has support to set the env.variables to JSON file for AWS environment. – Akhil Vadrevu Dec 24 '20 at 15:16
  • I've found this answer (very similar case) - maybe it will give you some idea: https://forum.serverless.com/t/google-credential-json-file-on-serverless-aws/3722/2 – Wojtek_B Dec 28 '20 at 10:03
  • Thanks @Wojtek_B. The article is also clueless about setting the environment variable in server-less framework but there seems to be other alternative of setting environment variables at the run time. – Akhil Vadrevu Dec 29 '20 at 12:15