@@ -5,12 +5,12 @@ import * as path from "path";
5
5
import { LambdaRestApiProps , RestApi } from "aws-cdk-lib/aws-apigateway" ;
6
6
import {
7
7
AllowedMethods ,
8
- CacheHeaderBehavior ,
9
- CachePolicy ,
10
- CacheQueryStringBehavior ,
8
+ CfnCachePolicy ,
9
+ CfnOriginRequestPolicy ,
11
10
DistributionProps ,
11
+ ICachePolicy ,
12
12
IOrigin ,
13
- OriginRequestPolicy ,
13
+ IOriginRequestPolicy ,
14
14
OriginSslPolicy ,
15
15
PriceClass ,
16
16
ViewerProtocolPolicy ,
@@ -21,11 +21,12 @@ import { Runtime } from "aws-cdk-lib/aws-lambda";
21
21
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs" ;
22
22
import { LogGroup , RetentionDays } from "aws-cdk-lib/aws-logs" ;
23
23
import { IBucket } from "aws-cdk-lib/aws-s3" ;
24
- import { ArnFormat , Aws , Duration , Lazy , Stack } from "aws-cdk-lib" ;
24
+ import { ArnFormat , Aws , Duration , Lazy , Resource , Stack } from "aws-cdk-lib" ;
25
25
import { Construct } from "constructs" ;
26
26
import { CloudFrontToApiGatewayToLambda } from "@aws-solutions-constructs/aws-cloudfront-apigateway-lambda" ;
27
27
28
28
import { addCfnSuppressRules } from "../../utils/utils" ;
29
+ import { Conditions } from "../common-resources/common-resources-construct" ;
29
30
import { SolutionConstructProps } from "../types" ;
30
31
31
32
export interface BackEndProps extends SolutionConstructProps {
@@ -35,6 +36,7 @@ export interface BackEndProps extends SolutionConstructProps {
35
36
readonly logsBucket : IBucket ;
36
37
readonly uuid : string ;
37
38
readonly cloudFrontPriceClass : string ;
39
+ readonly conditions : Conditions ;
38
40
}
39
41
40
42
export class BackEnd extends Construct {
@@ -135,21 +137,9 @@ export class BackEnd extends Construct {
135
137
} ,
136
138
] ) ;
137
139
138
- const cachePolicy = new CachePolicy ( this , "CachePolicy" , {
139
- cachePolicyName : `ServerlessImageHandler-${ props . uuid } ` ,
140
- defaultTtl : Duration . days ( 1 ) ,
141
- minTtl : Duration . seconds ( 1 ) ,
142
- maxTtl : Duration . days ( 365 ) ,
143
- enableAcceptEncodingGzip : true ,
144
- headerBehavior : CacheHeaderBehavior . allowList ( "origin" , "accept" ) ,
145
- queryStringBehavior : CacheQueryStringBehavior . allowList ( "signature" ) ,
146
- } ) ;
140
+ const cachePolicy = new CustomBackEndCachePolicy ( this , "CachePolicy" , props ) ;
147
141
148
- const originRequestPolicy = new OriginRequestPolicy ( this , "OriginRequestPolicy" , {
149
- originRequestPolicyName : `ServerlessImageHandler-${ props . uuid } ` ,
150
- headerBehavior : CacheHeaderBehavior . allowList ( "origin" , "accept" ) ,
151
- queryStringBehavior : CacheQueryStringBehavior . allowList ( "signature" ) ,
152
- } ) ;
142
+ const originRequestPolicy = new CustomBackEndOriginRequestPolicy ( this , "OriginRequestPolicy" , props ) ;
153
143
154
144
const apiGatewayRestApi = RestApi . fromRestApiId (
155
145
this ,
@@ -215,3 +205,79 @@ export class BackEnd extends Construct {
215
205
this . domainName = imageHandlerCloudFrontApiGatewayLambda . cloudFrontWebDistribution . distributionDomainName ;
216
206
}
217
207
}
208
+
209
+ class CustomBackEndCachePolicy extends Resource implements ICachePolicy {
210
+ public readonly cachePolicyId : string ;
211
+
212
+ constructor ( scope : Construct , id : string , props : BackEndProps ) {
213
+ super ( scope , id , {
214
+ physicalName : `ServerlessImageHandler-${ props . uuid } ` ,
215
+ } ) ;
216
+
217
+ const cachePolicy = new CfnCachePolicy ( this , "Resource" , {
218
+ cachePolicyConfig : {
219
+ name : `ServerlessImageHandler-${ props . uuid } ` ,
220
+ defaultTtl : Duration . days ( 1 ) . toSeconds ( ) ,
221
+ minTtl : Duration . seconds ( 1 ) . toSeconds ( ) ,
222
+ maxTtl : Duration . days ( 365 ) . toSeconds ( ) ,
223
+ parametersInCacheKeyAndForwardedToOrigin : {
224
+ enableAcceptEncodingGzip : true ,
225
+ enableAcceptEncodingBrotli : false ,
226
+ queryStringsConfig : {
227
+ queryStringBehavior : "whitelist" ,
228
+ queryStrings : [ "signature" ] ,
229
+ } ,
230
+ headersConfig : {
231
+ headerBehavior : "whitelist" ,
232
+ } ,
233
+ cookiesConfig : {
234
+ cookieBehavior : "none" ,
235
+ } ,
236
+ } ,
237
+ } ,
238
+ } ) ;
239
+
240
+ // https://github.com/aws/aws-cdk/issues/8396#issuecomment-857690411
241
+ cachePolicy . addOverride (
242
+ "Properties.CachePolicyConfig.ParametersInCacheKeyAndForwardedToOrigin.HeadersConfig.Headers" ,
243
+ {
244
+ "Fn::If" : [ props . conditions . enableAutoWebPCondition . logicalId , [ "origin" , "accept" ] , [ "origin" ] ] ,
245
+ }
246
+ ) ;
247
+
248
+ this . cachePolicyId = cachePolicy . ref ;
249
+ }
250
+ }
251
+
252
+ class CustomBackEndOriginRequestPolicy extends Resource implements IOriginRequestPolicy {
253
+ public readonly originRequestPolicyId : string ;
254
+
255
+ constructor ( scope : Construct , id : string , props : BackEndProps ) {
256
+ super ( scope , id , {
257
+ physicalName : `ServerlessImageHandler-${ props . uuid } ` ,
258
+ } ) ;
259
+
260
+ const originRequestPolicy = new CfnOriginRequestPolicy ( this , "Resource" , {
261
+ originRequestPolicyConfig : {
262
+ name : `ServerlessImageHandler-${ props . uuid } ` ,
263
+ headersConfig : {
264
+ headerBehavior : "whitelist" ,
265
+ } ,
266
+ queryStringsConfig : {
267
+ queryStringBehavior : "whitelist" ,
268
+ queryStrings : [ "signature" ] ,
269
+ } ,
270
+ cookiesConfig : {
271
+ cookieBehavior : "none" ,
272
+ } ,
273
+ } ,
274
+ } ) ;
275
+
276
+ // https://github.com/aws/aws-cdk/issues/8396#issuecomment-857690411
277
+ originRequestPolicy . addOverride ( "Properties.OriginRequestPolicyConfig.HeadersConfig.Headers" , {
278
+ "Fn::If" : [ props . conditions . enableAutoWebPCondition . logicalId , [ "origin" , "accept" ] , [ "origin" ] ] ,
279
+ } ) ;
280
+
281
+ this . originRequestPolicyId = originRequestPolicy . ref ;
282
+ }
283
+ }
0 commit comments