Deploy a Python function

Last updated about 2 months ago

Koyeb provides native support of Python functions. The native support includes continuous deployment features to automatically deploy your Python code each time you push your git commits.

We take care of the complete continuous deployment process for you:

  • During the build process, we build production-ready images and automatically install your Python dependencies
  • During the deployment process, we make sure all servers are always up-to-date and ready to handle new events without service disruption

In this guide, we will briefly describe how to configure Python git repositories on Koyeb to get automated continuous deployment.

Simple Python Hello World

We will start by creating a simple Hello World Python project without any dependency and push it to Koyeb with git.

The Git repository

We will create two files and a directory in a git repository.

1✗ tree -a hello-python-world
2hello-python-world
3├── koyeb.yaml
4└── hello_python_world
5    └── __init__.py
6

The Koyeb File

In our koyeb.yaml file, we will mention two key things:

  1. the runtime: the language version that will be used to execute your code
  2. the handler: the python function that will be called each time an event is received
1functions:
2  - name: hello-python-world
3    runtime: python3.8
4    handler: hello_python_world.handler
5

Simple Python Code

In the __init__.py, we add a simple dump of the parameters automatically passed to the function by the Koyeb runtime:

1def handler(event, context):
2    print("Got data", event)
3    print("Got context", context)
4    print("Got context event", context.event)
5    return {"response": "ok"}
6

Local testing

For local testing purpose, we provide a Docker image which replicates the behavior of the Koyeb production build engine and runtime. The image is available on the public Docker Hub: koyeb/runtime:build-python3.

When launching the Docker container, you will need to:

  • mount the root folder of your Python project as volume in /var/task/ with -v
  • use /var/buildpack/bin/debug as the entrypoint to trigger the build and launch an HTTP webserver with the built application with --entrypoint
  • export the port 8080 to reach the webserver running in the container with -p
  • tell the runtime which function to execute using a KOYEB_HANDLER environment variable with -e

Here is what the command looks like when run from the directory of your project:

1docker run -p 8080:8080 -e KOYEB_HANDLER=hello_python_world.handler -v $PWD:/var/task --rm -ti --entrypoint /var/buildpack/bin/debug koyeb/runtime:build-python3
2

Local invocation

The Koyeb events respect the CloudEvents specification for interoperability reason. To invoke the function using the local development runtime, you need to use HTTP headers to mock the function's event. Here are the headers to use:

  • ce-source: A string representing the event source.
  • ce-type: A string representing the event type.
  • ce-subject: A string representing the event subject.
  • ce-specversion: The CloudEvents spec version, 1.0 in our case.
  • ce-id: A string representing the event ID which can be arbitrary in our case.

For instance to invoke a function with a basic payload, run the following command:

1 curl localhost:8080  -H 'content-type: application/json' -H "ce-specversion: 1.0" -H "ce-source: local-invokation" -H "ce-type: dev" -H "ce-subject: local-function-invokation" -H "ce-id: 1" -d '{"body": "Hello World!"}'
2

Push to Koyeb

Once you are ready, simply git push to deploy to Koyeb.

Invoke

You can manually invoke your function. After invoking it, these are the logs you should see:

12020-08-28T12:59:24.125636504Z stderr F Got data {'data': {'hello': 'world'}}
22020-08-28T12:59:24.125667805Z stderr F Got context <bootstrap.Context object at 0x7f26a8df3ee0>
32020-08-28T12:59:24.12567988Z stderr F Got context event <bootstrap.Event object at 0x7f26a8df3fd0>
4

Manage Dependencies with Poetry

You can use Poetry to manage your function dependencies. The Koyeb platform will build and deploy your dependencies each time you push. Simply add a pyproject.toml in your repository.

We will provide more details about this soon.

footer-frame