Pants can create a Google Cloud Function-compatible zip file from your Python code, allowing you to develop your functions in your repository.
FYI: how Pants does this
Under-the-hood, Pants uses the [Lambdex](🔗) project. First, Pants will convert your code into a [Pex file](🔗). Then, Pants will use Lambdex to convert the Pex into a zip file understood by Google Cloud Functions.
## Step 1: Activate the Python Google Cloud Function backend
Add this to your `
This adds the new `
python_google_cloud_function` target, which you can confirm by running `
./pants help python_google_cloud_function `
## Step 2: Define a `
python_google_cloud_function ` target
First, add your Cloud function in a Python file like you would [normally do with Google Cloud Functions](🔗), such as creating a function `
def my_handler_name(event, context)` for event-based functions.
Then, in your BUILD file, make sure that you have a `
python_source` or `
python_sources` target with the handler file included in the `
sources` field. You can use [`
./pants tailor`](🔗) to automate this.
Add a `
python_google_cloud_function` target and define the `
handler`, and `
type` fields. The `
type` should be either `
"event"` or `
"http"`. The `
runtime` should be one of the values from <https://cloud.google.com/functions/docs/concepts/python-runtime>. The `
handler` has the form `
handler_file.py:handler_func`, which Pants will convert into a well-formed entry point. Alternatively, you can set `
handler` to the format `
Pants will use [dependency inference](🔗) based on the `
handler` field, which you can confirm by running `
./pants dependencies path/to:cloud_function`. You can also manually add to the `
You can optionally set the `
output_path` field to change the generated zip file's path.
resource` instead of `
file` / `
files` targets will not be included in the built Cloud Function because filesystem APIs like `
open()` would not load them as expected. Instead, use the `
resource` / `
resources` target. See [Assets and archives](🔗) for further explanation.
## Step 3: Run `
Now run `
./pants package` on your `
python_google_cloud_function` target to create a zipped file.
Running from macOS and failing to build?
Cloud Functions must run on Linux, so Pants tells PEX and Pip to build for Linux when resolving your third party dependencies. This means that you can only use pre-built [wheels](🔗) (bdists). If your project requires any source distributions ([sdists](🔗)) that must be built locally, PEX and pip will fail to run.
If this happens, you must either change your dependencies to only use dependencies with pre-built [wheels](🔗) or find a Linux environment to run `
## Step 4: Upload to Google Cloud
You can use any of the various Google Cloud methods to upload your zip file, such as the Google Cloud console or the [Google Cloud CLI](🔗).
You must specify the handler as `