如何在serverless.yml中禁用CloudFront缓存

7

Serverless-1.0.0可以使API通过AWS API Gateway使用自定义域名访问。

问题是:我需要在serverless.yml中禁用CloudFront的缓存(例如通过设置一些零TTL的缓存头...还有其他方法吗?)

这能否在serverless.yml或serverless框架内完成?

1个回答

1

您是否尝试根据Serverless参考YMLprovider下定义cachePolicy,然后通过函数的CloudFront事件链接cachePolicy?这正是从Serverless Frameworks示例YML中获取的:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接