首页 文章

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

提问于
浏览
2

我使用 CloudFormation 设置了 APIGateway ,它将一个方法暴露为 /customers/{customerId} . 该方法调用 dynamodb service而不是使用任何lambda,如果缺少映射,则返回带有空对象的 HTTP 200 或带有 HTTP 200customer 对象 .

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

Cache Settings

缓存状态:可用

缓存容量:0.5GB

缓存生存时间(TTL):300

Per-key cache invalidation

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

Default Method Throttling

启用限制:已选中

费率: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 回答

相关问题