Avoid compressing your function using the file explorer or finder, as this method can create an additional folder in the file structure of your function, which can lead to build errors. Refer to the troubleshooting section for more information.
How to package your function 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 startLink to this anchor
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 fileLink to this anchor
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 fileLink to this anchor
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 fileLink to this anchor
- 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.
Refer to our dedicated documentation for more information on how to create Serverless Functions
-
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 packageLink to this anchor
HandlerLink to this anchor
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.
.