2017-02-26 53 views
0

我想使用CloudFormation來設置APIGateway。我有模板準備好默認階段,但我想創建基於輸入參數(將在CF UI中創建堆棧時輸入)的階段testprod根據參數名稱創建階段

如果輸入參數是prod我想創建不同的舞臺burst,caching和其他屬性。

如果輸入參數是test,我想保留所有的默認值。

我知道如何在輸入參數中提供,並在下拉列表中只提供test & prod。但我如何使CF模板中的if/else塊自定義我的階段?

回答

0

在CloudFormation中,您使用Conditions來執行此操作。這裏有一個模板片斷,將設置CacheClusterEnabledtrueThrottlingBurstLimit僅當EnvType設置爲prod999

Parameters: 
    EnvType: 
    Description: Environment type. 
    Default: test 
    Type: String 
    AllowedValues: 
     - prod 
     - test 
    ConstraintDescription: must specify prod or test. 
Conditions: 
    ProdEnv: !Equals [ !Ref EnvType, prod ] 
Resources: 
    Stage: 
    Type: AWS::ApiGateway::Stage 
    Properties: 
     CacheClusterEnabled: !If [ProdEnv, true, !Ref "AWS::NoValue"] 
     MethodSettings: 
     - 
      ResourcePath: "/" 
      HttpMethod: "GET" 
      ThrottlingBurstLimit: !If [ProdEnv, 999, !Ref "AWS::NoValue"] 
     # ... 

注意AWS::NoValue防止在Fn::If功能使用時被設置爲所有的財產,所以默認將在EnvType不是prod時使用。

+0

謝謝。假設我設置了這個條件''Conditions「:{」CreateProdStage「:{」Fn :: Equals「:[{」Ref「:」EnvType「},」prod「]}},現在我想創建「測試」階段。我怎樣才能否定** CreateProdStage條件 –

+0

要否定條件,您可以:1.交換Fn :: If數組中的第二個(true)和第三個(false)值; 2.使用['Fn :: Not'](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-not)創建單獨的反向條件:'NotCreateProdStage:!Not [Condition:CreateProdStage]'。 – wjordan

+0

我用''條件「:{」Fn :: Not「:[{」Condition「:」CreateProdStage「}]},'得到錯誤說'模板驗證錯誤:模板格式錯誤:每個Condition成員必須是string.' –