AWS::Serverless::Api 配置缓存键支持任意查询字符串参数
解决方案
AWS API Gateway 的 cacheKeyParameters 不支持通配符来捕获所有未知查询参数,但可以通过**缓存键表达式(cacheKeyExpression)**实现包含所有查询字符串的需求,具体修改如下:
将原配置中的 cacheKeyParameters 替换为 cacheKeyExpression,利用 $context.request.querystring 变量获取完整的查询字符串(包含所有键值对),确保不同查询参数的请求生成不同的缓存键。
修改后的完整 API 定义:
openapi: 3.0.1 info: description: API as a proxy to public url version: 1.0.0 paths: "/{proxy+}": x-amazon-apigateway-any-method: parameters: - name: proxy in: path required: true schema: type: string responses: {} x-amazon-apigateway-integration: cacheKeyExpression: "$method.request.path.proxy + $context.request.querystring" responses: default: statusCode: '200' requestParameters: integration.request.path.proxy: method.request.path.proxy uri: Fn::FindInMap : [EnvMap, Ref: Env, proxyUrl] passthroughBehavior: when_no_match httpMethod: ANY type: http_proxy
说明
$method.request.path.proxy保留路径参数作为缓存键的一部分$context.request.querystring会自动拼接所有查询参数的键值对(格式与请求中的查询字符串一致),确保不同查询参数组合的请求被缓存为独立条目- 若坚持使用
cacheKeyParameters,由于 API Gateway 不支持通配符匹配未知查询参数,此方式无法实现需求,因此优先推荐缓存键表达式方案
内容的提问来源于stack exchange,提问作者user2919910
相关产品推荐
相关产品推荐

