Skip to main content

An open source FaaS (Function as a service) framework for writing portable Python functions -- brought to you by the Google Cloud Functions team.

Project description

Functions Framework for Python

PyPI version

Python unit CI Python lint CI Python conformace CI

An open source FaaS (Function as a service) framework for writing portable Python functions -- brought to you by the Google Cloud Functions team.

The Functions Framework lets you write lightweight functions that run in many different environments, including:

The framework allows you to go from:

def hello(request):
    return "Hello world!"

To:

curl http://my-url
# Output: Hello world!

All without needing to worry about writing an HTTP server or complicated request handling logic.

Features

  • Spin up a local development server for quick testing
  • Invoke a function in response to a request
  • Automatically unmarshal events conforming to the CloudEvents spec
  • Portable between serverless platforms

Installation

Install the Functions Framework via pip:

pip install functions-framework

Or, for deployment, add the Functions Framework to your requirements.txt file:

functions-framework==2.3.0

Quickstarts

Quickstart: Hello, World on your local machine

Create an main.py file with the following contents:

def hello(request):
    return "Hello world!"

Your function is passed a single parameter, (request), which is a Flask Request object.

Run the following command:

functions-framework --target=hello

Open http://localhost:8080/ in your browser and see Hello world!.

Quickstart: Set up a new project

Create a main.py file with the following contents:

def hello(request):
    return "Hello world!"

Now install the Functions Framework:

pip install functions-framework

Use the functions-framework command to start the built-in local development server:

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 potentially have multiple language frameworks installed).

Send requests to this function using curl from another terminal window:

curl localhost:8080
# Output: Hello world!

Quickstart: Register your function using decorator

Create an main.py file with the following contents:

import functions_framework

@functions_framework.cloud_event
def hello_cloud_event(cloud_event):
   return f"Received event with ID: {cloud_event['id']} and data {cloud_event.data}"

@functions_framework.http
def hello_http(request):
   return "Hello world!"

Run the following command to run hello_http target locally:

functions-framework --target=hello_http

Open http://localhost:8080/ in your browser and see Hello world!.

Run the following command to run hello_cloud_event target locally:

functions-framework --target=hello_cloud_event

More info on sending CloudEvents payloads, see examples/cloud_run_cloud_events instruction.

Quickstart: Error handling

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:

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 function will catch the ZeroDivisionError and return a different response instead.

Quickstart: Pub/Sub emulator

  1. Create a main.py file with the following contents:

    def hello(event, context):
         print("Received", context.event_id)
    
  2. Start the Functions Framework on port 8080:

    functions-framework --target=hello --signature-type=event --debug --port=8080
    
  3. In a second terminal, start the Pub/Sub emulator on port 8085.

    export PUBSUB_PROJECT_ID=my-project
    gcloud beta emulators pubsub start \
        --project=$PUBSUB_PROJECT_ID \
        --host-port=localhost:8085
    

    You should see the following after the Pub/Sub emulator has started successfully:

    [pubsub] INFO: Server started, listening on 8085
    
  4. 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.

    export PUBSUB_PROJECT_ID=my-project
    export TOPIC_ID=my-topic
    export PUSH_SUBSCRIPTION_ID=my-subscription
    $(gcloud beta emulators pubsub env-init)
    
    git clone https://github.com/googleapis/python-pubsub.git
    cd python-pubsub/samples/snippets/
    pip install -r requirements.txt
    
    python publisher.py $PUBSUB_PROJECT_ID create $TOPIC_ID
    python subscriber.py $PUBSUB_PROJECT_ID create-push $TOPIC_ID $PUSH_SUBSCRIPTION_ID http://localhost:8080
    python publisher.py $PUBSUB_PROJECT_ID publish $TOPIC_ID
    

    You should see the following after the commands have run successfully:

    Created topic: projects/my-project/topics/my-topic
    
    topic: "projects/my-project/topics/my-topic"
    push_config {
      push_endpoint: "http://localhost:8080"
    }
    ack_deadline_seconds: 10
    message_retention_duration {
      seconds: 604800
    }
    .
    Endpoint for subscription is: http://localhost:8080
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Published messages to projects/my-project/topics/my-topic.
    

    And in the terminal where the Functions Framework is running:

     * 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)
     * Restarting with fsevents reloader
     * Debugger is active!
     * Debugger PIN: 911-794-046
    Received 1
    127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
    Received 2
    127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
    Received 5
    127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
    Received 6
    127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
    Received 7
    127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
    Received 8
    127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
    Received 9
    127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 -
    Received 3
    127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 -
    Received 4
    127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 -
    

