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
{{ message }}
This repository was archived by the owner on Feb 1, 2025. It is now read-only.
* refactor: nextjs packaging, use npm packages to bundle instead of shell script, parameters added
* refactor(app): bundling process changed, simplified, CDK construct added
* ref(build): leaned into rollup, zipping done as part of rollup process
Co-authored-by: Jan Soukup <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
const imageOptimizerFn = new Function(this, 'LambdaFunction', {
80
80
code: Code.fromAsset(imageHandlerZip),
81
81
runtime: Runtime.NODEJS_16_X,
82
-
handler: 'index.handler',
82
+
handler: 'image-handler.handler',
83
83
layers: [sharpLayer],
84
84
memorySize: 1024,
85
85
timeout: Duration.seconds(15),
@@ -93,149 +93,13 @@ Lambda consumes Api Gateway requests, so we need to create ApiGw proxy (v2) that
93
93
94
94
Lambda is designed to serve `_next/image*` route in NextJS stack and replaces the default handler so we can optimize caching and memory limits for page renders and image optimization.
95
95
96
-
### CDK
97
-
98
-
Here is complete example using CDK:
99
-
100
-
```
101
-
import { HttpApi } from '@aws-cdk/aws-apigatewayv2-alpha'
102
-
import { HttpLambdaIntegration } from '@aws-cdk/aws-apigatewayv2-integrations-alpha'
// No need for big memory as image handling is done elsewhere.
137
-
memorySize: 512,
138
-
timeout: Duration.seconds(15),
139
-
})
140
-
141
-
const imageOptimizationFn = new Function(this, 'ImageOptimizationNextJs', {
142
-
code: Code.fromAsset(imageHandlerZip),
143
-
runtime: Runtime.NODEJS_16_X,
144
-
handler: 'index.handler',
145
-
layers: [sharpLayer],
146
-
memorySize: 1024,
147
-
timeout: Duration.seconds(10),
148
-
environment: {
149
-
S3_SOURCE_BUCKET: assetsBucket.bucketName,
150
-
},
151
-
})
152
-
153
-
assetsBucket.grantRead(imageOptimizationFn)
96
+
### Via CDK
154
97
155
-
const apiGwDefault = new HttpApi(this, 'NextJsLambdaProxy', {
156
-
createDefaultStage: true,
157
-
defaultIntegration: new HttpLambdaIntegration('LambdaApigwIntegration', lambdaFn),
158
-
})
98
+
See `NextStandaloneLambda` construct in `lib/construct.ts`.
99
+
This is a complete definition which consumes output of `pack` CLI command. It requires 3 zips (assets, dependencies and code) and exposes get methods for all resources to provide a way to set custom routes, set environment variables, etc.
159
100
160
-
const apiGwImages = new HttpApi(this, 'ImagesLambdaProxy', {
161
-
createDefaultStage: true,
162
-
defaultIntegration: new HttpLambdaIntegration('ImagesApigwIntegration', imageOptimizationFn),
163
-
})
164
-
165
-
const cfnDistro = new CloudFrontWebDistribution(this, 'TestApigwDistro', {
166
-
// Must be set, because cloufront would use index.html which would not match in NextJS routes.
new CfnOutput(this, 'cfnDistroUrl', { value: cfnDistro.distributionDomainName })
231
-
new CfnOutput(this, 'defaultApiGwUrl', { value: apiGwDefault.apiEndpoint })
232
-
new CfnOutput(this, 'imagesApiGwUrl', { value: apiGwImages.apiEndpoint })
233
-
new CfnOutput(this, 'assetsBucketUrl', { value: assetsBucket.bucketDomainName })
234
-
}
235
-
}
236
-
```
237
101
238
-
## Sharp
102
+
###Sharp
239
103
240
104
Besides handler (wrapper) itself, underlying NextJS also requires sharp binaries.
241
105
To build those, we use `npm install` with some extra parametes. Then we zip all sharp dependencies and compress it to easily importable zip file.
@@ -258,7 +122,7 @@ To package everything, make sure to be in your project root folder and next fold
258
122
259
123
It will create `next.out/` folder with 3 zip packages. One zip Lambda's code, one is dependencies layer and one is assets layer.
260
124
261
-
- code zip: include all files generated by next that are required to run on Lambda behind ApiGateway. Original handler as well as new server handler are included. Use `handler.handler` for custom one or `server.handler` for original one.
125
+
- code zip: include all files generated by next that are required to run on Lambda behind ApiGateway. Original handler as well as new server handler are included. Use `server-handler.handler` for custom one or `server.handler` for original one.
262
126
- dependencies layer: all transpilied `node_modules`. Next includes only used files, dramatically reducing overall size.
263
127
- assets layer: your public folder together with generated assets. Keep in mind that to public refer file, you need to include it in `public/assets/` folder, not just in public. This limitation dramatically simplifies whole setup. This zip file is uploaded to S3, it's not included in Lambda code.
0 commit comments