使用CloudFormation为APIGateway设置Stage环境(缓存)

时间:2017-02-28 17:16:22

标签: aws-api-gateway amazon-cloudformation

我使用APIGateway设置CloudFormation,其中一个方法公开为/customers/{customerId}。该方法调用dynamodb服务而不是使用任何lambda,如果缺少映射,则使用空对象发回HTTP 200或使用customer发送HTTP 200对象。

现在,我想为我的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

                }]
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

在每个阶段的属性中,您需要设置"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网关方法设置(注意,首先必须在舞台上启用缓存): http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-stage-methodsetting.html#cfn-apigateway-stage-methodsetting-cachingenabled