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.
If you have activated IAM, you may need certain IAM permissions to carry out some actions described on this page. This means:
- you are the Owner of the Scaleway Organization in which the actions will be carried out, or
- you are an IAM user of the Organization, with a policy granting you the necessary permission sets
- You have an account and are logged into the Scaleway console
- You have created a functions namespace
- You know how to create a Function
How to upload your zip file to the Scaleway Console
- Package your dependencies on your local computer, as explained in the Configure your package section.
- Create a Zip archive containing the files and folders you need.
- Go to the Functions section of the Scaleway console and click on the function namespace you want to configure.
- Click + Create a Function. The function creation page displays.
- On the function creation page, select Upload a Zip as Code entry type:
- Drop your ZIP in the reserved field.
- Specify your handler path.
- Configure your function.
- 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
- Make sure to provide the right handler name, namely
src/handlers/hello.say_hello
andsrc/handlers/world.to_the_world
- Provide a custom handler name for each of these handlers. Each of your functions will know which handler file to run:
hello
→src/handlers/hello.say_hello
andworld
→src/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
Your project will look like this:
src
| -- 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
- 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.
The example above uses python3 runtime, but you can easily change the docker image from rg.fr-par.scw.cloud/scwfunctionsruntimes-public/python-dep:3.10
to rg.fr-par.scw.cloud/scwfunctionsruntimes/python-dep:2.7
, rg.fr-par.scw.cloud/scwfunctionsruntimes/python-dep:3.8
, …
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.
Currently, a function handler must be a named exported component:
module.exports.myHandler = (event, context, callback) => {}
- Make sure to provide the right handler name, namely
src/handlers/hello.sayHello
andsrc/handlers/world.toTheWorld
.src
| -- handlers
| -- hello.js => module.exports.sayHello
| -- world.js => module.exports.toTheWorld - Provide a custom handler name for each of these handlers. Each of your functions will know which handler file to run:
hello
→src/handlers/hello.sayHello
andworld
→src/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.
- 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/world" subdirectory
handler.go -> package main at the root of serverless project
go.mod
go.sum - Provide a custom handler name for each of these handlers. Each of your function will know which handler file to run:
hello
→handlers/hello
,world
→handlers/world
, and for thehandler.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).
Be careful to pack the code and its dependencies directly, not the folder where the project is stored.
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>
How to configure your package using PHP
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 the handler file handler.php
.
├── composer.json -> optional
└── handler.php -> handler function defined here
- Make sure to provide the right handler name, in the previous folder tree :
handler.handle
- The
composer.json
file enables you to define dependencies that will be installed when deploying your functions, for more information, you can check the official documentation of composer
How to configure your package using Rust
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 the handler file handler.rs
inside the src
folder.
rust/
-- Cargo.toml
-- Cargo.lock
-- src/
--- handler.rs -> Handle async function defined here
- Specify the file to be executed in your
Cargo.toml
file[lib]
path = "src/handler.rs" - Make sure to provide the right handler name, namely
Handle
Additional dependencies
If you need external dependencies for your Rust handlers, you can provide these dependencies in your Cargo.toml
file.
Our runtimes automatically install your dependencies at Build time (once you start the function deployment).
Dependencies installation at the build-time results in longer builds.
You can also package your dependencies with the command cargo vendor
, and provide your generated vendor directory to the function archive. This approach will save you some time during builds:
rust/
-- Cargo.toml
-- Cargo.lock
-- src/
--- handler.rs
-- vendor # Dependencies should be installed inside your vendor directory
--- <your-dependencies>