amazon-web-services - CloudFormation 条件 EMR 实例

标签 amazon-web-services conditional-statements aws-cloudformation instance

我在 CF 模板中使用条件语句时遇到了一些麻烦,我想以最简洁的方式有条件地指定 EMR 集群实例组或队列。

此构建没有错误。它选择使用实例组(如果是产品)或实例队列(如果非产品)使用两个单独的条件:

Parameters:
  EnvironmentName:
    Type: String
    Description: 'Example: ci, qa, stage, prod'
      
Conditions:
  IsPreProd: !Or
    [!Equals [!Ref EnvironmentName, ci], !Equals [!Ref EnvironmentName, qa]]
  IsProd: !Or
    [!Equals [!Ref EnvironmentName, stage], !Equals [!Ref EnvironmentName, prod]]
    
Resources:
  EMRCluster:
    Type: 'AWS::EMR::Cluster'
    Properties:
      Instances:
        CoreInstanceGroup:
          !If 
            - IsProd
            - InstanceCount: 1
              InstanceType: m5.8xlarge
              Market: ON_DEMAND
              Name: CoreInstance
            - !Ref "AWS::NoValue"     
        CoreInstanceFleet:
          !If 
            - IsPreProd
            - InstanceTypeConfigs:
                - InstanceType: m5.8xlarge
              TargetOnDemandCapacity: 1
              TargetSpotCapacity: 1
              LaunchSpecifications:
                SpotSpecification:
                  TimeoutAction: SWITCH_TO_ON_DEMAND
                  TimeoutDurationMinutes: 10
            - !Ref "AWS::NoValue" 

  

我只想使用一个条件,如下所示,除非构建失败,告诉我在“If”所在的行上“YAML 格式不正确”。如果我像上面那样实现它,我最终会得到四个单独的条件,因为我还必须添加主实例组或队列。是否可以将这一切作为一个条件来完成?

Parameters:
  EnvironmentName:
    Type: String
    Description: 'Example: ci, qa, stage, prod'
      
Conditions:
  IsProd: !Or
    [!Equals [!Ref EnvironmentName, stage], !Equals [!Ref EnvironmentName, prod]]
    
Resources:
  EMRCluster:
    Type: 'AWS::EMR::Cluster'
    Properties:
      Instances:
        - !If 
            - IsProd
            - CoreInstanceGroup:
                InstanceCount: 1
                InstanceType: m5.8xlarge
                Market: ON_DEMAND
                Name: CoreInstance
            - CoreInstanceFleet:
                InstanceTypeConfigs:
                  - InstanceType: m5.8xlarge
                TargetOnDemandCapacity: 1
                TargetSpotCapacity: 1
                LaunchSpecifications:
                  SpotSpecification:
                    BlockDurationMinutes: 60
                    TimeoutAction: SWITCH_TO_ON_DEMAND
                    TimeoutDurationMinutes: 10   

最佳答案

实例不是列表!If 之前不需要 -:

Resources:
  EMRCluster:
    Type: 'AWS::EMR::Cluster'
    Properties:
      Instances:
         !If 
            - IsProd
            - CoreInstanceGroup:
                InstanceCount: 1
                InstanceType: m5.8xlarge
                Market: ON_DEMAND
                Name: CoreInstance
            - CoreInstanceFleet:
                InstanceTypeConfigs:
                  - InstanceType: m5.8xlarge
                TargetOnDemandCapacity: 1
                TargetSpotCapacity: 1
                LaunchSpecifications:
                  SpotSpecification:
                    BlockDurationMinutes: 60
                    TimeoutAction: SWITCH_TO_ON_DEMAND
                    TimeoutDurationMinutes: 10  

关于amazon-web-services - CloudFormation 条件 EMR 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73736287/

相关文章:

Linux Bash函数条件执行用户输入

amazon-web-services - 如何使用Cloudformation创建KMS非对称签名 key 资源?

amazon-web-services - Base64解码编码,得到不同的数据

amazon-web-services - 向尚不存在的 IAM 角色授予跨账户 Secretsmanager 访问权限

logic - 连取范式的书写条件

conditional-statements - 字符串参数长度的Cloudformation条件

amazon-web-services - 使用 ALB 创建 Elastic Beanstalk 应用程序时覆盖运行状况检查端口

amazon-web-services - AWS-SAM:如何重新使用 Route53 域而不是重新创建它?

amazon-web-services - 为什么 Lambda 需要权限才能完成生命周期 Hook ?

linux - 从 ec2-user 以外的用户通过 ftp/ssh 访问 ec2 实例