2017-02-28 45 views
2

我已經使用CloudFormation設置了APIGateway,它暴露了一種方法/customers/{customerId}。該方法調用dynamodb服務,而不是使用任何lambda,如果映射丟失或customer對象與HTTP 200發送回HTTP 200空對象。使用CloudFormation爲APIGateway設置舞臺環境(緩存)

現在,我想爲我的prod階段設置緩存。我有一個使用AWS APIGateway Web UI創建的現有API,並創建了prod階段。我想在CF中複製這些屬性。以下是我在我的舊的API

緩存設置

緩存狀態:免費

緩存容量:0.5GB

緩存時間生存(TTL):300

每鍵緩存失效

需要授權:已檢查 處理未經授權的請求:忽略緩存控制頭;添加預警響應頭

默認方法節流

啓用限制:檢查

率: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 

       }] 
      } 
     } 

    } 
} 
+0

您能否詳細說明在基於CloudFormation的API中完全不同? –

+0

生成的堆棧沒有'Cache Status'作爲'Checked','CacheCapacity'和'Cache time-live'沒有設置。此外,我無法找出otu如何設置「每鍵高速緩存失效」 –

+0

看起來您缺少舞臺定義中的CacheCluster定義(除了方法設置)。至於每個關鍵的失效,我似乎沒有在雲端的選項。 –

回答

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網關MethodSetting (注意緩存必須先在舞臺上啓用): http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-stage-methodsetting.html#cfn-apigateway-stage-methodsetting-cachingenabled