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: README.md
+53Lines changed: 53 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,6 +36,7 @@ OpenNext aims to support all Next.js 13 features. Some features are work in prog
36
36
-[x] Image optimization
37
37
-[x][NextAuth.js](https://next-auth.js.org)
38
38
-[x][Running at edge](#running-at-edge)
39
+
-[x][No cold start](#warmer-function)
39
40
40
41
## How does OpenNext work?
41
42
@@ -55,6 +56,7 @@ my-next-app/
55
56
assets/ -> Static files to upload to an S3 Bucket
56
57
server-function/ -> Handler code for server Lambda Function
57
58
image-optimization-function/ -> Handler code for image optimization Lambda Function
59
+
warmer-function/ -> Cron job code to keep server functionwarm
58
60
```
59
61
60
62
## Deployment
@@ -210,6 +212,57 @@ To configure the CloudFront distribution:
210
212
|`/api/*`| API | set `x-forwarded-host`<br />[see why](#workaround-set-x-forwarded-host-header-aws-specific)| server function | - |
211
213
|`/*`| catch all | set `x-forwarded-host`<br />[see why](#workaround-set-x-forwarded-host-header-aws-specific)| server function | S3 bucket<br />[see why](#workaround-public-static-files-served-out-by-server-function-aws-specific)|
212
214
215
+
#### Warmer function
216
+
217
+
Server functions may experience performance issues due to Lambda cold starts. To mitigate this, the server function can be invoked periodically. Remmember, **Warming is optional** and is only required if you want to keep the server function warm.
218
+
219
+
To set this up, create a Lambda function using the code in the `.open-next/warmer-function` folder with `index.mjs` as the handler. Ensure the function is configured as follows:
220
+
221
+
- Set the `FUNCTION_NAME` environment variable with the value being the name of the server Lambda function.
222
+
- Set the `CONCURRENCY` environment variable with the value being the number of server functions to warm.
223
+
- Grant `lambda:InvokeFunction` permission to allow the warmer to invoke the server function.
224
+
225
+
Also, create an EventBridge scheduled rule to invoke the warmer function every 5 minutes.
226
+
227
+
Please note, warming is currently only supported when the server function is deployed to a single region (Lambda).
228
+
229
+
**Prewarm**
230
+
231
+
Each time you deploy, a new version of the Lambda function will be generated. All warmed server function instances will be turned off. And there won't be any warm instances until the warmer function runs again at the next 5-minute interval.
232
+
233
+
To ensure the functions are prewarmed on deploy, create a [CloudFormation Custom Resource](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html) to invoke the warmer function on deployment. The custom resource should be configured as follows:
234
+
235
+
- Invoke the warmer function on resource `Create` and `Update`.
236
+
- Include a timestamp value in the resource property to ensure the custom resource runs on every deployment.
237
+
- Grant `lambda:InvokeFunction` permission to allow the custom resource to invoke the warmer function.
238
+
239
+
**Cost**
240
+
241
+
There are three components to the cost:
242
+
243
+
1. EventBridge scheduler: $0.00864
244
+
```
245
+
Requests cost — 8,640 invocations per month x $1/million = $0.00864
246
+
```
247
+
1. Warmer function: $0.145728288
248
+
```
249
+
Requests cost — 8,640 invocations per month x $0.2/million = $0.001728
250
+
Duration cost — 8,640 invocations per month x 1GB memory x 1s duration x $0.0000166667/GB-second = $0.144000288
251
+
```
252
+
1. Server function: $0.0161280288 per warmed instance
253
+
```
254
+
Requests cost — 8,640 invocations per month x $0.2/million = $0.001728
255
+
Duration cost — 8,640 invocations per month x 1GB memory x 100ms duration x $0.0000166667/GB-second = $0.0144000288
256
+
```
257
+
258
+
For example, keeping 50 instances of the server function warm will cost approximately **$0.96 per month**
259
+
260
+
```
261
+
$0.00864 + $0.145728288 + $0.0161280288 x 50 = $0.960769728
262
+
```
263
+
264
+
This cost estimate is based on the `us-east-1` region pricing and does not consider any free tier benefits.
265
+
213
266
## Limitations and workarounds
214
267
215
268
#### WORKAROUND: `public/` static files served by the server function (AWS specific)
0 commit comments