aws-api-gateway - 根据参数名称创建阶段

标签 aws-api-gateway aws-cloudformation

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

如果输入参数是prod,我想创建具有不同突发缓存和其他属性的阶段。

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

我知道如何接受输入参数并在下拉列表中仅提供 testprod 。但是我如何在 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/

相关文章:

amazon-web-services - 如何使用 Cloudformation 更新 Athena 输出位置

node.js - 如何使用 aws-sdk 启用所需的 apiKey 为 true?

aws-lambda - AWS API Gateway 自定义授权程序未运行

c# - AWS Lambda C# CloudFormation 配置

amazon-web-services - 在 SAM 部署期间检测到 AWS SAM 模板验证错误。伪参数 ${AWS::AccountId} 不接受

amazon-dynamodb - 在 Cloudformation 中创建 DynamoDB 表失败

amazon-web-services - 映射模板的默认内容类型

amazon-web-services - 从 AWS API Gateway Web 界面配置 AWS Lambda 时无法选择/查看 Lambda 函数

amazon-web-services - 如何通过 API Gateway 调用 AWS Step Functions?

amazon-web-services - EC2 CloudFormation 中的卷标签和 block 设备映射标签有什么区别