sst / ion

❍ — a new engine for SST
https://ion.sst.dev
MIT License
1.09k stars 126 forks source link

How to handle AWS cache policy limit #368

Open nserbass opened 1 month ago

nserbass commented 1 month ago

Use case: My team is using SST to deploy a Nextjs application in multiple stage environments for testing feeatures before deploy to production (another env deployed by sst).

Problem: Due to aws cloudfront cache policy limit, we can't create too many environments because we reach the limit quickly (20)

I've followed your documentation about transform function. I've tried several implementations but always creates a new one and throws errors with missing defaultCacheBehavior parameters:

new sst.aws.Nextjs("MyWeb", {
  environment,
  transform: {
    cdn: {
      defaultCacheBehavior: {
        cachePolicyId: 'f99c40f2-50bf-4215-8023-125e7a46fb0e'
      }
    }
  },
});
new sst.aws.Nextjs("MyWeb", {
  environment,
  transform: {
    cdn: (cdnArgs) => {
      cdnArgs.defaultCacheBehavior = {
        cachePolicyId: 'f99c40f2-50bf-4215-8023-125e7a46fb0e'
      }
    }
  },
});
// it doesn't throw errors but creates a new cache policy as well
new sst.aws.Nextjs("MyWeb", {
  environment,
  domain: customDomain,
  transform: {
    cdn: (args) => ({
      ...args,
      defaultCacheBehavior: {
        ...args.defaultCacheBehavior,
        cachePolicyId: 'f99c40f2-50bf-4215-8023-125e7a46fb0e'
      }
    })
  }
});

Any ideas?

Thank you!

eschaefer commented 1 month ago

Just chiming in to +1 this since we have a similar limit for our team. I imagine this would be a common use-case for teams that work in orgs with arbitrary limits on cache policies.

jschuur commented 1 month ago

I also ran into this the other week after capriciously creating a bunch of SST Ion prototypes until this popped up. Luckily, I was able to delete other ones as a short term stop gap.

Based on the docs, it wasn't clear to me how to use this transform param.

jayair commented 1 month ago

Yeah this sounds like something we should try and handle internally. Let me ask the team.

cgcompassion commented 2 days ago

Note: In SST 2 this was possible as per these docs: https://docs.sst.dev/constructs/NextjsSite#reusing-cloudfront-cache-policies

iliaamiri commented 2 days ago

This was addressed and solved by @fwang before in SST 2 and I've never had an issue with it. But, in SST Ion, it's a blocker for my case due to AWS' hard limit of 20 cache policies.

Will really appreciate an update on this :heart: :pray: