Last updated 5 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:
In this guide, we will briefly describe how to deploy a Python serverless function on Koyeb
We will start by creating a simple Hello World Python project without any dependency and push it to Koyeb with git.
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
In our koyeb.yaml
file, we will mention two key things:
runtime
: the language version that will be used to execute your codehandler
: the python function that will be called each time an event is
received1functions: 2 - name: hello-python-world 3 runtime: python3.8 4 handler: hello_python_world.handler 5
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
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:
/var/task/
with
-v
/var/buildpack/bin/debug
as the entrypoint to trigger the build and launch
an HTTP webserver with the built application with --entrypoint
-p
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
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
Once you are ready, simply git push
to deploy to Koyeb.
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
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.
Get in touch or create an account and deploy your serverless stack in minutes.