Skip to content

Commit 4b9011f

Browse files
njlynchCurtis Eppel
authored and
Curtis Eppel
committed
feat(cloudfront): Behaviors support cached methods, compression, viewer protocol, and smooth streaming (aws#9411)
Adds support for many of the missing properties for controlling behaviors on the new Distribution construct. Also removed (currently unavailable) properties from the README. The remaining properties will come in a follow-up PR. They were not included in this PR due to either being blocked by the latest CloudFormation spec merge, or are still being prioritized (e.g., fieldLevelEncryption). related aws#7086 related aws#9107 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 301fb52 commit 4b9011f

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

packages/@aws-cdk/aws-cloudfront/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,11 @@ const myWebDistribution = new cloudfront.Distribution(this, 'myDist', {
133133

134134
Additional behaviors can be specified at creation, or added after the initial creation. Each additional behavior is associated with an origin,
135135
and enable customization for a specific set of resources based on a URL path pattern. For example, we can add a behavior to `myWebDistribution` to
136-
override the default time-to-live (TTL) for all of the images.
136+
override the default viewer protocol policy for all of the images.
137137

138138
```ts
139139
myWebDistribution.addBehavior('/images/*.jpg', new origins.S3Origin(myBucket), {
140140
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
141-
defaultTtl: cdk.Duration.days(7),
142141
});
143142
```
144143

@@ -156,7 +155,6 @@ new cloudfront.Distribution(this, 'myDist', {
156155
'/images/*.jpg': {
157156
origin: bucketOrigin,
158157
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
159-
defaultTtl: cdk.Duration.days(7),
160158
},
161159
},
162160
});

packages/@aws-cdk/aws-cloudfront/lib/distribution.ts

+46-1
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,21 @@ export class AllowedMethods {
376376
private constructor(methods: string[]) { this.methods = methods; }
377377
}
378378

379+
/**
380+
* The HTTP methods that the Behavior will cache requests on.
381+
*/
382+
export class CachedMethods {
383+
/** HEAD and GET */
384+
public static readonly CACHE_GET_HEAD = new CachedMethods(['GET', 'HEAD']);
385+
/** HEAD, GET, and OPTIONS */
386+
public static readonly CACHE_GET_HEAD_OPTIONS = new CachedMethods(['GET', 'HEAD', 'OPTIONS']);
387+
388+
/** HTTP methods supported */
389+
public readonly methods: string[];
390+
391+
private constructor(methods: string[]) { this.methods = methods; }
392+
}
393+
379394
/**
380395
* Options for configuring custom error responses.
381396
*
@@ -461,10 +476,26 @@ export interface AddBehaviorOptions {
461476
/**
462477
* HTTP methods to allow for this behavior.
463478
*
464-
* @default - GET and HEAD
479+
* @default AllowedMethods.ALLOW_GET_HEAD
465480
*/
466481
readonly allowedMethods?: AllowedMethods;
467482

483+
/**
484+
* HTTP methods to cache for this behavior.
485+
*
486+
* @default CachedMethods.CACHE_GET_HEAD
487+
*/
488+
readonly cachedMethods?: CachedMethods;
489+
490+
/**
491+
* Whether you want CloudFront to automatically compress certain files for this cache behavior.
492+
* See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html#compressed-content-cloudfront-file-types
493+
* for file types CloudFront will compress.
494+
*
495+
* @default false
496+
*/
497+
readonly compress?: boolean;
498+
468499
/**
469500
* Whether CloudFront will forward query strings to the origin.
470501
* If this is set to true, CloudFront will forward all query parameters to the origin, and cache
@@ -482,6 +513,20 @@ export interface AddBehaviorOptions {
482513
*/
483514
readonly forwardQueryStringCacheKeys?: string[];
484515

516+
/**
517+
* Set this to true to indicate you want to distribute media files in the Microsoft Smooth Streaming format using this behavior.
518+
*
519+
* @default false
520+
*/
521+
readonly smoothStreaming?: boolean;
522+
523+
/**
524+
* The protocol that viewers can use to access the files controlled by this behavior.
525+
*
526+
* @default ViewerProtocolPolicy.ALLOW_ALL
527+
*/
528+
readonly viewerProtocolPolicy?: ViewerProtocolPolicy;
529+
485530
/**
486531
* The Lambda@Edge functions to invoke before serving the contents.
487532
*

packages/@aws-cdk/aws-cloudfront/lib/private/cache-behavior.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ export class CacheBehavior {
3838
return {
3939
pathPattern: this.props.pathPattern,
4040
targetOriginId: this.originId,
41-
allowedMethods: this.props.allowedMethods?.methods ?? undefined,
41+
allowedMethods: this.props.allowedMethods?.methods,
42+
cachedMethods: this.props.cachedMethods?.methods,
43+
compress: this.props.compress,
4244
forwardedValues: {
4345
queryString: this.props.forwardQueryString ?? false,
4446
queryStringCacheKeys: this.props.forwardQueryStringCacheKeys,
4547
},
46-
viewerProtocolPolicy: ViewerProtocolPolicy.ALLOW_ALL,
48+
smoothStreaming: this.props.smoothStreaming,
49+
viewerProtocolPolicy: this.props.viewerProtocolPolicy ?? ViewerProtocolPolicy.ALLOW_ALL,
4750
lambdaFunctionAssociations: this.props.edgeLambdas
4851
? this.props.edgeLambdas.map(edgeLambda => {
4952
if (edgeLambda.functionVersion.version === '$LATEST') {

packages/@aws-cdk/aws-cloudfront/test/private/cache-behavior.test.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import '@aws-cdk/assert/jest';
22
import { App, Stack } from '@aws-cdk/core';
3-
import { AllowedMethods } from '../../lib';
3+
import { AllowedMethods, CachedMethods, ViewerProtocolPolicy } from '../../lib';
44
import { CacheBehavior } from '../../lib/private/cache-behavior';
55

66
let app: App;
@@ -29,18 +29,25 @@ test('renders with all properties specified', () => {
2929
const behavior = new CacheBehavior('origin_id', {
3030
pathPattern: '*',
3131
allowedMethods: AllowedMethods.ALLOW_ALL,
32+
cachedMethods: CachedMethods.CACHE_GET_HEAD_OPTIONS,
33+
compress: true,
3234
forwardQueryString: true,
3335
forwardQueryStringCacheKeys: ['user_id', 'auth'],
36+
smoothStreaming: true,
37+
viewerProtocolPolicy: ViewerProtocolPolicy.HTTPS_ONLY,
3438
});
3539

3640
expect(behavior._renderBehavior()).toEqual({
3741
targetOriginId: 'origin_id',
3842
pathPattern: '*',
3943
allowedMethods: ['GET', 'HEAD', 'OPTIONS', 'PUT', 'PATCH', 'POST', 'DELETE'],
44+
cachedMethods: ['GET', 'HEAD', 'OPTIONS'],
45+
compress: true,
4046
forwardedValues: {
4147
queryString: true,
4248
queryStringCacheKeys: ['user_id', 'auth'],
4349
},
44-
viewerProtocolPolicy: 'allow-all',
50+
smoothStreaming: true,
51+
viewerProtocolPolicy: 'https-only',
4552
});
4653
});

0 commit comments

Comments
 (0)