functions-framework: An open source FaaS (Function as a service) framework for writing portable Python functions -- brought to you by the Google Cloud Functions team. · Pythie
Your function is passed a single parameter, (request), which is a Flask Request object.
Run the following command:
functions-framework --target hello --debug
* Serving Flask app "hello" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
(You can also use functions-framework-python if you have multiple
language frameworks installed).
Or send requests to this function using curl from another terminal window:
curl localhost:8080
# Output: Hello world!
Quickstart: CloudEvent Function
Create an main.py file with the following contents:
import functions_framework
from cloudevents.http.event import CloudEvent
@functions_framework.cloud_event
def hello_cloud_event(cloud_event: CloudEvent) -> None:
print(f"Received event with ID: {cloud_event['id']} and data {cloud_event.data}")
Your function is passed a single CloudEvent parameter.
Run the following command to run hello_cloud_event target locally:
functions-framework --target=hello_cloud_event
In a different terminal, curl the Functions Framework server:
The framework includes an error handler that is similar to the
flask.Flask.errorhandler
function, which allows you to handle specific error types with a decorator:
You should see the following after the Pub/Sub emulator has started successfully:
[pubsub] INFO: Server started, listening on 8085
In a third terminal, create a Pub/Sub topic and attach a push subscription to the topic, using http://localhost:8080 as its push endpoint. Publish some messages to the topic. Observe your function getting triggered by the Pub/Sub messages.
On Cloud Functions, using the Functions Framework is not necessary: you don't need to add it to your requirements.txt file.
After you've written your function, you can simply deploy it from your local machine using the gcloud command-line tool. Check out the Cloud Functions quickstart.
Container environments based on Knative
Cloud Run and Cloud Run on GKE both implement the Knative Serving API. The Functions Framework is designed to be compatible with Knative environments. Just build and deploy your container to a Knative environment.
Configure the Functions Framework
You can configure the Functions Framework using command-line flags or environment variables. If you specify both, the environment variable will be ignored.
Command-line flag
Environment variable
Description
--host
HOST
The host on which the Functions Framework listens for requests. Default: 0.0.0.0
--port
PORT
The port on which the Functions Framework listens for requests. Default: 8080
--target
FUNCTION_TARGET
The name of the exported function to be invoked in response to requests. Default: function
--signature-type
FUNCTION_SIGNATURE_TYPE
The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Default: http; accepted values: http, event or cloudevent
--source
FUNCTION_SOURCE
The path to the file containing your function. Default: main.py (in the current working directory)
--debug
DEBUG
A flag that allows to run functions-framework to run in debug mode, including live reloading. Default: False
Enable Google Cloud Run function Events
The Functions Framework can unmarshall incoming
Google Cloud Run functions event payloads to event and context objects.
These will be passed as arguments to your function when it receives a request.
Note that your function must use the event-style function signature:
To enable automatic unmarshalling, set the function signature type to event
using the --signature-type command-line flag or the FUNCTION_SIGNATURE_TYPE environment variable. By default, the HTTP
signature will be used and automatic event unmarshalling will be disabled.
For more details on this signature type, see the Google Cloud Functions
documentation on
background functions.