We're looking to manage Cloudfront outside of serverless as we have multiple origins to support,
We're noticing that if we create cache behaviours in the AWS console and deploy they get wiped on deploy.
Is there any way to persist behaviours created outside of serverless?
I'm working with dan on this. Would you accept a pr if tested which persists the existing CacheBehaviors?
https://github.com/serverless-nextjs/serverless-next.js/compare/master...raffij:patch-1
Have you tried passing an existing distribution id?
# serverless.yml
myNextApplication:
component: "@sls-next/serverless-component@{version_here}"
inputs:
cloudfront:
# if you want to use an existing cloudfront distribution, provide it here
distributionId: XYZEXAMPLE #optional
It was briefly documented here https://github.com/serverless-nextjs/serverless-next.js/tree/master/packages/serverless-components/aws-cloudfront#updating-an-existing-cloudfront-distribution
Thanks for the info @danielcondemarin we are using this functionality, but we want the existing CacheBehaviors to persist.
I saw in the code you'd done this for Origins https://github.com/serverless-nextjs/serverless-next.js/blob/master/packages/serverless-components/aws-cloudfront/lib/index.js#L254-L268, but looks like CacheBehavior only outputs the config from serverless and doesn't merge with the existing config. https://github.com/serverless-nextjs/serverless-next.js/blob/master/packages/serverless-components/aws-cloudfront/lib/index.js#L271
This is a use case I hadn't foreseen tbh. Would you mind expanding a bit more on your usecase? i.e. Why using the origin provisioned by serverless-next.js rather than having a separate origin? Is it that you're trying to reuse the bucket serverless-next.js provisions?
Heya. The use case is that were using cloudfront for a larger deployment, several origins configured and several cache behaviours for elastic beanstalk and EC2 instances.
Setting the existing distribution id in the serverless yml works, however every deploy clears all the cache behaviours we've created for our other origins
It's quite possible I've misunderstood your advice though and im doing something unwise 馃樄
Thanks again for your response.
Our current situation:
We have all our infrastructure configured with terrraform. This creates a cloudfront distribution with a number of origins /behaviours all setup as paths off the main domain.
Our desired outcome:
Continue using this cloudfront distribution, but deploy a nextjs app to the root / of cloudfront and also any behaviours / origins that it requires. We'll then set terrraform to ignore the origins / behaviours that serverless produces.
Our thinking was we could continue using the terrraform cloudfront and serverless-nextjs could add what it needs, but it doesn't appear to work how we need. When we run serverless it keeps the origins that already exist, but destroys the cache behaviours that are not defined in our serverless.yml.
Hopefully that makes sense?
Ah I understand, you're absolutely right, it should keep pre-existing cache behaviours and only override those managed by serverless-next.js.
Happy to take a PR for your patch if you add some tests 馃檹 thanks!
Thanks for confirming. We'll send a pr once we've checked its working as needed and written tests.
We also have another pr coming that will address the final issue with origins.