【问题标题】:Setting up caching for APIGateway Methods为 APIGateway 方法设置缓存
【发布时间】:2017-02-27 04:33:00
【问题描述】:

我有以下 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, /cacheClusterEnabled/cacheClusterSize/clientCertificateId/{resourcePath}/{httpMethod}/metrics/enabled, /{resourcePath}/{httpMethod}/logging/dataTrace, /{resourcePath}/{httpMethod}/logging/loglevel, /{resourcePath}/{httpMethod}/throttling/burstLimit/{resourcePath}/{httpMethod}/throttling/rateLimit/{resourcePath}/{httpMethod}/caching/ttlInSeconds, /{resourcePath}/{httpMethod}/caching/enabled, /{resourcePath}/{httpMethod}/caching/dataEncrypted, /{resourcePath}/{httpMethod}/caching/requireAuthorizationForCacheControl, /{resourcePath}/{httpMethod}/caching/unauthorizedCacheControlHeaderStrategy, ///metrics/enabled, ///logging/dataTrace, ///logging/loglevel, ///throttling/burstLimit ///throttling/rateLimit ///caching/ttlInSeconds, ///caching/enabled, ///caching/dataEncrypted, ///caching/requireAuthorizationForCacheControl, ///caching/unauthorizedCacheControlHeaderStrategy, /va

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

【问题讨论】:

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


    【解决方案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)文档: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-stage-methodsetting.html

    【讨论】:

      【解决方案2】:

      如果有人仍在使用缓存,但没有使用缓存,我提供了一个示例来设置整个 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
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

        • 有趣 - 如果它不同,你能发布错误吗?
        • 没有错误。使用 HttpMethodResourcePath 创建的堆栈已更改(如您所建议的那样),但尚未启用缓存。
        • "true" 可能也需要是一个字符串:你可以尝试用引号括起来吗?
        猜你喜欢
        • 2017-07-19
        • 2020-10-19
        • 2017-11-12
        • 2016-08-19
        • 2020-04-27
        • 1970-01-01
        • 2014-10-02
        • 1970-01-01
        相关资源
        最近更新 更多