0
votes

Firebase Hosting can be configured to direct requests to a Cloud Function:

Will that work for a Python Cloud Function deployed directly to Cloud Functions, i.e., not using firebase deploy? That is particularly relevant for Python Functions, as Firebase can only deploy Node.js Functions.

1

1 Answers

3
votes

I've verified that this use case is supported.

You can deploy a python cloud function using the gcloud cli, and then deploy a url to your firebase hosting target that rewrites to this python function.

Here is an example firebase.json file:

{
  "hosting": [
    {
      "target": "optional_target",
      "public": "public",
      "ignore": [
        "*"
      ],
      "rewrites": [
        {
          "source": "**",
          "function": "my_python_function"
        }
      ]
    }
  ]
}

Firebase requires a public directory even though it's unused in this case. So you can create a simple public directory with a .gitkeep file to make sure it's preserved in your source code repository. The ignore attribute in the firebase.json file makes sure that the .gitkeep file is not uploaded to firebase hosting.

For convenience, here is a simple bash script to deploy both the function and the hosting configuration in one command:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ORGINAL_PWD=$PWD

PROJECT_ID=$1

cd $DIR/../functions

gcloud functions deploy my_python_function \
  --project "$PROJECT_ID" \
  --runtime python37 \
  --trigger-http

firebase use $PROJECT
firebase deploy

cd $ORGINAL_PWD

In this case, the subdirectory functions contains a main.py module with the my_python_function defined.

Also to be clear, the use of firebase use $PROJECT, as well as defining a target in firebase.json, requires that a .firebaserc file be configured with the project/target mappings.