question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[cloudfront] Cache Policies for CloudFrontWebDistribution

See original GitHub issue

Support for Cache Policies was released for the Distribution construct in #10656. This feature request tracks porting that same support over to the CloudFrontWebDistribution construct.

Use Case

For users of the stable CloudFrontWebDistribution construct who want to be able to control the cache key and TTLs.


This is a 🚀 Feature Request

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:24
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
nwitte-rocketloanscommented, Aug 20, 2021

I switched to this construct because the Distribution construct doesn’t have custom origin headers, but now I don’t have automation for setting cache policy.

2reactions
robertdcommented, Aug 21, 2021

The Distribution construct seems to be missing the ability to use custom headers on S3 Origins, which was why I switched to CloudfrontWebDistribution. I seem to be in an endless loop here because there are 3 Cloudfront Distribution constructs and all of them seem to be misssing features that I need. CfnDistribution doesn’t have a clear answer to associating Lambda@Edge functions to Cloudfront events.

CfnDistribution is an L1 construct where as CloudfrontWebDistribution and Distribution are L2 (opinionated) constructs. Typically, Cfn* constructs are used as a last resort.

Here is an example attaching lambda@edge function to a CF distro. Note that this is an experimental functionality. However, we’re using it in prod and so far it’s been really good. Also, CloudFront functions are a new thing (https://aws.amazon.com/blogs/aws/introducing-cloudfront-functions-run-your-code-at-the-edge-with-low-latency-at-any-scale/)… it’s worth taking a peek at those if you haven’t already.

const edgeLambda = new cloudfront.experimental.EdgeFunction(this, 'lambda-edge', {
  runtime: lambda.Runtime.NODEJS_14_X,
  handler: 'index.handler',
  code: lambda.Code.fromAsset(path.join(__dirname, 'path/to/function')),
});

const distro = new cloudfront.Distribution(this, 'distro', {
  defaultBehavior: {
    ...
    edgeLambdas: {
      functionVersion: edgeLambda.currentVersion,
      eventType: cloudfront.LambdaEdgeEventType.ORIGIN_REQUEST, // see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-cloudfront-trigger-events.html
    },
    ...
  },
...
});

I’ll have to test custom headers on S3 origins for a bit… but I bet you can use escape hatches to apply those if they’re not officially supported yet. (https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html)

Read more comments on GitHub >

github_iconTop Results From Across the Web

class CachePolicy (construct) · AWS CDK
Using an existing cache policy for a Distribution declare const bucketOrigin: origins.S3Origin; new cloudfront.Distribution(this, 'myDistManagedPolicy' ...
Read more >
javascript - How to set CachePolicy in cloudfront using CDK's ...
I am trying to set CachePolicy, OriginRequestPolicy and ResponseHeaderPolicy using CloudfrontWebDistribution Construct.
Read more >
CDK pattern – Caching static assets with AWS S3 and ...
In the AWS ecosystem, we need to store the files in S3 and set the cache-control header to tell the CloudFront distribution how...
Read more >
aws.cloudfront.Distribution - Pulumi
Creates an Amazon CloudFront web distribution. ... The unique identifier of the cache policy that is attached to the cache behavior. compress boolean....
Read more >
@aws-cdk/aws-cloudfront - npm
You can use a cache policy to improve your cache hit ratio by controlling the values (URL query strings, HTTP headers, and cookies)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found