aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.61k stars 3.9k forks source link

Cloudfront Distribution: Re-ordering behaviors #14866

Open haapti opened 3 years ago

haapti commented 3 years ago

:question: General Issue

The Question

Is there a way to re-order behaviors inside already created distribution?

The reason why im asking this is that im using Serverless NextJSLambdaEdge constructor for deploying Next.js app. My Next app needs custom distribution behaviors for several api routes but these behaviours drops on bottom of the list which makes them overridden by rules above.

For reference NextJSLambdaEdge constructor creates the distribution here:

https://github.com/serverless-nextjs/serverless-next.js/blob/master/packages/serverless-components/nextjs-cdk-construct/src/index.ts#L289-L308

I have asked a feature in order to adding custom behaviors. Here you can also see how im adding behaviors now without being able to set the specific order/precedence for them.

https://github.com/serverless-nextjs/serverless-next.js/issues/1069

Environment

njlynch commented 3 years ago

There is not currently a way to re-order behaviors on a Distribution. I'm repurposing this as a feature request to track it. I am unassigning and marking this issue as p2, which means that we are unable to work on this immediately.

We use +1s to help prioritize our work, and are happy to revaluate this issue based on community feedback. You can reach out to the cdk.dev community on Slack to solicit support for reprioritization.

In the meantime, you could do some hacky work-arounds, include manipulating the CacheBehaviors ordering using escape hatches or tweaking the order by accessing the private member:

// Swaps the first and second cache behaviors (but not the default cache behavior!) on a Distribution called `dist`
const behaviors = (dist as any).additionalBehaviors as CacheBehavior[];
const tmp = behaviors[0];
behaviors[0] = behaviors[1];
behaviors[1] = tmp;
haapti commented 3 years ago

ok, thanks! this hack works fine and going to use it until something better is available.

gtskaushik commented 2 days ago

Thanks. This hack works