简体   繁体   中英

CloudFormation conditional EMR instances

I struggle a bit with the use of conditionals in CF templates, and I would like to conditionally specify EMR cluster instances groups or fleets in the most concise way.

This builds w/o error. It chooses to use instance groups if prod, or instance fleets if non-prod using two separate conditionals:

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" 

  

I would like to use just a single conditional, like below, except the build fails telling me 'YAML not well-formed' on the line where the 'If' is. If I implement it like above, I would end up having four separate conditionals since I also have to add master instance groups or fleets as well. Is is possible to do it like this all as one conditional?

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   

Instances is not a list . You don't need - before !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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM