Scaleway DocumentationComputeFunctionsHow to
Package a function in a zip-file

Jump toUpdate content

How to package your function and upload it as a zip-file

This page explains how to upload your code as a zip file using the Scaleway Console. This feature enables you to add your librairies or static files to your function.

Requirements:

How to upload your zip file to the Scaleway Console

  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 function namespace you want to configure.

  4. Click + Create a Function. The function creation page displays.

  5. On the function creation page, select Upload a Zip as Code entry type:

  6. Drop your ZIP in the reserved field.

  7. Specify your handler path.

  8. Configure your function.

  9. Click Create a function to finish.

How to configure your package using Python

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 two handlers hello.py and world.py inside the src/handlers folder.

src
-- handlers
---- hello.py => def say_hello
---- world.py => def to_the_world
  1. Make sure to provide the right handler name, namely src/handlers/hello.say_hello and src/handlers/world.to_the_world

  2. Provide a custom handler name for each of these handlers. Each of your functions will know which handler file to run: hellosrc/handlers/hello.say_hello and worldsrc/handlers/world.to_the_world.

    Note:

    By default, the handler path is handler.handle (def handle in handler.py).

Additional dependencies

Additional dependencies must be included inside a package directory at the root of your archive or project. To do so, launch the following command:

# At the root of your archive
mkdir package
- requirements.txt
- handlers
--- handler.py => import requests
--- secondHandler.py => import requests
- package
--- requests
--- ...

Standard dependencies

In addition, you can install your dependencies in the package directory. To do so, launch 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 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:

docker run --rm -v $(pwd):/home/app/function --workdir /home/app/function rg.fr-par.scw.cloud/scwfunctionsruntimes/python-dep3:v1.2.6 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.

Note:

The example above uses python3 runtime, but you can easily change the docker image from rg.fr-par.scw.cloud/scwfunctionsruntimes/python-dep3:v1.2.6 to rg.fr-par.scw.cloud/scwfunctionsruntimes/python-dep2:v1.2.6

How to configure your package using Node

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 two handlers hello.js and world.js inside the src/handlers folder.

Note:

Currently, a function handler must be a named exported component:

module.exports.myHandler = (event, context, callback) => {}
  1. Make sure to provide the right handler name, namely src/handlers/hello.sayHello and src/handlers/world.toTheWorld.

    src
    -- handlers
    ---- hello.js => module.exports.sayHello
    ---- world.js => module.exports.toTheWorld
  2. Provide a custom handler name for each of these handlers. Each of your functions will know which handler file to run: hellosrc/handlers/hello.sayHello and worldsrc/handlers/world.toTheWorld.

Additional dependencies

If you need to push external dependencies for your node.js functions, you must package your node_modules directory into your deployment archive. To do so, launch the following command:

-- handler.js
-- node_modules
---- <your-dependencies>

You can use tools such as webpack or NCC a CLI tool to build node.js executables, which packages your code into separate files. Then, you will be able to upload your compiled handler file reducing the size of your bundle. For example: ncc handler.js -o build/handler.js # -> Builds dist/inde

Then, set up your function handler to be: build/handler.js if you package the whole build directory. Do not forget to point the function handler property to the path of your built handler in your archive (if build/handler.myHandler then handler must be build/handler.js)

How to configure your package using Golang

Handler

Golang is a compiled language. Therefore, you must provide Scaleway Function API with a handler name pointing to the function’s directory.

  1. Provide Scaleway Function API with a handler name:
src
-- handlers
---- hello
------ go.mod
------ go.sum
------ main.go -> package main in "handlers/hello" subdirectory
---- world
------ go.mod
------ go.sum
------ main.go -> package main in "handlers/workd" subdirectory
handler.go -> package main at the root of serverless project
go.mod
go.sum
  1. Provide a custom handler name for each of these handlers. Each of your function will know which handler file to run: hellohandlers/hello, worldhandlers/world, and for the handler.go at the root of the project→.

    Note:

    By default, the handler path is . (package main at the root of the archive).

Additional dependencies

If you need external dependencies for your Golang handlers, you can provide these dependencies using Go Modules. Our runtimes automatically install your dependencies at Build time (once you start the function deployment).

Note:

Dependencies installation at the build-time results in longer builds.

-- handler.go
-- go.mod
-- go.sum

Package your dependencies with the command go mod vendor, and provide your generated vendor directory to the function archive. This approach will save you some time during builds:

-- handler.go
-- go.mod
-- go.sum
-- vendor # Dependencies should be installed inside your vendor directory
---- <your-dependencies>