Refer to our dedicated documentation for more information on how to create Serverless Functions
How to package your function and dependencies to a zip file and upload it
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
- 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 functions namespace you want to configure.
- Click + Create function. The function creation page displays.
- On the function creation page, choose your desired runtime.
- Select Upload a ZIP under Function code.
- Drag and drop your zip file in the reserved field or upload it.
- Specify your handler path.
- Configure your function.
- Click Create function to finish.
-
In a terminal, set an environment variable to name the zip file automatically:
export FUNCTION_ARCHIVE="function-$SCW_FUNCTION_ID.zip" -
Create a zip archive with your code:
zip $FUNCTION_ARCHIVE handler.jsNoteIf you wish to use external dependencies, you will have to package them inside the zip archive as well:
zip -r $FUNCTION_ARCHIVE package.json handler.js node_modules -
Run
ls -lh
to get the size of your archive in bytes:ls -lh-rw-r--r-- 1 user group 675 Apr 18 15:42 -
Export the archive size in bytes as an environment variable:
export ARCHIVE_SIZE=675 -
Get the presigned URL from the API.
export PRESIGNED_URL=$(curl -X GET -H "X-Auth-Token: $SCW_SECRET_KEY" \"https://api.scaleway.com/functions/v1beta1/regions/$SCW_DEFAULT_REGION/functions/$SCW_FUNCTION_ID/upload-url?content_length=$ARCHIVE_SIZE" | jq ."url") -
Run the following command to check that the presigned URL has been properly exported:
echo $PRESIGNED_URLA response like the following displays:
"https://s3.fr-par.scw.cloud/scw-database-srvless-prod/uploads/function-b0525a73-947d-4ba4-92de-17f267a7ec5a.zip?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=SCW6Z6VKJVG81FQZVB14%2F20190627%2Ffr-par%2Fs3%2Faws4_request&X-Amz-Date=20190627T092839Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=content-length%3Bcontent-type%3Bhost&X-Amz-Signature=e9f3e22f39638dac047f0f4e9ab521c7971cacb01f61f523cb948baa328a0eff"ImportantMake sure the URL is contained in double quotes (
"
), and that it does not contain raw Javascript character codes (such as\u0026
instead of&
). -
Run the following code to upload your code to the presigned URL:
curl -H "Content-Type: application/octet-stream" --upload-file $FUNCTION_ARCHIVE \-H "Content-Length: $ARCHIVE_SIZE" $PRESIGNED_URL
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): ...
-
Package your function. On Unix systems, you can use the
zip
utility:zip -r functions.zip handlers/ -
Upload the archive in the console.
-
Provide a custom handler name. Here, the handler is
handlers/hello.say_hello
.NoteBy default, the handler path is
handler.handle
(def handle in handler.py). Refer to the Functions handlers reference for more information.
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 handlers
folder.
.└── handlers└── hello.js → export {sayHello}; function sayHello(...) {}
-
Package your function. On Unix systems, you can use the
zip
utility:zip -r functions.zip handlers/ -
Upload the archive in the console.
-
Provide a custom handler name. In this case, the handler is
handlers/hello.sayHello
.
With Golang functions, the handler name should be the name of your exported handler function if your handler is at the root of your folder. If your handler is in a subdirectory, the handler name should be prefixed by the directory followed by the exported function name.
.├── go.mod├── go.sum└── handler.go → package handler with func Handle()
-
Package your function. On Unix systems, you can use the
zip
utility:zip -r functions.zip go.mod go.sum handler.go -
Upload the archive in the console.
-
Provide a custom handler name. Each of your functions will know which handler file to run:
- For the file
handler.go
at the root of the project →Handle
.
- For the file
By default, the handler path is Handle
(package main at the root of the archive).
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
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.
-
Package your functions. On Unix systems, you can use the
zip
utility:zip -r function.zip composer.json handler.php -
Upload the archive in the console.
-
Make sure to provide the right handler name, with the previous directory structure:
handler.handle
.
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.
.├── Cargo.lock├── Cargo.toml└── src└── handler.rs → pub async fn handle(...)
-
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
.
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.
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
).
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).
Dependencies installation at the build-time results in longer builds.
.├── go.mod├── go.sum└── handler.go
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:
.├── go.mod├── go.sum├── main.go└── vendor└── your-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.
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.