@@ -30,9 +30,14 @@ export class CdnStack extends NestedStack {
30
30
certificateArn : string
31
31
certificate : acm . ICertificate
32
32
logBucket : s3 . IBucket
33
+ publicBucket : s3 . IBucket
34
+ firewallArn : string
35
+ apiCachePolicy : cloudfront . CachePolicy | undefined
36
+ props : NestedCloudProps
33
37
34
38
constructor ( scope : Construct , props : NestedCloudProps ) {
35
39
super ( scope , 'Cdn' , props )
40
+ this . props = props
36
41
37
42
// do lookups
38
43
this . zone = route53 . PublicHostedZone . fromLookup ( this , 'AppUrlHostedZone' , {
@@ -41,11 +46,18 @@ export class CdnStack extends NestedStack {
41
46
this . certificateArn = Stack . of ( this ) . formatArn ( {
42
47
service : 'acm' ,
43
48
resource : 'certificate' ,
44
- resourceName : 'Certificate' , // replace with your actual certificate logical ID
49
+ resourceName : 'Certificate' ,
45
50
} )
46
51
this . certificate = acm . Certificate . fromCertificateArn ( this , 'Certificate' , this . certificateArn )
52
+ this . firewallArn = Stack . of ( this ) . formatArn ( {
53
+ service : 'wafv2' ,
54
+ resource : 'webacl' ,
55
+ resourceName : 'WebFirewall' ,
56
+ } )
57
+
47
58
const bucketPrefix = `${ props . appName } -${ props . appEnv } `
48
59
this . logBucket = s3 . Bucket . fromBucketName ( this , 'LogBucket' , `${ bucketPrefix } -logs-${ props . partialAppKey } ` )
60
+ this . publicBucket = s3 . Bucket . fromBucketName ( this , 'PublicBucket' , `${ bucketPrefix } -${ props . partialAppKey } ` )
49
61
50
62
// proceed with cdn logic
51
63
this . originAccessIdentity = new cloudfront . OriginAccessIdentity ( this , 'OAI' )
@@ -99,22 +111,23 @@ export class CdnStack extends NestedStack {
99
111
const cfnOriginRequestFunction = originRequestFunction . node . defaultChild as CfnResource
100
112
cfnOriginRequestFunction . applyRemovalPolicy ( RemovalPolicy . RETAIN )
101
113
102
- const cdn = new cloudfront . Distribution ( this , 'Distribution' , {
114
+ // the actual CDN distribution
115
+ new cloudfront . Distribution ( this , 'Distribution' , {
103
116
domainNames : [ props . domain ] ,
104
117
defaultRootObject : 'index.html' ,
105
118
comment : `CDN for ${ config . app . url } ` ,
106
- certificate,
119
+ certificate : this . certificate ,
107
120
enableLogging : true ,
108
- logBucket : props . logBucket ,
121
+ logBucket : this . logBucket ,
109
122
httpVersion : cloudfront . HttpVersion . HTTP2_AND_3 ,
110
123
priceClass : cloudfront . PriceClass . PRICE_CLASS_ALL ,
111
124
enabled : true ,
112
125
minimumProtocolVersion : cloudfront . SecurityPolicyProtocol . TLS_V1_2_2021 ,
113
- webAclId : props . firewall . attrArn ,
126
+ webAclId : this . firewallArn ,
114
127
enableIpv6 : true ,
115
128
116
129
defaultBehavior : {
117
- origin : new origins . S3Origin ( props . publicBucket , {
130
+ origin : new origins . S3Origin ( this . publicBucket , {
118
131
originAccessIdentity : this . originAccessIdentity ,
119
132
} ) ,
120
133
edgeLambdas : [
@@ -240,24 +253,6 @@ export class CdnStack extends NestedStack {
240
253
return config . cloud . deploy ?. api
241
254
}
242
255
243
- deployApi ( props ) {
244
- 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' , '_' ]
245
- keysToRemove . forEach ( key => delete env [ key as EnvKey ] )
246
-
247
- const secrets = new secretsmanager . Secret ( this , 'StacksSecrets' , {
248
- secretName : `${ props . appName } -${ props . appEnv } -secrets` ,
249
- description : 'Secrets for the Stacks application' ,
250
- generateSecretString : {
251
- secretStringTemplate : JSON . stringify ( env ) ,
252
- generateStringKey : Object . keys ( env ) . join ( ',' ) . length . toString ( ) ,
253
- } ,
254
- } )
255
-
256
- const functionName = `${ props . appName } -${ props . appEnv } -server`
257
-
258
- // this.apiVanityUrl = api.url
259
- }
260
-
261
256
apiBehaviorOptions ( ) : Record < string , cloudfront . BehaviorOptions > {
262
257
const origin = ( path : '/api' | '/api/*' = '/api' ) => new origins . HttpOrigin ( Fn . select ( 2 , Fn . split ( '/' , this . apiVanityUrl ) ) , { // removes the https://
263
258
originPath : path ,
@@ -287,7 +282,7 @@ export class CdnStack extends NestedStack {
287
282
docsBehaviorOptions ( ) : Record < string , cloudfront . BehaviorOptions > {
288
283
return {
289
284
'/docs' : {
290
- origin : new origins . S3Origin ( this . props . storage . publicBucket , {
285
+ origin : new origins . S3Origin ( this . publicBucket , {
291
286
originAccessIdentity : this . originAccessIdentity ,
292
287
originPath : '/docs' ,
293
288
} ) ,
@@ -298,7 +293,7 @@ export class CdnStack extends NestedStack {
298
293
cachePolicy : cloudfront . CachePolicy . CACHING_OPTIMIZED ,
299
294
} ,
300
295
'/docs/*' : {
301
- origin : new origins . S3Origin ( this . props . storage . publicBucket , {
296
+ origin : new origins . S3Origin ( this . publicBucket , {
302
297
originAccessIdentity : this . originAccessIdentity ,
303
298
originPath : '/docs' ,
304
299
} ) ,
@@ -315,13 +310,23 @@ export class CdnStack extends NestedStack {
315
310
return hasFiles ( p . projectPath ( 'docs' ) )
316
311
}
317
312
318
- additionalBehaviors ( props ) : Record < string , cloudfront . BehaviorOptions > {
313
+ additionalBehaviors ( props : NestedCloudProps ) : Record < string , cloudfront . BehaviorOptions > {
319
314
let behaviorOptions : Record < string , cloudfront . BehaviorOptions > = { }
320
315
321
316
if ( this . shouldDeployApi ( ) ) {
322
- this . deployApi ( props )
317
+ 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' , '_' ]
318
+ keysToRemove . forEach ( key => delete env [ key as EnvKey ] )
319
+
320
+ new secretsmanager . Secret ( this , 'StacksSecrets' , {
321
+ secretName : `${ props . appName } -${ props . appEnv } -secrets` ,
322
+ description : 'Secrets for the Stacks application' ,
323
+ generateSecretString : {
324
+ secretStringTemplate : JSON . stringify ( env ) ,
325
+ generateStringKey : Object . keys ( env ) . join ( ',' ) . length . toString ( ) ,
326
+ } ,
327
+ } )
323
328
324
- behaviorOptions = this . apiBehaviorOptions ( )
329
+ // behaviorOptions = this.apiBehaviorOptions()
325
330
}
326
331
327
332
// if docMode is used, we don't need to add a behavior for the docs
0 commit comments