serverless-components / aws-cloudfront

Deploy an AWS CloudFront distribution for the provided origins using Serverless Components
https://serverless.com
Apache License 2.0
27 stars 12 forks source link

More cache behavior options. #6

Open Pavel910 opened 5 years ago

Pavel910 commented 5 years ago

So today I ran into a problem where I'm deploying an Apollo Server for my GraphQL API and CloudFront is causing a few problems:

1) it is not forwarding several headers that are important for my API (Accept and Accept-Language), that behavior is documented in official AWS docs.

2) by default POST requests are not allowed, and GraphQL runs on POST most of the time.

Proposed solution

Introduce more options to @serverless/aws-cloudfront component to allow headers and methods customization. Here is an example:

distribution:
  component: '@serverless/aws-cloudfront'
  inputs:
    origins:
      - url: ${api.url}
        pathPatterns:
          "*":
            ttl: 0
            forward:
              headers: ["Accept", "Accept-Language"]
            allowedMethods: ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]

@eahefnawy This is related to the issue in aws-api-gateway repo. I know this looks like too many options, but since CloudFront is a quirky CDN, I can't seem to find a cleaner solution to the problem.

I'd be glad to send a PR, but I want to hear your opinion on this first.

Cheers! 🍻

eahefnawy commented 5 years ago

@Pavel910 yeah totally agreed with you. This component is meant to be a lower level component, so it should eventually support all these options. So PR is welcome 😊

Honestly, we need to refactor the entire domain component cause there's a lot of CloudFront stuff happening in there that should be included here. But this task turned out to be more complex than I thought as it also affects the website and cdn components. But overall, the cloudfront component is where we want to handle all the cloudfront provisioning stuff.