Description
Enable stage caching when the same REST API requests repeatedly reach the backend and their responses can be reused.
Potential impact
Caching can reduce backend calls and latency. The wrong cache key or expiry can expose another user’s response or serve old data.
Remediation
Set CacheClusterEnabled: true on the stage and CachingEnabled: true for the intended methods. Use cache keys that distinguish users or other inputs that change the response. Choose how long each response may be reused.
Examples
The example caches GET responses across all paths. Limit caching to suitable routes and supply MyRestApi and MyDeployment.
Before
yaml
Resources:
ProdStage:
Type: AWS::ApiGateway::Stage
Properties:
StageName: Prod
RestApiId: !Ref MyRestApi
DeploymentId: !Ref MyDeployment
TracingEnabled: true
After
yaml
Resources:
ProdStage:
Type: AWS::ApiGateway::Stage
Properties:
StageName: Prod
RestApiId: !Ref MyRestApi
DeploymentId: !Ref MyDeployment
TracingEnabled: true
CacheClusterEnabled: true
MethodSettings:
- ResourcePath: /*
HttpMethod: GET
CachingEnabled: true