Description
Cache GET responses on a SAM REST API when repeated requests can safely reuse a response.
Potential impact
A cache can reduce latency and backend load. A shared entry can also serve stale data or another user’s response.
Remediation
Set CacheClusterEnabled: true. In MethodSettings, enable CachingEnabled for the intended methods. Choose cache keys that separate distinct responses and a time to live that meets your freshness needs.
Examples
ResourcePath: /* applies the example to every GET route. Narrow it to routes that can share cached responses and supply the API definition and cache keys.
Before
yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
TracingEnabled: true
After
yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
TracingEnabled: true
CacheClusterEnabled: true
MethodSettings:
- ResourcePath: /*
HttpMethod: GET
CachingEnabled: true