Functions Framwowrk for Python

The purpose of this repo is to outline how GCP Functions can be deployed in an automated way by making a change in the source code's GitHub repo and the pipeline deploying to GCP Function.

The Functions Framework allows you to write functions and run them in Dev then promote them to Cloud Functions in GCP or Cloud Run. Take a look at official docs for more details.

Run Locally

Install functions framework: pip install functions-framework

Create main.py

def hello(request):
    return "Hello world!"

Run function locally using following command: functions-framework --target=hello

target should be the function that is invoked when the function is called.

Open http://localhost:8080 in your browser and see the response.

Run tests by using pytest:

pytest test_script.py

Deploy to Cloud

You can use gcloud on your machine to deploy to cloud from your terminal of choice. Install gcloud sdk from here.

Run the following command to authenticate against gcp:

gcloud auth login

This will open a browser and ask user to login with GCP credentials.

Run the following command and select the project where the function needs to be deployed to.

gcloud config set project <project id>

Deploy function using:

gcloud functions deploy NAME --runtime python37 --trigger-http

NOTE: This function will be publically available. Extra settings need to be applied to secure the function (using service account permissions etc).

Ensure that NAME is the name of the function that is to be called when the function is run.

Navigate to cloud functions in GCP to view your newly deployed function: https://console.cloud.google.com/functions/