Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable CloudFront caching in serverless.yml

Serverless-1.0.0 enables deploying an API to be accessible via a AWS API Gateway with a custom domain

The question: With my serverless.yml I need to disable CloudFront's caching (e.g. by setting some zero TTL for caching headers... is there any other way?)

Can that be done from within serverless.yml or serverless framework?

like image 895
Blaise Avatar asked Oct 21 '16 10:10

Blaise


People also ask

How do I turn off CloudFront caching?

On your custom origin web server application, add Cache-Control no-cache, no-store, or private directives to the objects that you don't want CloudFront to cache. Or, add Expires directives to the objects that you don't want CloudFront to cache.

How do I enable caching in CloudFront?

If you want OPTIONS responses to be cached, do the following: Choose the options for default cache behavior settings that enable caching for OPTIONS responses. Configure CloudFront to forward the following headers: Origin , Access-Control-Request-Headers , and Access-Control-Request-Method .

How do I set cache policy in CloudFront?

Open the CloudFront console at https://console.aws.amazon.com/cloudfront/v3/home . Choose Create distribution. In the Cache key and origin requests section, make sure that Cache policy and origin request policy is chosen. For Cache policy, choose the cache policy to attach to this distribution's default cache behavior.

Is AWS CloudFront Serverless?

CloudFront allows Serverless applications to provide a fast and low-latency experience to end users by making sure that the static parts of their application load fast, irrespective of where its users are.


1 Answers

Have you tried defining a cachePolicy under provider, as per Serverless's reference YML, and then linking the cachePolicy via a function's CloudFront event? Straight from Serverless Frameworks sample YML like this:

provider:
  cloudFront:
    myCachePolicy1: # used as a reference in function.events[].cloudfront.cachePolicy.name
      DefaultTTL: 60
      MinTTL: 30
      MaxTTL: 3600
      Comment: my brand new cloudfront cache policy # optional
      ParametersInCacheKeyAndForwardedToOrigin:
        CookiesConfig:
          CookieBehavior: whitelist # Possible values are 'none', 'whitelist', 'allExcept' and 'all'
          Cookies:
            - my-public-cookie
        EnableAcceptEncodingBrotli: true # optional
        EnableAcceptEncodingGzip: true
        HeadersConfig:
          HeadersBehavior: whitelist # Possible values are 'none' and 'whitelist'
          Headers:
            - authorization
            - content-type
        QueryStringsConfig:
          QueryStringBehavior: allExcept # Possible values are 'none', 'whitelist', 'allExcept' and 'all'
          QueryStrings:
            - not-cached-query-string

functions:
  yourFunction:
    events:
      - cloudFront:
          eventType: viewer-response
          includeBody: true
          pathPattern: /docs*
          cachePolicy:
            # Note, you can use only one of name or id
            name: myCachePolicy1 # Refers to a Cache Policy defined in provider.cloudFront.cachePolicies
            id: 658327ea-f89d-4fab-a63d-7e88639e58f6 # Refers to any external Cache Policy id
          origin:
            DomainName: serverless.com
            OriginPath: /framework
            CustomOriginConfig:
              OriginProtocolPolicy: match-viewer
like image 190
pomSense Avatar answered Sep 20 '22 16:09

pomSense