This utility is a simple tool that allows running Google Cloud Functions using the Python runtime via a local Flask app. You can then deploy the function as-is to Google Cloud.
It's mostly a prototyping utility used to interact with other APIs or perform light data transformations. Its authentication method should not be used alone if you're running your Cloud Functions in production (read more).
The Functions created with this utility can return a Pandas DataFrame or a boolean, and can, therefore, be used to run simple tests or retrieve and format data for basic use cases.
Don't forget limitations around Cloud Functions.
- Use
venv
to setup a Python 3.7 environment - Install
Pandas
,Flask
flask run
Before running flask run
, make sure you set the relevant environment variables.
export FLASK_APP=server.py
export FLASK_ENV=development
export GCFP_TOKEN='securetoken'
securetoken
is a required value that you will pass in your Cloud Function http calls. You can generate one usingopenssl rand -base64 256
. Note that this value is required even if your Cloud Function requires authentication at the Google Cloud level.
Url encode securetoken
and call:
http://127.0.0.1:5000/?gcfp_token={{urlencoded_securetoken}}¶m1=value1¶m2=value2&script_name=demo_script
One thing you can do, is re-use the same function to invoke different custom scripts.
- Create your custom script using
demo_script.py
as a template - Call that script my passing its name in your call's
script_name
parameter
- Make sure to be in
/functions
and havegcloud
installed when running this command (see gcloud quick start). - This command is using some defaults, be careful, it has
--allow-unauthenticated
and you should remove this flag if you want to rely on Google Cloud authentication
gcloud functions deploy {{function_name}} --entry-point main --runtime python37 --trigger-http --allow-unauthenticated --region europe-west2 --set-env-vars=GCFP_TOKEN='{{securetoken}}' --memory=128MB --timeout=120
Errors will show in your CLI with more details. The API returns generic HTTP codes
403 Forbidden
: This is likely due to using the wrong gcfp_token, not using a gcfp_token, or having no token environment variable set for that function500 Internal Server Error
: This is likely due to an error loading the custom script
- Supports only one gcfp token
- Limited use cases