【问题标题】:Creating stages based on parameter name根据参数名称创建阶段
【发布时间】:2017-07-17 10:18:39
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: aws-api-gateway amazon-cloudformation


    【解决方案1】:

    在 CloudFormation 中,您可以使用 Conditions 执行此操作。这是一个模板 sn-p,仅当 EnvType 设置为 prod 时,才会将 CacheClusterEnabled 设置为 trueThrottlingBurstLimit 设置为 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::NoValueFn::If 函数中使用时根本不会设置属性,因此当 EnvType 不是 prod 时将使用默认值。

    【讨论】:

    • 谢谢。所以假设我设置了这个条件"Conditions":{"CreateProdStage" : {"Fn::Equals": [{"Ref":"EnvType"}, "prod"]}},,现在我想创建test 阶段。我如何否定 CreateProdStage 条件
    • 要否定一个条件,您可以: 1. 交换Fn::If 数组中的第二个(真)和第三个(假)值; 2.使用Fn::Not创建单独的逆条件:NotCreateProdStage: !Not [ Condition: CreateProdStage ]
    • 我使用了 "Condition":{"Fn::Not": [{"Condition": "CreateProdStage"}]}, 并得到了错误提示 Template validation error: Template format error: Every Condition member must be a string.
    • 不能在资源的Condition关联中直接使用Fn::Not函数;否定必须定义为单独的条件(在Conditions 部分中),然后您可以将资源中的新条件作为字符串引用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多