aws-api-gateway - 使用 CloudFormation 为 APIGateway 设置阶段环境(缓存)

标签 aws-api-gateway aws-cloudformation

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

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

缓存设置

缓存状态:可用

缓存容量:0.5GB

缓存生存时间 (TTL):300

每键缓存失效

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

默认方法限制

启用节流:选中

价格:1000

爆发:200

我尝试像这样设置第一部分(缓存设置),但它没有像我预期的那样产生所需的产品阶段设置。我如何使用 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

                }]
            }
        }

    }
}

最佳答案

在每个阶段的属性中需要设置"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

关于aws-api-gateway - 使用 CloudFormation 为 APIGateway 设置阶段环境(缓存),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42514937/

相关文章:

aws-lambda - Snowflake 的异步外部函数不遵守 HttpStatus 429

amazon-web-services - 如何在 CloudFormation 中使用 ECS PlacementConstraints

amazon-web-services - 云形成模板: 403 while files are copied from private S3

php - Beanstalk 上示例 PHP 应用程序的 AWS CloudFormation 模板失败

aws-cloudformation - 是否可以重用cloudformation模板中的现有路由表

amazon-web-services - 如何将Cognito身份池与API网关集成?

amazon-web-services - 对来自 API 网关的查询参数进行编码

amazon-web-services - 允许使用 Cognito 访问 S3 上的自定义前缀

javascript - 修复 AWS API 网关和放大中不存在的 CORS "Response to preflight..." header

amazon-web-services - 在不同区域创建 Amazon Kinesis 数据生成器堆栈