0
# Lambda Functions
1
2
Creation and management of serverless Lambda functions with ZIP packaging, configuration options, and deployment utilities for the Vercel platform.
3
4
## Capabilities
5
6
### Lambda Class
7
8
Represents a serverless Lambda function with all necessary deployment information.
9
10
```typescript { .api }
11
/**
12
* Represents a serverless Lambda function
13
*/
14
class Lambda {
15
type: 'Lambda';
16
zipBuffer: Buffer;
17
handler: string;
18
runtime: string;
19
memory?: number;
20
maxDuration?: number;
21
environment: Environment;
22
23
constructor(options: LambdaOptions);
24
}
25
26
interface LambdaOptions {
27
zipBuffer: Buffer;
28
handler: string;
29
runtime: string;
30
memory?: number;
31
maxDuration?: number;
32
environment: Environment;
33
}
34
35
interface Environment {
36
[key: string]: string;
37
}
38
```
39
40
**Usage Examples:**
41
42
```typescript
43
import { Lambda, createLambda } from "@vercel/build-utils";
44
45
// Create Lambda directly (advanced usage - typically use createLambda instead)
46
// Note: zipBuffer must be created externally as createZip is not exported
47
const zipBuffer = /* externally created ZIP buffer */;
48
const lambda = new Lambda({
49
zipBuffer,
50
handler: "index.handler",
51
runtime: "nodejs18.x",
52
memory: 512,
53
maxDuration: 30,
54
environment: {
55
NODE_ENV: "production",
56
API_KEY: "secret"
57
}
58
});
59
60
// Recommended approach: use createLambda
61
const lambda = await createLambda({
62
files,
63
handler: "index.handler",
64
runtime: "nodejs18.x",
65
memory: 512,
66
maxDuration: 30,
67
environment: {
68
NODE_ENV: "production",
69
API_KEY: "secret"
70
}
71
});
72
```
73
74
### createLambda
75
76
Creates a Lambda instance from files with automatic ZIP packaging.
77
78
```typescript { .api }
79
/**
80
* Creates a Lambda instance from files
81
* @param options - Lambda creation options
82
* @returns Promise resolving to Lambda instance
83
*/
84
function createLambda(options: CreateLambdaOptions): Promise<Lambda>;
85
86
interface CreateLambdaOptions {
87
files: Files;
88
handler: string;
89
runtime: string;
90
memory?: number;
91
maxDuration?: number;
92
environment?: Environment;
93
}
94
```
95
96
**Usage Examples:**
97
98
```typescript
99
import { createLambda, FileBlob, FileFsRef } from "@vercel/build-utils";
100
101
// Create Lambda from in-memory files
102
const files = {
103
"index.js": new FileBlob({
104
data: `
105
exports.handler = async (event) => {
106
return {
107
statusCode: 200,
108
body: JSON.stringify({ message: "Hello World" })
109
};
110
};
111
`
112
}),
113
"package.json": new FileBlob({
114
data: JSON.stringify({
115
name: "my-lambda",
116
main: "index.js"
117
})
118
})
119
};
120
121
const lambda = await createLambda({
122
files,
123
handler: "index.handler",
124
runtime: "nodejs18.x",
125
memory: 256,
126
maxDuration: 10,
127
environment: {
128
NODE_ENV: "production"
129
}
130
});
131
132
// Create Lambda from filesystem files
133
const fsFiles = {
134
"index.js": await FileFsRef.fromFsPath({ fsPath: "/src/handler.js" }),
135
"lib/utils.js": await FileFsRef.fromFsPath({ fsPath: "/src/lib/utils.js" })
136
};
137
138
const fsLambda = await createLambda({
139
files: fsFiles,
140
handler: "index.handler",
141
runtime: "nodejs18.x"
142
});
143
```
144
145
146
### getLambdaOptionsFromFunction
147
148
Extracts Lambda configuration options from build configuration.
149
150
```typescript { .api }
151
/**
152
* Extracts Lambda options from configuration
153
* @param options - Function configuration options
154
* @returns Promise resolving to Lambda options
155
*/
156
function getLambdaOptionsFromFunction(options: GetLambdaOptionsFromFunctionOptions): Promise<Pick<LambdaOptions, 'memory' | 'maxDuration'>>;
157
158
interface GetLambdaOptionsFromFunctionOptions {
159
sourceFile: string;
160
config?: Config;
161
}
162
```
163
164
**Usage Examples:**
165
166
```typescript
167
import { getLambdaOptionsFromFunction } from "@vercel/build-utils";
168
169
// Extract options from configuration
170
const config = {
171
functions: {
172
"api/handler.js": {
173
memory: 512,
174
maxDuration: 30
175
},
176
"api/*.js": {
177
memory: 256,
178
maxDuration: 10
179
}
180
}
181
};
182
183
const options = await getLambdaOptionsFromFunction({
184
sourceFile: "api/handler.js",
185
config
186
});
187
// Returns: { memory: 512, maxDuration: 30 }
188
189
// Pattern matching
190
const patternOptions = await getLambdaOptionsFromFunction({
191
sourceFile: "api/users.js",
192
config
193
});
194
// Returns: { memory: 256, maxDuration: 10 }
195
```
196
197
### Prerender Class
198
199
Represents prerender configuration for static generation with Lambda fallback.
200
201
```typescript { .api }
202
/**
203
* Represents prerender configuration for static generation
204
*/
205
class Prerender {
206
type: 'Prerender';
207
expiration: number | false;
208
lambda: Lambda;
209
fallback: FileBlob | FileFsRef | FileRef | null;
210
group?: number;
211
bypassToken: string | null;
212
213
constructor(options: PrerenderOptions);
214
}
215
216
interface PrerenderOptions {
217
expiration: number | false;
218
lambda: Lambda;
219
fallback: FileBlob | FileFsRef | FileRef | null;
220
group?: number;
221
bypassToken?: string | null;
222
}
223
```
224
225
**Usage Examples:**
226
227
```typescript
228
import { Prerender, createLambda, FileBlob } from "@vercel/build-utils";
229
230
// Create Lambda for prerendering
231
const lambda = await createLambda({
232
files: { "index.js": new FileBlob({ data: "// SSG lambda code" }) },
233
handler: "index.handler",
234
runtime: "nodejs18.x"
235
});
236
237
// Create fallback page
238
const fallback = new FileBlob({
239
data: "<html><body>Loading...</body></html>",
240
contentType: "text/html"
241
});
242
243
// Create prerender configuration
244
const prerender = new Prerender({
245
expiration: 3600, // 1 hour cache
246
lambda,
247
fallback,
248
group: 1,
249
bypassToken: "bypass-token-must-be-32-chars-long"
250
});
251
252
// No expiration (static forever)
253
const staticPrerender = new Prerender({
254
expiration: false,
255
lambda,
256
fallback: null
257
});
258
```
259
260
## Runtime Support
261
262
Supported runtimes for Lambda functions:
263
264
- `nodejs18.x` - Node.js 18.x
265
- `nodejs16.x` - Node.js 16.x (deprecated)
266
- `nodejs14.x` - Node.js 14.x (deprecated)
267
- `python3.9` - Python 3.9
268
- `python3.8` - Python 3.8
269
- `go1.x` - Go 1.x
270
271
## Memory and Duration Limits
272
273
- **Memory**: 128MB to 3008MB (in 64MB increments)
274
- **Max Duration**: 1 to 900 seconds
275
- **Default Memory**: 1024MB if not specified
276
- **Default Duration**: 25 seconds if not specified
277
278
## Environment Variables
279
280
Lambda functions support environment variables for configuration:
281
282
```typescript
283
const lambda = await createLambda({
284
files,
285
handler: "index.handler",
286
runtime: "nodejs18.x",
287
environment: {
288
NODE_ENV: "production",
289
DATABASE_URL: "postgresql://...",
290
API_SECRET: "secret-key",
291
REGION: "us-east-1"
292
}
293
});
294
```