titel
titel

Reputation: 3502

SAM template resource cache override

Within a SAM template file I have defined an API as well as two Lambda functions that have events configured for a few routes.

At API level I have enabled the caching for the API and a TTL. I would now want to have the caching settings overridden for one of the API routes but I don't seem to find out how to go about doing that.

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?

Upvotes: 2

Views: 1313

Answers (1)

petey
petey

Reputation: 17140

Lambda functions don't include caching out of the box. Lets try instead to:

  1. Create another "AWS::Serverless::Api" Resource with your new caching needs
  2. Have your intended "AWS::Serverless::Function" resource use it instead.

Here is an example of a new "AWS::Serverless::Api" with more caching added into the mix

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

...

Upvotes: 1

Related Questions