@@ -46,9 +46,11 @@ export class CdnStack extends NestedStack {
46
46
apiCachePolicy : cloudfront . CachePolicy | undefined
47
47
vanityUrl ! : string
48
48
apiVanityUrl ! : string
49
+ props : ResourceNestedStackProps
49
50
50
51
constructor ( scope : Construct , props : ResourceNestedStackProps ) {
51
52
super ( scope , 'Cdn' , props )
53
+ this . props = props
52
54
// ...
53
55
const originAccessIdentity = new cloudfront . OriginAccessIdentity ( this , 'OAI' )
54
56
@@ -65,7 +67,7 @@ export class CdnStack extends NestedStack {
65
67
66
68
// Fetch the timestamp from SSM Parameter Store
67
69
const timestampParam = ssm . StringParameter . fromSecureStringParameterAttributes ( this , 'TimestampParam' , {
68
- parameterName : `/${ props . appName . toLowerCase ( ) } /timestamp` ,
70
+ parameterName : `/${ this . props . appName . toLowerCase ( ) } /timestamp` ,
69
71
version : 1 ,
70
72
} )
71
73
@@ -75,14 +77,14 @@ export class CdnStack extends NestedStack {
75
77
if ( ! timestamp ) {
76
78
timestamp = new Date ( ) . getTime ( ) . toString ( )
77
79
new ssm . StringParameter ( this , 'TimestampParam' , {
78
- parameterName : `/${ props . appName . toLowerCase ( ) } /timestamp` ,
80
+ parameterName : `/${ this . props . appName . toLowerCase ( ) } /timestamp` ,
79
81
stringValue : timestamp ,
80
82
} )
81
83
}
82
84
83
85
new route53 . ARecord ( this , 'AliasRecord' , {
84
- recordName : props . domain ,
85
- zone : props . zone ,
86
+ recordName : this . props . domain ,
87
+ zone : this . props . zone ,
86
88
target : route53 . RecordTarget . fromAlias ( new targets . CloudFrontTarget ( this . cdn ) ) ,
87
89
} )
88
90
@@ -92,7 +94,7 @@ export class CdnStack extends NestedStack {
92
94
// this needs to have partialAppKey & timestamp to ensure it is unique, because there is a chance that during testing, you deploy
93
95
// the same app many times using the same app key. Since Origin Request (Lambda@Edge) functions are replicated functions, the
94
96
// deletion process takes a long time. This is to ensure that the function is always unique in cases of quick recreations.
95
- functionName : `${ props . appName } -${ props . appEnv } -origin-request-${ props . partialAppKey } -${ timestamp } ` ,
97
+ functionName : `${ this . props . appName } -${ this . props . appEnv } -origin-request-${ this . props . partialAppKey } -${ timestamp } ` ,
96
98
description : 'The Stacks Origin Request function that prettifies URLs' ,
97
99
runtime : lambda . Runtime . NODEJS_18_X ,
98
100
handler : 'dist/origin-request.handler' ,
@@ -110,21 +112,21 @@ export class CdnStack extends NestedStack {
110
112
cfnOriginRequestFunction . applyRemovalPolicy ( RemovalPolicy . RETAIN )
111
113
112
114
const cdn = new cloudfront . Distribution ( this , 'Distribution' , {
113
- domainNames : [ props . domain ] ,
115
+ domainNames : [ this . props . domain ] ,
114
116
defaultRootObject : 'index.html' ,
115
117
comment : `CDN for ${ config . app . url } ` ,
116
- certificate : props . certificate ,
118
+ certificate : this . props . certificate ,
117
119
enableLogging : true ,
118
- logBucket : props . logBucket ,
120
+ logBucket : this . props . logBucket ,
119
121
httpVersion : cloudfront . HttpVersion . HTTP2_AND_3 ,
120
122
priceClass : cloudfront . PriceClass . PRICE_CLASS_ALL ,
121
123
enabled : true ,
122
124
minimumProtocolVersion : cloudfront . SecurityPolicyProtocol . TLS_V1_2_2021 ,
123
- webAclId : props . firewall . attrArn ,
125
+ webAclId : this . props . firewall . attrArn ,
124
126
enableIpv6 : true ,
125
127
126
128
defaultBehavior : {
127
- origin : new origins . S3Origin ( props . storage . publicBucket , {
129
+ origin : new origins . S3Origin ( this . props . storage . publicBucket , {
128
130
originAccessIdentity : this . originAccessIdentity ,
129
131
} ) ,
130
132
edgeLambdas : [
@@ -156,9 +158,9 @@ export class CdnStack extends NestedStack {
156
158
// setup the www redirect
157
159
// Create a bucket for www.yourdomain.com and configure it to redirect to yourdomain.com
158
160
const wwwBucket = new s3 . Bucket ( this , 'WwwBucket' , {
159
- bucketName : `www.${ props . domain } ` ,
161
+ bucketName : `www.${ this . props . domain } ` ,
160
162
websiteRedirect : {
161
- hostName : props . domain ,
163
+ hostName : this . props . domain ,
162
164
protocol : s3 . RedirectProtocol . HTTPS ,
163
165
} ,
164
166
removalPolicy : RemovalPolicy . DESTROY ,
@@ -167,8 +169,8 @@ export class CdnStack extends NestedStack {
167
169
168
170
// Create a Route53 record for www.yourdomain.com
169
171
new route53 . ARecord ( this , 'WwwAliasRecord' , {
170
- recordName : `www.${ props . domain } ` ,
171
- zone : props . zone ,
172
+ recordName : `www.${ this . props . domain } ` ,
173
+ zone : this . props . zone ,
172
174
target : route53 . RecordTarget . fromAlias ( new targets . BucketWebsiteTarget ( wwwBucket ) ) ,
173
175
} )
174
176
@@ -251,57 +253,21 @@ export class CdnStack extends NestedStack {
251
253
}
252
254
253
255
deployApi ( ) {
254
- const layer = new lambda . LayerVersion ( this , 'BunLambdaLayer' , {
255
- code : lambda . Code . fromAsset ( p . projectStoragePath ( 'framework/cloud/bun-lambda-layer.zip' ) ) ,
256
- compatibleRuntimes : [ lambda . Runtime . PROVIDED_AL2 ] ,
257
- compatibleArchitectures : [ lambda . Architecture . ARM_64 ] ,
258
- license : 'MIT' ,
259
- description : 'Bun is an incredibly fast JavaScript runtime, bundler, transpiler, and package manager.' ,
260
- } )
261
-
262
256
const keysToRemove = [ '_HANDLER' , '_X_AMZN_TRACE_ID' , 'AWS_REGION' , 'AWS_EXECUTION_ENV' , 'AWS_LAMBDA_FUNCTION_NAME' , 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE' , 'AWS_LAMBDA_FUNCTION_VERSION' , 'AWS_LAMBDA_INITIALIZATION_TYPE' , 'AWS_LAMBDA_LOG_GROUP_NAME' , 'AWS_LAMBDA_LOG_STREAM_NAME' , 'AWS_ACCESS_KEY' , 'AWS_ACCESS_KEY_ID' , 'AWS_SECRET_ACCESS_KEY' , 'AWS_SESSION_TOKEN' , 'AWS_LAMBDA_RUNTIME_API' , 'LAMBDA_TASK_ROOT' , 'LAMBDA_RUNTIME_DIR' , '_' ]
263
257
keysToRemove . forEach ( key => delete env [ key as EnvKey ] )
264
258
265
259
const secrets = new secretsmanager . Secret ( this , 'StacksSecrets' , {
266
- secretName : `${ props . appName } -${ props . appEnv } -secrets` ,
260
+ secretName : `${ this . props . appName } -${ this . props . appEnv } -secrets` ,
267
261
description : 'Secrets for the Stacks application' ,
268
262
generateSecretString : {
269
263
secretStringTemplate : JSON . stringify ( env ) ,
270
264
generateStringKey : Object . keys ( env ) . join ( ',' ) . length . toString ( ) ,
271
265
} ,
272
266
} )
273
267
274
- const functionName = `${ props . appName } -${ props . appEnv } -server`
275
- const serverFunction = new lambda . Function ( this , 'StacksServer' , {
276
- functionName,
277
- description : 'The Stacks Server' ,
278
- memorySize : 512 ,
279
- vpc : props . vpc ,
280
- filesystem : lambda . FileSystem . fromEfsAccessPoint ( props . storage . accessPoint ! , '/mnt/efs' ) ,
281
- timeout : Duration . seconds ( 30 ) ,
282
- tracing : lambda . Tracing . ACTIVE ,
283
- code : lambda . Code . fromAsset ( p . projectStoragePath ( 'framework/cloud/api.zip' ) , {
284
- assetHash : this . node . tryGetContext ( 'serverFunctionCodeHash' ) ,
285
- assetHashType : AssetHashType . CUSTOM ,
286
- } ) ,
287
- handler : 'server.fetch' ,
288
- runtime : lambda . Runtime . PROVIDED_AL2 ,
289
- architecture : lambda . Architecture . ARM_64 ,
290
- layers : [ layer ] ,
291
- } )
292
-
293
- secrets . grantRead ( serverFunction )
294
- serverFunction . addEnvironment ( 'SECRETS_ARN' , secrets . secretArn )
295
-
296
- const api = new lambda . FunctionUrl ( this , 'StacksServerUrl' , {
297
- function : serverFunction ,
298
- authType : lambda . FunctionUrlAuthType . NONE , // becomes a public API
299
- cors : {
300
- allowedOrigins : [ '*' ] ,
301
- } ,
302
- } )
268
+ const functionName = `${ this . props . appName } -${ this . props . appEnv } -server`
303
269
304
- this . apiVanityUrl = api . url
270
+ // this.apiVanityUrl = api.url
305
271
}
306
272
307
273
apiBehaviorOptions ( ) : Record < string , cloudfront . BehaviorOptions > {
@@ -333,7 +299,7 @@ export class CdnStack extends NestedStack {
333
299
docsBehaviorOptions ( ) : Record < string , cloudfront . BehaviorOptions > {
334
300
return {
335
301
'/docs' : {
336
- origin : new origins . S3Origin ( props . storage . publicBucket , {
302
+ origin : new origins . S3Origin ( this . props . storage . publicBucket , {
337
303
originAccessIdentity : this . originAccessIdentity ,
338
304
originPath : '/docs' ,
339
305
} ) ,
@@ -344,7 +310,7 @@ export class CdnStack extends NestedStack {
344
310
cachePolicy : cloudfront . CachePolicy . CACHING_OPTIMIZED ,
345
311
} ,
346
312
'/docs/*' : {
347
- origin : new origins . S3Origin ( props . storage . publicBucket , {
313
+ origin : new origins . S3Origin ( this . props . storage . publicBucket , {
348
314
originAccessIdentity : this . originAccessIdentity ,
349
315
originPath : '/docs' ,
350
316
} ) ,
@@ -388,7 +354,7 @@ export class CdnStack extends NestedStack {
388
354
389
355
this . apiCachePolicy = new cloudfront . CachePolicy ( this , 'ApiCachePolicy' , {
390
356
comment : 'Stacks API Cache Policy' ,
391
- cachePolicyName : `${ props . appName } -${ props . appEnv } -api-cache-policy` ,
357
+ cachePolicyName : `${ this . props . appName } -${ this . props . appEnv } -api-cache-policy` ,
392
358
// minTtl: config.cloud.cdn?.minTtl ? Duration.seconds(config.cloud.cdn.minTtl) : undefined,
393
359
defaultTtl : Duration . seconds ( 0 ) ,
394
360
cookieBehavior : cloudfront . CacheCookieBehavior . none ( ) ,
0 commit comments