SAM模板资源缓存覆盖

时间:2020-07-20 10:26:57

标签: aws-lambda aws-api-gateway aws-sam

在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?

1 个答案:

答案 0 :(得分:1)

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

...