You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/serverless-functions/how-to/package-function-dependencies-in-zip.mdx
+24-4Lines changed: 24 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
---
2
2
meta:
3
-
title: How to package your function and dependencies to a zip file and upload it
3
+
title: How to package your function to a zip file and upload it
4
4
description: Package function dependencies in a ZIP file for Scaleway Serverless Functions.
5
5
content:
6
-
h1: How to package your function and dependencies to a zip file and upload it
6
+
h1: How to package your function to a zip file and upload it
7
7
paragraph: Package function dependencies in a ZIP file for Scaleway Serverless Functions.
8
-
tags: functions zip-file
8
+
tags: functions zip-file handler package
9
9
dates:
10
-
validation: 2024-10-23
10
+
validation: 2025-02-04
11
11
posted: 2021-05-26
12
12
categories:
13
13
- serverless
@@ -30,6 +30,10 @@ There are [different methods to deploy functions](/serverless-functions/referenc
30
30
31
31
To match the Scaleway build pipelines requirements, functions zip files must contain the content of the root folder you want to publish.
32
32
33
+
<Messagetype="important">
34
+
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](#troubleshooting) section for more information.
35
+
</Message>
36
+
33
37
### How to create a zip file
34
38
35
39
Use the `zip` command to create an archive of your function and its dependencies:
@@ -369,3 +373,19 @@ Like that, you can create 3 functions with the same zip file simply by changing
369
373
370
374
You can also create a single Serverless Function with several handlers in the same zip file, and change the [handler](/serverless-functions/concepts/#handler) parameter according to your needs.
371
375
376
+
## Troubleshooting
377
+
378
+
If you encounter build errors after packaging your function, make sure that your archive is properly structured, as shown above. Compressing a folder from its parent directory will include this folder in the archive structure that can lead to build errors while deploying your function.
379
+
380
+
To avoid archive structure issue, make sure to zip your function from its root folder, and not the parent directory by using the following method:
381
+
382
+
```bash
383
+
cd folder_i_want_to_zip
384
+
zip -r ../function.zip .
385
+
```
386
+
387
+
You can then use the `unzip` command with the `-v` option to list the content of your archive to ensure it is properly structured:
A handler is a routine/function/method that processes specific events. Upon invoking your function, the handler is executed and returns an output.
18
+
19
+
To learn how to package a function and its dependencies in a zip file, refer to the [dedicated documentation](/serverless-functions/how-to/package-function-dependencies-in-zip).
20
+
17
21
## Handler syntax
18
22
19
23
<Tabsid="handlers">
@@ -27,6 +31,17 @@ categories:
27
31
28
32
- If the `handle` function is defined in a file named `handler.py` within a folder (for example, `myFunction/handler.py`): the handler will be `myFunction/handler.handle`.
29
33
34
+
#### Python handler example
35
+
36
+
```py
37
+
defhandle(event, context):
38
+
return {
39
+
"body": {
40
+
"message": 'Hello, world',
41
+
},
42
+
"statusCode": 200,
43
+
}
44
+
```
30
45
</TabsTab>
31
46
32
47
<TabsTablabel="Node">
@@ -39,6 +54,18 @@ categories:
39
54
40
55
- If the `handle` function is defined in a file named `handler.js` within a folder (for example, `myFunction/handler.js`): the handler will be `myFunction/handler.handle`.
41
56
57
+
#### Node handler example
58
+
59
+
```js
60
+
export {handle};
61
+
62
+
functionhandle (event, context, cb) {
63
+
return {
64
+
body:process.version,
65
+
statusCode:200,
66
+
};
67
+
};
68
+
```
42
69
</TabsTab>
43
70
44
71
<TabsTablabel="PHP">
@@ -51,33 +78,92 @@ categories:
51
78
52
79
- If the `handle` function is defined in a file named `handler.php` within a folder (for example, `myFunction/handler.php`): the handler will be `myFunction/handler.handle`.
53
80
81
+
#### PHP handler example
82
+
83
+
```php
84
+
<?php
85
+
function handle($event, $context) {
86
+
return [
87
+
"body" => phpversion(),
88
+
"statusCode" => 200,
89
+
];
90
+
}
91
+
```
54
92
</TabsTab>
55
93
56
94
<TabsTablabel="Go">
57
95
58
-
For Go, the handler must be written in the `<folder>/<function_name>` format:
59
-
60
-
- If the `Handle` function is defined in a `handler.go` file in a root folder: the handler name will be `Handle`.
61
-
62
-
- If the `Handle` function is defined in a `handler.go` file (for example, `myFunction/handler.go`): the handler will be `myFunction/Handle`
63
-
64
-
<Messagetype="note">
65
-
With Go, the method name must be capitalized.
66
-
</Message>
67
-
96
+
For Go, the handler must be written in the `<folder>/<function_name>` format:
97
+
98
+
- If the `Handle` function is defined in a `handler.go` file in a root folder: the handler name will be `Handle`.
99
+
100
+
- If the `Handle` function is defined in a `handler.go` file (for example, `myFunction/handler.go`): the handler will be `myFunction/Handle`
101
+
102
+
<Messagetype="note">
103
+
With Go, the method name must be capitalized.
104
+
</Message>
105
+
106
+
#### Go handler example
107
+
108
+
```go
109
+
package myfunc
110
+
111
+
import (
112
+
"encoding/json"
113
+
"net/http"
114
+
)
115
+
116
+
// Handle - Handle event
117
+
funcHandle(whttp.ResponseWriter, r *http.Request) {
118
+
response:=map[string]interface{}{
119
+
"message": "We're all good",
120
+
}
121
+
122
+
responseBytes, err:= json.Marshal(response)
123
+
if err != nil {
124
+
w.WriteHeader(http.StatusInternalServerError)
125
+
return
126
+
}
127
+
128
+
// Set the header explicitly depending the returned data
0 commit comments