SAM 模板资源缓存覆盖

SAM template resource cache override

在 SAM 模板文件中,我定义了一个 API 以及两个为一些路由配置了事件的 Lambda 函数。

在 API 级别,我启用了 API 和 TTL 的缓存。我现在想要覆盖 API 路由之一的缓存设置,但我似乎不知道如何去做。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Elrond API Facade

Resources:
  Api:
    Type: AWS::Serverless::Api
    Properties:
      Name: api
      StageName: Prod
      CacheClusterEnabled: true
      CacheClusterSize: '0.5'
      MethodSettings:
        - CachingEnabled: true
          CacheTtlInSeconds: 30
          HttpMethod: '*'
          ResourcePath: '/*'

  Handler:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: handler
      CodeUri: ./handler
      Handler: ./handler/index.handler
      Events:
        Method:
          Type: Api
          Properties:
            RestApiId: !Ref Api
            Path: /method
            Method: get
            # --> what to add here to override global caching settings?

Lambda 函数不包括开箱即用的缓存。让我们试试:

  1. 根据您的新缓存需求创建另一个“AWS::Serverless::Api”资源
  2. 改用您想要的“AWS::Serverless::Function”资源。

这是一个新的“AWS::Serverless::Api”示例,其中添加了更多缓存

Resources:

  Api:
    Type: AWS::Serverless::Api
    Properties:
      Name: api
      StageName: Prod
      CacheClusterEnabled: true
      CacheClusterSize: '0.5'
      MethodSettings:
        - CachingEnabled: true
          CacheTtlInSeconds: 30
          HttpMethod: '*'
          ResourcePath: '/*'
  BiggerCacheApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      CacheClusterEnabled: true
      CacheClusterSize: '0.5'
      MethodSettings:
        - CachingEnabled: true
          CacheTtlInSeconds: 3000
          HttpMethod: '*'
          ResourcePath: '/*'
  Handler:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: handler
      CodeUri: ./handler
      Handler: ./handler/index.handler
      Events:
        Method:
          Type: Api
          Properties:
            RestApiId: !Ref BiggerCacheApi
            Path: /method
            Method: get
  OtherHandler:
    Type: AWS::Serverless::Function
    Properties:
    ...
            RestApiId: !Ref Api

...