根据参数名称创建阶段

问题描述:

我想使用CloudFormation来设置APIGateway。我有模板准备好默认阶段,但我想创建基于输入参数(将在CF UI中创建堆栈时输入)的阶段testprod根据参数名称创建阶段

如果输入参数是prod我想创建不同的舞台burst,caching和其他属性。

如果输入参数是test,我想保留所有的默认值。

我知道如何在输入参数中提供,并在下拉列表中只提供test & prod。但我如何使CF模板中的if/else块自定义我的阶段?

在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.' –