NavigationContentFooter
Jump toSuggest an edit

How to package your function and dependencies to a zip file and upload it

Reviewed on 16 April 2024Published on 26 May 2021

This page explains how to upload your functions and their dependencies as a zip file using the Scaleway console.

This feature allows you to add your libraries or static files to your function.

Before you start

To complete the actions presented below, you must have:

  • A Scaleway account logged into the console
  • Owner status or IAM permissions allowing you to perform actions in the intended Organization
  • A Functions namespace
  • installed jq

How to package a function into zip file

There are different methods to deploy functions and some of them require the function to be packaged into a zip file.

To match the Scaleway build pipelines requirements, functions zip files must contain the content of the root folder you want to publish.

How to create a zip file

Use the zip command to create an archive of your function and its dependencies:

zip -r myFunction.zip myFunction/

The example above will create a .zip archive that contains the myFunction folder and its content. You can then upload this archive to your Serverless Functions.

How to upload your zip file

  1. Package your dependencies on your local computer, as explained in the Configure your package section.
  2. Create a ZIP archive containing the files and folders you need.
  3. Go to the Functions section of the Scaleway console and click on the functions namespace you want to configure.
  4. Click + Create function. The function creation page displays.
  5. On the function creation page, choose your desired runtime.
  6. Select Upload a ZIP under Function code.
  7. Drag and drop your zip file in the reserved field or upload it.
  8. Specify your handler path.
  9. Configure your function.
  10. Click Create function to finish.
Note

Refer to our dedicated documentation for more information on how to create Serverless Functions

How to configure your package

Handler

The Handler name is a path to the handler file, suffixed with the function name to use as a handler. In the following example, we use one handler, hello.py, inside the src/handlers folder.

.
└── handlers
└── hello.py → def say_hello(event, context): ...
  1. Package your function. On Unix systems, you can use the zip utility:

    zip -r functions.zip handlers/
  2. Upload the archive in the console.

  3. Provide a custom handler name. Here, the handler is handlers/hello.say_hello.

    Note

    By default, the handler path is handler.handle (def handle in handler.py). Refer to the Functions handlers reference for more information.

Dependencies

Additional dependencies must be included inside a package directory at the root of your archive.

Your directory structure should look like this:

.
├── handlers
│ ├── handler.py
│ └── second_handler.py
└── package → Your vendored Python dependencies
└── requests

To package your functions into an archive that can be uploaded to the console, you can use the zip utility:

zip -r functions.zip handlers/ package/

Python standard dependencies

In addition, you can install your dependencies in the package directory. To do so, run the following command:

pip install requests --target ./package

Or with a requirements.txt file:

pip install -r requirements.txt --target ./package

Specific libraries (with needs for specific C compiled code)

In some specific cases, you might need to install libraries that require specific C compiled code such as:

  • numpy
  • tensorflow
  • pandas
  • scikit-learn
  • psycopg2 and others.

Our Python runtimes run on top of Alpine Linux environments, for these specific dependencies, you will have to install your dependencies inside a Docker container, with a specific image, that we are providing to our users. Run the following command from the root of your project to install your dependencies before uploading your source code and deploying your function:

PYTHON_VERSION=3.10 # or 3.7, 3.8, ...
docker run --rm -v $(pwd):/home/app/function --workdir /home/app/function rg.fr-par.scw.cloud/scwfunctionsruntimes-public/python-dep:$PYTHON_VERSION pip install -r requirements.txt --target ./package

This command will run pip install with the given requirements.txt file inside a docker container compatible with our function runtimes, and pull the installed dependencies locally to your package directory. As these dependencies have been installed on top of Alpine Linux with our compatible system libraries, you will be able to upload your source code and deploy your function properly.

How to manage multiple handles in the same zip file

To enhance deployment simplicity, all runtimes support the inclusion of multiple handlers, either within a single file or across multiple files.

Example:

.
├── myFuncA.lang (contains handlerA() and handlerB())
└── myFuncB.lang (contains handlerC())

Like that, you can create 3 functions with the same zip file simply by changing the handler parameter to match the handler you want:

  • myFuncA.handlerA
  • myFuncA.handlerB
  • myFuncB.handlerC

You can also create a single Serverless Function with several handlers in the same zip file, and change the handler parameter according to your needs.

See also
How to test a functionHow to manage a function
Docs APIScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCarreer
© 2023-2024 – Scaleway