gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:21:37 24 4
gpt4 key购买 nike

我已经设置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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com