For more details on extracting data from a Pub/Sub event, see https://cloud.google.com/functions/docs/tutorials/pubsub#functions_helloworld_pubsub_tutorial-python

Quickstart: Build a Deployable Container

  1. Install Docker and the pack tool.

  2. Build a container from your function using the Functions buildpacks:

     pack build \
         --builder gcr.io/buildpacks/builder:v1 \
         --env GOOGLE_FUNCTION_SIGNATURE_TYPE=http \
         --env GOOGLE_FUNCTION_TARGET=hello \
         my-first-function
    
  3. Start the built container:

     docker run --rm -p 8080:8080 my-first-function
     # Output: Serving function...
    
  4. Send requests to this function using curl from another terminal window:

     curl localhost:8080
     # Output: Hello World!
    

Run your function on serverless platforms

Google Cloud Functions

This Functions Framework is based on the Python Runtime on Google Cloud Functions.

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.

Cloud Run/Cloud Run on GKE

Once you've written your function and added the Functions Framework to your requirements.txt file, all that's left is to create a container image. Check out the Cloud Run quickstart for Python to create a container image and deploy it to Cloud Run. You'll write a Dockerfile when you build your container. This Dockerfile allows you to specify exactly what goes into your container (including custom binaries, a specific operating system, and more). Here is an example Dockerfile that calls Functions Framework.

If you want even more control over the environment, you can deploy your container image to Cloud Run on GKE. With Cloud Run on GKE, you can run your function on a GKE cluster, which gives you additional control over the environment (including use of GPU-based instances, longer timeouts and more).

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
--dry-run DRY_RUN A flag that allows for testing the function build from the configuration without creating a server. Default: False

Enable Google Cloud Functions Events

The Functions Framework can unmarshall incoming Google Cloud 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:

def hello(event, context):
    print(event)
    print(context)

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.

See the running example.

Enable CloudEvents

The Functions framework can also unmarshall incoming CloudEvents payloads to the cloud_event object. This will be passed as a CloudEvent to your function when it receives a request. Note that your function must use the CloudEvents-style function signature:

def hello(cloud_event):
    print(f"Received event with ID: {cloud_event['id']}")

To enable automatic unmarshalling, set the function signature type to cloudevent using the --signature-type command-line flag or the FUNCTION_SIGNATURE_TYPE environment variable. By default, the HTTP signature type will be used and automatic event unmarshalling will be disabled.

For more details on this signature type, check out the Google Cloud Functions documentation on background functions.

Advanced Examples

More advanced guides can be found in the examples/ directory. You can also find examples on using the CloudEvent Python SDK here.

Contributing

Contributions to this library are welcome and encouraged. See CONTRIBUTING for more information on how to get started.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

functions-framework-3.0.0.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

functions_framework-3.0.0-py3-none-any.whl (30.0 kB view details)

Uploaded Python 3

File details

Details for the file functions-framework-3.0.0.tar.gz.

File metadata

  • Download URL: functions-framework-3.0.0.tar.gz
  • Upload date:
  • Size: 25.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for functions-framework-3.0.0.tar.gz
Algorithm Hash digest
SHA256 422aa72c54c5e26076e34e074eadf3e713595a25b387ec51068aa7b9952a5047
MD5 382b41d1baa8691304452aa880cccb3a
BLAKE2b-256 b0743bc344da8d4c3a6338af93a85f2f6cdc5c679806a5503b1733f5e1dd52e5

See more details on using hashes here.

File details

Details for the file functions_framework-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: functions_framework-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 30.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for functions_framework-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c1fe2dd78e9ac7e87aae7f0f59a8c4bd2dec2b31010c2552c8c4dc061d52011
MD5 10fb4863f6cb5166d3f439885a43abd7
BLAKE2b-256 de009753b3ed6a8f05909d00b78c98766d170846fe6d8892b3f3d8608b1067b9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page