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): support for cache policies #10656

Merged
merged 11 commits into from
Oct 7, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"Properties": {
"DistributionConfig": {
"DefaultCacheBehavior": {
"ForwardedValues": {
"QueryString": false
},
"CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
"TargetOriginId": "cloudfronthttporiginDistributionOrigin162B02709",
"ViewerProtocolPolicy": "allow-all"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,7 @@
"Properties": {
"DistributionConfig": {
"DefaultCacheBehavior": {
"ForwardedValues": {
"QueryString": false
},
"CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
"TargetOriginId": "cloudfrontloadbalanceroriginDistributionOrigin1BCC75186",
"ViewerProtocolPolicy": "allow-all"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,14 @@
"DistributionConfig": {
"CacheBehaviors": [
{
"ForwardedValues": {
"QueryString": false
},
"CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
"PathPattern": "/api",
"TargetOriginId": "cloudfrontorigingroupDistributionOriginGroup10B57F1D1",
"ViewerProtocolPolicy": "allow-all"
}
],
"DefaultCacheBehavior": {
"ForwardedValues": {
"QueryString": false
},
"CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
"TargetOriginId": "cloudfrontorigingroupDistributionOriginGroup10B57F1D1",
"ViewerProtocolPolicy": "allow-all"
},
Expand Down Expand Up @@ -153,4 +149,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@
"Properties": {
"DistributionConfig": {
"DefaultCacheBehavior": {
"ForwardedValues": {
"QueryString": false
},
"CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
"TargetOriginId": "cloudfronts3originDistributionOrigin1741C4E95",
"ViewerProtocolPolicy": "allow-all"
},
Expand Down Expand Up @@ -106,4 +104,4 @@
}
}
}
}
}
37 changes: 37 additions & 0 deletions packages/@aws-cdk/aws-cloudfront/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,43 @@ new cloudfront.Distribution(this, 'myDist', {
});
```

### Customizing Cache Keys and TTLs with Cache Policies

You can use a cache policy to improve your cache hit ratio by controlling the values (URL query strings, HTTP headers, and cookies)
that are included in the cache key, and/or adjusting how long items remain in the cache via the time-to-live (TTL) settings.
CloudFront provides some predefined cache policies, known as managed policies, for common use cases. You can use these managed policies,
or you can create your own cache policy that’s specific to your needs.
See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/controlling-the-cache-key.html for more details.

```ts
// Using an existing cache policy
new cloudfront.Distribution(this, 'myDistManagedPolicy', {
defaultBehavior: {
origin: bucketOrigin,
cachePolicy: cloudfront.CachePolicy.CACHING_OPTIMIZED,
},
});

// Creating a custom cache policy -- all parameters optional
const myCachePolicy = new cloudfront.CachePolicy(this, 'myCachePolicy', {
cachePolicyName: 'MyPolicy',
comment: 'A default policy',
defaultTtl: Duration.days(2),
minTtl: Duration.minutes(1),
maxTtl: Duration.days(10),
cookieBehavior: cloudfront.CacheCookieBehavior.all(),
headerBehavior: cloudfront.CacheHeaderBehavior.allowList('X-CustomHeader'),
queryStringBehavior: cloudfront.CacheQueryStringBehavior.denyList('username'),
enableAcceptEncodingGzip: true,
});
new cloudfront.Distribution(this, 'myDistCustomPolicy', {
defaultBehavior: {
origin: bucketOrigin,
cachePolicy: myCachePolicy,
},
});
```

### Lambda@Edge

Lambda@Edge is an extension of AWS Lambda, a compute service that lets you execute functions that customize the content that CloudFront delivers.
Expand Down
Loading