title: How to limit the number of spot instace pools in an ASG?
body: <p>I'm setting up an auto scaling group (ASG) with a <a href="https://aws.amazon.com/blogs/aws/new-ec2-auto-scaling-groups-with-multiple-instance-types-purchase-options/" rel="nofollow noreferrer">mixed instances policy</a> to use multiple spot instance types. How do I limit the number of spot instance pools used by my ASG?</p>
  
  <p><a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-spot-instances.html" rel="nofollow noreferrer">Spot Instance pools</a> are defined as follows:</p>
  
  <blockquote>
    <p>A set of unused EC2 instances with the same instance type (for example, <code>m5.large</code>), operating system, Availability Zone, and network platform.</p>
  </blockquote>
  
  <p>I understand that in my case, a spot instance pool is basically a distinct pair of availability zone and instance type.</p>
  
  <p>My CloudFormation template creates an auto scaling group of 16 instances with a mixed instances policy. It uses four instance types and all availability zones. The test region <code>us-west-2</code> has four availability zones. In theory, the group should be able to use up to sixteen spot isntance pools.</p>
  
  <p>The stack's <code>SpotInstancePools</code> parameter sets the eponymous property of the ASG. I've tried setting it to various values, but it doesn't seem to directly control the number of spot instance pools used by the ASG.</p>
  
  <p>CloudFormation template:</p>
  
  <pre><code>AWSTemplateFormatVersion: '2010-09-09'
  Description: Testing mixed instance policies
  
  Parameters:
  
    SpotInstancePools:
      Type: Number
      Default: 1
      Description: The ASG's number of spot instance pools.
  
    ImageId:
      Type: AWS::EC2::Image::Id
      Default: ami-061392db613a6357b
      Description: Launch template's AMI. Defaults to Amazon Linux 2.
  
  Resources:
  
    AutoScalingGroup:
      Type: AWS::AutoScaling::AutoScalingGroup
      Properties:
        AvailabilityZones: !GetAZs
        MaxSize: 16
        MinSize: 16
        MixedInstancesPolicy:
          InstancesDistribution:
            OnDemandAllocationStrategy: prioritized
            OnDemandBaseCapacity: 0
            OnDemandPercentageAboveBaseCapacity: 0
            SpotAllocationStrategy: lowest-price
            SpotInstancePools: !Ref SpotInstancePools
            SpotMaxPrice: ''
          LaunchTemplate:
            LaunchTemplateSpecification:
              LaunchTemplateId: !Ref LaunchTemplate
              Version: !GetAtt LaunchTemplate.LatestVersionNumber
            Overrides:
              - InstanceType: t2.small
              - InstanceType: t3.small
              - InstanceType: t2.medium
              - InstanceType: t3.medium
  
    LaunchTemplate:
      Type: AWS::EC2::LaunchTemplate
      Properties:
        LaunchTemplateData:
          ImageId: !Ref ImageId
  </code></pre>
  
  <p>Command to create stack <code>mixed-instances-policy-test-1</code>, which has a SpotInstancePools count of 1:</p>
  
  <pre><code>aws cloudformation create-stack \
  --stack-name mixed-instances-policy-test-1 \
  --template-body file://mixed-instances-policy.yaml \
  --parameters ParameterKey=SpotInstancePools,ParameterValue=1 \
  --region us-west-2 \
  --profile test
  </code></pre>
  
  <p>Command to create stack <code>mixed-instances-policy-5</code>, which has a SpotInstancePools count of 5:</p>
  
  <pre><code>aws cloudformation create-stack \
  --stack-name mixed-instances-policy-test-5 \
  --template-body file://mixed-instances-policy.yaml \
  --parameters ParameterKey=SpotInstancePools,ParameterValue=5 \
  --region us-west-2 \
  --profile test
  </code></pre>
  
  <p>Command to list the number of unique spot instance pools used (replace name of stack as appropriate):</p>
  
  <pre><code>aws ec2 describe-instances \
  --filters 'Name=tag:aws:cloudformation:stack-name,Values=mixed-instances-policy-test-1' \
  --query 'Reservations[].Instances[].[InstanceType, Placement.AvailabilityZone]' \
  --output text \
  --region us-west-2 \
  --profile test |
  sort |
  uniq --count
  </code></pre>
  
  <p>After waiting for each stack to finish creating, I check the number of unique spot instance pools.</p>
  
  <p>Where <code>SpotInstancePools</code> is set to <code>1</code>, I see 3 unique pools.</p>
  
  <pre><code>      5 t2.small    us-west-2a
        5 t3.small    us-west-2b
        6 t3.small    us-west-2c
  </code></pre>
  
  <p>Where <code>SpotInstancePools</code> is set to <code>5</code>, I see 11 unique pools.</p>
  
  <pre><code>      2 t2.medium   us-west-2a
        1 t2.medium   us-west-2b
        1 t2.medium   us-west-2c
        2 t2.small    us-west-2a
        2 t2.small    us-west-2b
        1 t2.small    us-west-2c
        1 t3.medium   us-west-2a
        1 t3.medium   us-west-2b
        1 t3.medium   us-west-2c
        2 t3.small    us-west-2b
        2 t3.small    us-west-2c
  </code></pre>
  
  <p>In each case, I expect the number of pools to equal the parameter value.</p>