Functions Framework for Python
Functions Framework for Python is an open-source Function as a Service (FaaS) framework, enabling developers to seamlessly write and deploy portable Python functions. Developed by the Google Cloud Functions team, it brings flexibility to function deployment, allowing these functions to run effortlessly across various environments, such as Google Cloud Functions, local development machines, Cloud Run, Cloud Run for Anthos, and Knative-based environments.
Features
The Functions Framework offers capabilities that include:
- Starting a local server for quick testing and debugging.
- Invoking functions in response to incoming requests without writing an HTTP server from scratch.
- Automatically handling events following the CloudEvents specification.
- Maintaining portability across different serverless platforms.
Installation
Installing the Functions Framework can be easily done using pip:
pip install functions-framework
For deployment purposes, adding the Functions Framework to a requirements.txt
file ensures it gets included in your environment:
functions-framework==3.*
Quickstarts
HTTP Function (Hello World)
To create a simple HTTP function, start by writing a main.py
file:
import flask
import functions_framework
@functions_framework.http
def hello(request: flask.Request) -> flask.typing.ResponseReturnValue:
return "Hello world!"
Run your function locally using:
functions-framework --target hello --debug
Navigate to http://localhost:8080/
in your browser, or use curl
:
curl localhost:8080
# Output: Hello world!
CloudEvent Function
To handle CloudEvents, create a main.py
file with:
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}")
Run it with:
functions-framework --target=hello_cloud_event
Post a CloudEvent using curl
:
curl -X POST localhost:8080 \
-H "Content-Type: application/cloudevents+json" \
-d '{
"specversion" : "1.0",
"type" : "example.com.cloud.event",
"source" : "https://example.com/cloudevents/pull",
"id" : "A234-1234-1234",
"data" : "hello world"
}'
Error Handling
Functions Framework supports error handling similar to Flask's errorhandler
.
import functions_framework
@functions_framework.errorhandler(ZeroDivisionError)
def handle_zero_division(e):
return "I'm a teapot", 418
def function(request):
1 / 0
return "Success", 200
This code catches ZeroDivisionError
and responds with a custom status.
Pub/Sub Emulator
Functions can interact with a Pub/Sub emulator, starting with main.py
:
def hello(event, context):
print("Received", context.event_id)
Start the Framework and Pub/Sub emulator as indicated in the example above, then create topics and subscriptions to see your function respond to Pub/Sub messages.
Deploying on Serverless Platforms
Google Cloud Functions
Deploying on Google Cloud Functions can be done directly from code without explicitly adding the Functions Framework to dependencies.
Container Environments and Cloud Run
For environments like Cloud Run or Knative, containers come into play. Use Google Cloud buildpacks to build and run your function within a container.
Configuration
The Functions Framework is configurable through command-line flags or environment variables, controlling server behavior, function invocation, signature types, and more.
Enabling Google Cloud Function Events
The Framework can automatically process Google Cloud Function event payloads, needing just the correct function signature:
def hello(event, context):
print(event)
print(context)
Set signature types appropriately for event-driven architectures.
Advanced Examples and Contributions
The Functions Framework's repository includes numerous advanced examples and encourages community contributions. Developers can explore additional examples or contribute via GitHub by referring to the contributing guidelines.
Functions Framework for Python bridges the gap between efficient function development and robust deployment across cloud environments, simplifying how developers build serverless applications in Python.