1
votes

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

1 Answers

0
votes

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.