根据参数名称创建阶段

Creating stages based on parameter name

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

如果输入参数是 prod 我想创建具有不同 burstcaching 和其他属性的舞台。

如果输入参数是test,我想保持默认。

我知道如何接受输入参数并在下拉列表中只提供 testprod。但是我如何在 CF 模板中制作 if/else 块来自定义我的阶段?

在 CloudFormation 中,您可以使用 Conditions 执行此操作。这是一个模板片段,仅当 EnvType 设置为 prod 时,才会将 CacheClusterEnabled 设置为 true 并将 ThrottlingBurstLimit 设置为 999:

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 prevents the property from being set at all when used in an Fn::If 函数,因此当 EnvType 不是 prod 时将使用默认值。