我想使用 CloudFormation 设置 APIGateway。我已准备好默认阶段的模板,但我想根据输入参数(将在 CF UI 中创建堆栈时输入)创建阶段 test
或 prod
。
如果输入参数是prod
,我想创建具有不同突发
、缓存
和其他属性的阶段。
如果输入参数是test
,我想保留所有默认值。
我知道如何接受输入参数并在下拉列表中仅提供 test
和 prod
。但是我如何在 CF 模板中制作 if
/else
block 来自定义我的阶段?
最佳答案
在 CloudFormation 中,您可以使用 Conditions 来执行此操作。以下是一个模板片段,仅当 EnvType
时,才会将 CacheClusterEnabled
设置为 true
,并将 ThrotdlingBurstLimit
设置为 999
code> 设置为 prod
:
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
时将使用默认值。
关于aws-api-gateway - 根据参数名称创建阶段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42474963/