@@ -22,14 +22,14 @@ class NextStandaloneStack extends Stack {
22
22
super ( scope , id , props )
23
23
24
24
const config = {
25
+ apigwServerPath : '/_server' ,
26
+ apigwImagePath : '/_image' ,
25
27
assetsZipPath : path . resolve ( commandCwd , './next.out/assetsLayer.zip' ) ,
26
28
codeZipPath : path . resolve ( commandCwd , './next.out/code.zip' ) ,
27
29
dependenciesZipPath : path . resolve ( commandCwd , './next.out/dependenciesLayer.zip' ) ,
28
- sharpLayerZipPath : path . resolve ( cdkFolder , '../dist/sharp-layer.zip' ) ,
29
- nextLayerZipPath : path . resolve ( cdkFolder , '../dist/next-layer.zip' ) ,
30
30
imageHandlerZipPath : path . resolve ( cdkFolder , '../dist/image-handler.zip' ) ,
31
31
customServerHandler : 'handler.handler' ,
32
- customImageHandler : 'index .handler' ,
32
+ customImageHandler : 'handler .handler' ,
33
33
cfnViewerCertificate : undefined ,
34
34
...props ,
35
35
}
@@ -42,28 +42,19 @@ class NextStandaloneStack extends Stack {
42
42
description : `${ depsPrefix } -deps` ,
43
43
} )
44
44
45
- // @TODO : Load Sharp version from source package.json so we respect it.
46
- const sharpLayer = new LayerVersion ( this , 'SharpLayer' , {
47
- code : Code . fromAsset ( config . sharpLayerZipPath , { assetHashType : AssetHashType . CUSTOM , assetHash : depsPrefix } ) ,
48
- description : `${ depsPrefix } -sharp` ,
49
- } )
50
-
51
- // @TODO : Load Next version from source package.json so we respect it.
52
- const nextLayer = new LayerVersion ( this , 'NextLayer' , {
53
- code : Code . fromAsset ( config . nextLayerZipPath , { assetHashType : AssetHashType . CUSTOM , assetHash : depsPrefix } ) ,
54
- description : `${ depsPrefix } -next` ,
55
- } )
56
-
57
45
const serverLambda = new Function ( this , 'DefaultNextJs' , {
58
46
code : Code . fromAsset ( config . codeZipPath , {
59
47
followSymlinks : SymlinkFollowMode . NEVER ,
60
48
} ) ,
61
49
runtime : Runtime . NODEJS_16_X ,
62
50
handler : config . customServerHandler ,
63
- layers : [ depsLayer , sharpLayer , nextLayer ] ,
51
+ layers : [ depsLayer ] ,
64
52
// No need for big memory as image handling is done elsewhere.
65
53
memorySize : 512 ,
66
54
timeout : Duration . seconds ( 15 ) ,
55
+ environment : {
56
+ NEXTJS_LAMBDA_BASE_PATH : config . apigwServerPath ,
57
+ } ,
67
58
} )
68
59
69
60
const assetsBucket = new Bucket ( this , 'NextAssetsBucket' , {
@@ -77,7 +68,6 @@ class NextStandaloneStack extends Stack {
77
68
code : Code . fromAsset ( config . imageHandlerZipPath ) ,
78
69
runtime : Runtime . NODEJS_16_X ,
79
70
handler : config . customImageHandler ,
80
- layers : [ sharpLayer , nextLayer ] ,
81
71
memorySize : 1024 ,
82
72
timeout : Duration . seconds ( 10 ) ,
83
73
environment : {
@@ -87,15 +77,12 @@ class NextStandaloneStack extends Stack {
87
77
88
78
assetsBucket . grantRead ( imageLambda )
89
79
90
- const serverApigatewayProxy = new HttpApi ( this , 'ServerProxy' , {
91
- createDefaultStage : true ,
92
- defaultIntegration : new HttpLambdaIntegration ( 'LambdaApigwIntegration' , serverLambda ) ,
93
- } )
80
+ const apigatewayProxy = new HttpApi ( this , 'ServerProxy' )
94
81
95
- const imageApigatewayProxy = new HttpApi ( this , 'ImagesProxy' , {
96
- createDefaultStage : true ,
97
- defaultIntegration : new HttpLambdaIntegration ( 'ImagesApigwIntegration ' , imageLambda ) ,
98
- } )
82
+ // We could do parameter mapping here and remove prefix from path.
83
+ // However passing env var (basePath) is easier to use, understand and integrate to other solutions.
84
+ apigatewayProxy . addRoutes ( { path : ` ${ config . apigwServerPath } /{proxy+}` , integration : new HttpLambdaIntegration ( 'LambdaApigwIntegration ' , serverLambda ) } )
85
+ apigatewayProxy . addRoutes ( { path : ` ${ config . apigwImagePath } /{proxy+}` , integration : new HttpLambdaIntegration ( 'ImagesApigwIntegration' , imageLambda ) } )
99
86
100
87
const s3AssetsIdentity = new OriginAccessIdentity ( this , 'OAICfnDistroS3' , {
101
88
comment : 'Allows CloudFront to access S3 bucket with assets' ,
@@ -123,7 +110,8 @@ class NextStandaloneStack extends Stack {
123
110
} ,
124
111
] ,
125
112
customOriginSource : {
126
- domainName : `${ serverApigatewayProxy . apiId } .execute-api.${ this . region } .amazonaws.com` ,
113
+ originPath : config . apigwServerPath ,
114
+ domainName : `${ apigatewayProxy . apiId } .execute-api.${ this . region } .amazonaws.com` ,
127
115
} ,
128
116
} ,
129
117
{
@@ -137,7 +125,8 @@ class NextStandaloneStack extends Stack {
137
125
} ,
138
126
] ,
139
127
customOriginSource : {
140
- domainName : `${ imageApigatewayProxy . apiId } .execute-api.${ this . region } .amazonaws.com` ,
128
+ originPath : config . apigwImagePath ,
129
+ domainName : `${ apigatewayProxy . apiId } .execute-api.${ this . region } .amazonaws.com` ,
141
130
} ,
142
131
} ,
143
132
{
@@ -171,8 +160,7 @@ class NextStandaloneStack extends Stack {
171
160
172
161
new CfnOutput ( this , 'cfnDistroUrl' , { value : cfnDistro . distributionDomainName } )
173
162
new CfnOutput ( this , 'cfnDistroId' , { value : cfnDistro . distributionId } )
174
- new CfnOutput ( this , 'defaultApiGwUrl' , { value : serverApigatewayProxy . apiEndpoint } )
175
- new CfnOutput ( this , 'imagesApiGwUrl' , { value : imageApigatewayProxy . apiEndpoint } )
163
+ new CfnOutput ( this , 'apiGwUrl' , { value : apigatewayProxy . apiEndpoint } )
176
164
new CfnOutput ( this , 'assetsBucketUrl' , { value : assetsBucket . bucketDomainName } )
177
165
}
178
166
}
0 commit comments