Google Cloud Functions Client Library
The `google-cloud-functions` Python client library provides a programmatic interface for managing Google Cloud Functions. Cloud Functions is a scalable, pay-as-you-go Functions-as-a-Service (FaaS) offering that allows you to run event-driven code without managing servers. This library is part of the larger `google-cloud-python` monorepo, with its latest version being 1.23.0, and it requires Python 3.9 or higher. While the monorepo sees frequent updates, this specific client library updates less frequently. [3, 19, 25]
Warnings
- breaking Migration from Cloud Functions 1st generation to 2nd generation (now often referred to as 'Cloud Run functions') involves significant architectural changes. Direct redeployment of a 1st Gen function as 2nd Gen can break existing triggers due to the shift from native triggers to Eventarc. [14, 21, 29]
- breaking Python versions older than 3.9 are no longer supported. Deploying functions with unsupported Python runtimes will fail. [3, 19]
- gotcha Authentication for HTTP-triggered Cloud Functions, especially when invoked programmatically, requires proper IAM permissions (`roles/cloudfunctions.invoker`) for the calling identity and often the use of an Identity Token (ID Token), not just an OAuth Access Token. Unauthenticated invocations must be explicitly enabled during deployment. [15, 16, 20, 26]
- gotcha Python dependencies for Cloud Functions are exclusively managed via a `requirements.txt` file in the root of your function's source directory. Tools like `Pipfile`/`Pipfile.lock` are not supported. For private dependencies or local packages, specific bundling or Artifact Registry configurations are required. The `functions-framework` library should be explicitly listed in `requirements.txt`. [1, 6, 8]
- gotcha Cloud Functions (2nd gen) are now referred to as 'Cloud Run functions' due to their underlying infrastructure being Cloud Run. While the `google-cloud-functions` client library can manage both generations, understanding this naming convention and the benefits of 2nd gen (concurrency, longer timeouts, Eventarc integration) is crucial. [12, 14, 29]
Install
-
pip install google-cloud-functions -
pip install functions-framework
Imports
- CloudFunctionsServiceClient
from google.cloud.functions_v2 import CloudFunctionsServiceClient
- http, CloudEvent
import functions_framework @functions_framework.http # or @functions_framework.cloud_event
Quickstart
import functions_framework
import os
@functions_framework.http
def hello_http(request):
"""HTTP Cloud Function that responds to HTTP requests.
Args:
request (flask.Request): The request object.
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`.
"""
request_json = request.get_json(silent=True)
request_args = request.args
# Example of accessing an environment variable set during deployment
target_name = os.environ.get('FUNCTION_TARGET_NAME', 'World')
name = target_name
if request_json and 'name' in request_json:
name = request_json['name']
elif request_args and 'name' in request_args:
name = request_args['name']
return f'Hello {name}!'
# To deploy this function (save as main.py):
# gcloud functions deploy hello-http --gen2 --runtime=python312 \
# --region=us-central1 --source=. --entry-point=hello_http --trigger-http \
# --set-env-vars=FUNCTION_TARGET_NAME=Pythonista
# To test locally after `pip install functions-framework`:
# functions-framework --target hello_http --debug
# Then send a request to http://localhost:8080/