【问题标题】:Setting up Stage environment (Caching) for APIGateway using CloudFormation使用 CloudFormation 为 APIGateway 设置舞台环境(缓存)
【发布时间】:2017-07-19 18:56:27
【问题描述】:

我已经使用CloudFormation 设置了APIGateway,它公开了一种方法为/customers/{customerId}。该方法调用 dynamodb 服务而不是使用任何 lambda,如果映射丢失,则返回带有空对象的 HTTP 200 或带有 HTTP 200customer 对象。

现在,我想为我的prod 阶段设置缓存。我有一个现有的 api,我使用 AWS APIGateway Web UI 创建并创建了 prod 阶段。我想在 CF 中复制这些属性。这是我在旧 api 中的内容

缓存设置

缓存状态:可用

缓存容量:0.5GB

缓存生存时间 (TTL):300

每键缓存失效

需要授权:勾选 处理未经授权的请求:忽略缓存控制头;在响应头中添加警告

默认方法限制

启用限制:选中

率:1000

爆发:200

我尝试像这样设置第一部分(缓存设置),但它没有像我预期的那样产生所需的 prod 阶段设置。如何使用 CloudFormation 实现上述输出?

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters":{
        "EnvType": {
            "Type": "String",
            "Default": "test",
            "AllowedValues": ["test", "prod"],
            "Description": "Select what stage need to be created"
        }
    },
    "Conditions":{
        "CreateProdStage" : {"Fn::Equals": [{"Ref":"EnvType"}, "prod"]},
        "CreateTestStage" : {"Fn::Equals": [{"Ref":"EnvType"}, "test"]}
    },
    "Resources": {
        "MyAPI": {
            ...
        },
        "MyAPIResource": {
            ...
        },
        "GetMethod":{
            ...
        },
        "ApiDeployment":{
            "Type":"AWS::ApiGateway::Deployment",
            "Properties":{
                "RestApiId":{"Ref":"MyAPI"}
            },
            "DependsOn":["GetMethod"]
        },
        "TestStage" : {
            "Type" : "AWS::ApiGateway::Stage",
            "Condition":"CreateTestStage",
            "Properties" : {
                "DeploymentId" : {"Ref":"ApiDeployment"},
                "Description" : "Test Stage",
                "RestApiId" : {"Ref":"MyAPI"},
                "StageName" : "test"
            }
        },
        "ProdStage" : {
            "Type" : "AWS::ApiGateway::Stage",
            "Condition":"CreateProdStage",
            "Properties" : {
                "DeploymentId" : {"Ref":"ApiDeployment"},
                "Description" : "Prod Stage",
                "RestApiId" : {"Ref":"MyAPI"},
                "StageName" : "prod",
                "MethodSettings":[{
                    "CachingEnabled":true,
                    "HttpMethod":"*",
                    "ResourcePath":"/*",
                    "CacheTtlInSeconds":300,
                    "ThrottlingBurstLimit" : 2000,
                    "ThrottlingRateLimit" : 1000

                }]
            }
        }

    }
}

【问题讨论】:

  • 您能否详细说明生成的基于 CloudFormation 的 API 到底有什么不同?
  • 生成的堆栈没有Cache StatusChecked,没有CacheCapacityCache time-to-live 的设置。另外,我不知道如何设置Per-Key cache invalidation
  • 看起来您缺少阶段定义中的 CacheCluster 定义(除了方法设置)。至于每个密钥的失效,我在 cloudformation 中似乎没有这个选项。
  • @EmAe,你有没有想过如何做到这一点?
  • 不。我猜它目前不被 CF 支持

标签: aws-api-gateway amazon-cloudformation


【解决方案1】:

在每个阶段的属性中需要设置"CacheClusterEnabled": true,即:

"TestStage" : {
        "Type" : "AWS::ApiGateway::Stage",
        "Condition":"CreateTestStage",
        "Properties" : {
            "DeploymentId" : {"Ref":"ApiDeployment"},
            "Description" : "Test Stage",
            "RestApiId" : {"Ref":"MyAPI"},
            "StageName" : "test",
            "CacheClusterEnabled": "true"
        }
    },

这里是文档: API 网关阶段 (CacheClusterEnabled): http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-stage.html#cfn-apigateway-stage-cacheclusterenabled

API Gateway MethodSetting(注意说缓存必须先在舞台上启用): http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-stage-methodsetting.html#cfn-apigateway-stage-methodsetting-cachingenabled

【讨论】:

    猜你喜欢
    • 2020-10-19
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 2012-11-15
    • 2017-11-12
    • 2020-02-22
    • 1970-01-01
    • 2021-01-11
    相关资源
    最近更新 更多