0
votes

When going through the tutorial for creating a basic slack app with google cloud, the deployment stage fails with:

>gcloud functions deploy kg_search --runtime python37 --trigger-http Deploying function (may take a while - up to 2 minutes)...failed. ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. Error message: 'API_KEY'

Trying to figure out how to fix this

2

2 Answers

1
votes

In line 29 the code os.environ['API_KEY'] is trying to fetch the API_KEY from shell environment variables but as it's undefined the program crashes. As you discovered, when you get rid of the first part of the or statement the exception is not raised anymore.

The way to set environment variables when you deploy Google Cloud Functions is explained here.

Could you retry the deployment step running gcloud functions deploy kg_search --set-env-vars API_KEY=YOUR-API-KEY --runtime python37 --trigger-http and see if it works that way?

0
votes

To fix this, I went back to the main.py file, and found the following in line 29:
developerKey=os.environ['API_KEY'] or config['KG_API_KEY']). I switched it to developerKey=config['KG_API_KEY'])

If anyone knows how to avoid the error message and rely on the or statement to pull the API_KEY from the config, please comment here and for extra point make and send a PR to this file.