Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudfront): Behaviors support cached methods, compression, viewer protocol, and smooth streaming #9411

Merged
merged 3 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/@aws-cdk/aws-cloudfront/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,11 @@ const myWebDistribution = new cloudfront.Distribution(this, 'myDist', {

Additional behaviors can be specified at creation, or added after the initial creation. Each additional behavior is associated with an origin,
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
override the default time-to-live (TTL) for all of the images.
override the default viewer protocol policy for all of the images.

```ts
myWebDistribution.addBehavior('/images/*.jpg', new origins.S3Origin(myBucket), {
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
defaultTtl: cdk.Duration.days(7),
});
```

Expand All @@ -156,7 +155,6 @@ new cloudfront.Distribution(this, 'myDist', {
'/images/*.jpg': {
origin: bucketOrigin,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
defaultTtl: cdk.Duration.days(7),
},
},
});
Expand Down
47 changes: 46 additions & 1 deletion packages/@aws-cdk/aws-cloudfront/lib/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,21 @@ export class AllowedMethods {
private constructor(methods: string[]) { this.methods = methods; }
}

/**
* The HTTP methods that the Behavior will cache requests on.
*/
export class CachedMethods {
/** HEAD and GET */
public static readonly CACHE_GET_HEAD = new CachedMethods(['GET', 'HEAD']);
/** HEAD, GET, and OPTIONS */
public static readonly CACHE_GET_HEAD_OPTIONS = new CachedMethods(['GET', 'HEAD', 'OPTIONS']);

/** HTTP methods supported */
public readonly methods: string[];

private constructor(methods: string[]) { this.methods = methods; }
}

/**
* Options for configuring custom error responses.
*
Expand Down Expand Up @@ -419,10 +434,26 @@ export interface AddBehaviorOptions {
/**
* HTTP methods to allow for this behavior.
*
* @default - GET and HEAD
* @default AllowedMethods.ALLOW_GET_HEAD
*/
readonly allowedMethods?: AllowedMethods;

/**
* HTTP methods to cache for this behavior.
*
* @default CachedMethods.CACHE_GET_HEAD
*/
readonly cachedMethods?: CachedMethods;

/**
* Whether you want CloudFront to automatically compress certain files for this cache behavior.
* See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html#compressed-content-cloudfront-file-types
* for file types CloudFront will compress.
*
* @default false
*/
readonly compress?: boolean;

/**
* Whether CloudFront will forward query strings to the origin.
* If this is set to true, CloudFront will forward all query parameters to the origin, and cache
Expand All @@ -440,6 +471,20 @@ export interface AddBehaviorOptions {
*/
readonly forwardQueryStringCacheKeys?: string[];

/**
* Set this to true to indicate you want to distribute media files in the Microsoft Smooth Streaming format using this behavior.
*
* @default false
*/
readonly smoothStreaming?: boolean;

/**
* The protocol that viewers can use to access the files controlled by this behavior.
*
* @default ViewerProtocolPolicy.ALLOW_ALL
*/
readonly viewerProtocolPolicy?: ViewerProtocolPolicy;

/**
* The Lambda@Edge functions to invoke before serving the contents.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ export class CacheBehavior {
return {
pathPattern: this.props.pathPattern,
targetOriginId: this.originId,
allowedMethods: this.props.allowedMethods?.methods ?? undefined,
allowedMethods: this.props.allowedMethods?.methods,
cachedMethods: this.props.cachedMethods?.methods,
compress: this.props.compress,
forwardedValues: {
queryString: this.props.forwardQueryString ?? false,
queryStringCacheKeys: this.props.forwardQueryStringCacheKeys,
},
viewerProtocolPolicy: ViewerProtocolPolicy.ALLOW_ALL,
smoothStreaming: this.props.smoothStreaming,
viewerProtocolPolicy: this.props.viewerProtocolPolicy ?? ViewerProtocolPolicy.ALLOW_ALL,
lambdaFunctionAssociations: this.props.edgeLambdas
? this.props.edgeLambdas.map(edgeLambda => {
if (edgeLambda.functionVersion.version === '$LATEST') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '@aws-cdk/assert/jest';
import { App, Stack } from '@aws-cdk/core';
import { AllowedMethods } from '../../lib';
import { AllowedMethods, CachedMethods, ViewerProtocolPolicy } from '../../lib';
import { CacheBehavior } from '../../lib/private/cache-behavior';

let app: App;
Expand Down Expand Up @@ -29,18 +29,25 @@ test('renders with all properties specified', () => {
const behavior = new CacheBehavior('origin_id', {
pathPattern: '*',
allowedMethods: AllowedMethods.ALLOW_ALL,
cachedMethods: CachedMethods.CACHE_GET_HEAD_OPTIONS,
compress: true,
forwardQueryString: true,
forwardQueryStringCacheKeys: ['user_id', 'auth'],
smoothStreaming: true,
viewerProtocolPolicy: ViewerProtocolPolicy.HTTPS_ONLY,
});

expect(behavior._renderBehavior()).toEqual({
targetOriginId: 'origin_id',
pathPattern: '*',
allowedMethods: ['GET', 'HEAD', 'OPTIONS', 'PUT', 'PATCH', 'POST', 'DELETE'],
cachedMethods: ['GET', 'HEAD', 'OPTIONS'],
compress: true,
forwardedValues: {
queryString: true,
queryStringCacheKeys: ['user_id', 'auth'],
},
viewerProtocolPolicy: 'allow-all',
smoothStreaming: true,
viewerProtocolPolicy: 'https-only',
});
});