为APIGateway方法设置缓存

时间:2017-02-27 04:33:01

标签: templates caching aws-api-gateway amazon-cloudformation

我有以下CF模板

{
    "Conditions":{
        "CreatedProdStage" : {...}
    }
    ...
    "Resources":{
        "GetMethod": {
            ...
        },
        "ApiDeployement":{
            ...
        },
        "ProdStage":{
            "Type":"AWS::ApiGateway::Stage",
            "Condition":"CreatedProdStage",
            "Properties": {
                "DeploymentId":"...",
                "RestApiId":"...",
                "MethodSettings":[{
                    "CachingEnabled":true,
                    "HttpMethod":{"Ref":"GetMethod"},
                    "ResourcePath":"/"
                }]
            }
        }
    }
}

我收到错误

  

无效的方法设置路径:   /〜1 / ST-GetMetho-xxxAUMMRWxxx /缓存/启用。必须是以下之一:   [/ deploymentId,/ description,   / {列举HTTPMethod} / /启用/ cacheClusterEnabled / cacheClusterSize / clientCertificateId / {resourcePath}度量   / {resourcePath} / {列举HTTPMethod} /记录/ dataTrace,   / {resourcePath} / {列举HTTPMethod} /记录/记录级,   / {resourcePath} / {列举HTTPMethod} /节流/ burstLimit / {resourcePath} / {列举HTTPMethod} /节流/ rateLimit / {resourcePath} / {列举HTTPMethod} /缓存/ ttlInSeconds,   / {resourcePath} / {列举HTTPMethod} /缓存/启用,   / {resourcePath} / {列举HTTPMethod} /缓存/ dataEncrypted,   / {resourcePath} / {列举HTTPMethod} /缓存/ requireAuthorizationForCacheControl,   / {resourcePath} / {列举HTTPMethod} /缓存/ unauthorizedCacheControlHeaderStrategy,   / / / metrics / enabled,/ / / logging / dataTrace,/ / / logging / loglevel,   / / / throttling / burstLimit / / / throttling / rateLimit   / / / caching / ttlInSeconds,/ / / caching / enabled,   / / 的/缓存/ dataEncrypted,   / / 的/缓存/ requireAuthorizationForCacheControl,   / / / caching / unauthorizedCacheControlHeaderStrategy,/ va

我错过了什么吗?我认为ResourcePathHttpMethod是唯一必需的属性

3 个答案:

答案 0 :(得分:1)

首先需要在CacheClusterEnabled属性的舞台上启用缓存。这将允许您像在MethodSettings中一样设置方法的缓存:

...
"ProdStage":{
        "Type":"AWS::ApiGateway::Stage",
        "Condition":"CreatedProdStage",
        "Properties": {
            "DeploymentId":"...",
            "RestApiId":"...",
            "CacheClusterEnabled": true
            "MethodSettings":[{
                "CachingEnabled":true,
                "HttpMethod":{"Ref":"GetMethod"},
                "ResourcePath":"/"
            }]
        }
    }

然后你需要修复给定的错误。您的ResourcePath与错误输出中列出的其中一个匹配。这些没有在文档中列出,因此您需要使用它有点令人困惑。您目前拥有的只是根路径设置。如果您希望所有路径都使用"/*"

APIGateWay :: MethodSettings(请参阅ResourcePath)doc: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-stage-methodsetting.html

答案 1 :(得分:0)

尝试将HttpMethod设置为字符串而不是引用:

"MethodSettings":[{
        "CachingEnabled":true,
        "HttpMethod": "GET",
        "ResourcePath":"/"
    }]
}

答案 2 :(得分:0)

如果有人仍然到达这里,但没有使用缓存,我提供了一个示例来设置整个 API 的节流和日志记录。直到我开始使用 ResourcePath 和 HttpMethod 并注意到错误发生变化,我才弄明白。

请注意,我将 * 用于路径和方法以及 USED QUOTATIONS。没有引号会失败。

  ProdStage:
    Type: AWS::ApiGateway::Stage
    Properties:
      StageName: Prod
      RestApiId: !Ref StunningDisco
      DeploymentId: !Ref StunningDiscoDeployment
      MethodSettings:
        - ResourcePath: '/*'
          HttpMethod: '*'
          LoggingLevel: INFO
          DataTraceEnabled: True
          ThrottlingBurstLimit: '10'
          ThrottlingRateLimit: '10.0'

  StunningDiscoDomainMapping:
    Type: 'AWS::ApiGateway::BasePathMapping'
    DependsOn: ProdStage
    Properties:
      DomainName: !Ref StunningDiscoDomain
      RestApiId: !Ref StunningDisco
      Stage: !Ref ProdStage

  StunningDiscoDeployment:
    Type: AWS::ApiGateway::Deployment
    DependsOn: [StunningDiscoRootEndpoint, LightsInvokeEndpoint]
    Properties:
      RestApiId: !Ref StunningDisco