AWS_EN

Mix on-demand/spot instances in EC2 Auto Scaling Group

The following text describes techniques to lower AWS costs.

AWSコスト最適化ガイドブック

One of the methods introduced is spot instances.

スポットインスタンスは、単体のAmazonEC2インスタンスとしても起動できますが、ほとんどのユースケースではAmazonEC2AutoScalingグループで管理するのがおすすめです。AmazonEC2AutoScalingサービスは、負荷に応じてインスタンスの台数を自動的に増減する仕組みですが、その手前に重要な機能があります。それは、稼働する台数の自動維持です。

AWSコスト最適化ガイドブック

And in an EC2 Auto Scaling group, it is possible to configure a mix of on-demand and spot instances.

You can launch and automatically scale a fleet of On-Demand Instances and Spot Instances within a single Auto Scaling group.

Auto Scaling groups with multiple instance types and purchase options

This time we will create an EC2 Auto Scaling group consisting of on-demand and spot instances.

Environment

Diagram of mix on-demand/spot instances in EC2 Auto Scaling Group.

Create an EC2 Auto Scaling group in a private subnet.
The OS for the instances created in the group will be the latest Amazon Linux 2.

Fix the instance size to the group to 2.
In other words, set all desired/minimum/maximum numbers to 2.
And do not set the scaling policy.

Of the two instances, one is an on-demand instance.
And the other as a spot instance.

Place ALBs on all sides of the EC2 Auto Scaling group.

CloudFormation template files

The above configuration is built with CloudFormation.
The CloudFormation templates are placed at the following URL

awstut-fa/139 at main · awstut-an-r/awstut-fa
Contribute to awstut-an-r/awstut-fa development by creating an account on GitHub.

Explanation of key points of template files

This page covers how to create an EC2 Auto Scaling group with a mix of on-demand and spot instances.

For basic information on EC2 Auto Scaling, please refer to the following pages.

Launch Template

Resources:
  LaunchTemplate:
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateData:
        IamInstanceProfile:
          Arn: !GetAtt InstanceProfile.Arn
        ImageId: !Ref ImageId
        InstanceType: !Ref InstanceType1
        SecurityGroupIds: 
          - !Ref InstanceSecurityGroup
        UserData: !Base64 |
          #!/bin/bash -xe
          yum update -y
          yum install -y httpd
          systemctl start httpd
          systemctl enable httpd
          ec2-metadata -i > /var/www/html/index.html
      LaunchTemplateName: !Sub "${Prefix}-LaunchTemplate"
Code language: YAML (yaml)

No special settings are required.

Specify t3a.nano as the instance type.

Define the initialization process with user data.
The process is, install Apache and prepare index.html.

Auto Scaling Group

Resources:
  AutoScalingGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
      AutoScalingGroupName: !Sub "${Prefix}-AutoScalingGroup"
      DesiredCapacity: !Ref DesiredCapacity
      MaxSize: !Ref MaxSize
      MinSize: !Ref MinSize
      MixedInstancesPolicy:
        InstancesDistribution: 
          OnDemandAllocationStrategy: prioritized
          OnDemandBaseCapacity: 1
          OnDemandPercentageAboveBaseCapacity: 0
          SpotAllocationStrategy: price-capacity-optimized
        LaunchTemplate: 
          LaunchTemplateSpecification: 
            LaunchTemplateId: !Ref LaunchTemplate
            Version: !GetAtt LaunchTemplate.LatestVersionNumber
          Overrides: 
            - InstanceType: !Ref InstanceType1
            - InstanceType: !Ref InstanceType2
            - InstanceType: !Ref InstanceType3
      VPCZoneIdentifier:
        - !Ref PrivateSubnet1
        - !Ref PrivateSubnet2
      TargetGroupARNs:
        - !Ref ALBTargetGroup
Code language: YAML (yaml)

The key setting for a mixed configuration of on-demand and spot instances is the MixedInstancesPolicy property.

Set the LaunchTemplate property to the launch template identified earlier.

This time, override the instance type.

Overrides are changes you make to the properties of your launch template. Amazon EC2 Auto Scaling supports overrides to the instance type property. That way, you can specify multiple instance types.

Configure overrides

Specify the following three instance types for the Overrides property.

  1. t3a.nano
  2. t3.nano
  3. t2.nano

For on-demand instances, set the following three properties

The first is the OnDemandAllocationStrategy property.
Specify “prioritized” for this property.
The meaning of this is explained below.

When fulfilling On-Demand capacity, Amazon EC2 Auto Scaling determines which instance type to use first based on the order of instance types in the list of launch template overrides.

Allocation strategies

In this case, the instance type with the highest priority is t3a.nano.
So this will be the on-demand instance type chosen.

The second and third are the OnDemandBaseCapacity and OnDemandPercentageAboveBaseCapacity properties.
These are parameters to control the number of on-demand instances. The following page has more details.

Auto Scaling groups with multiple instance types and purchase options - Amazon EC2 Auto Scaling
Learn how to create an Auto Scaling group with Spot and On-Demand Instances and allocation strategies. Allocation strategies determine which instance types the ...

The way it is set up this time, one on-demand instance will always be running.

The SpotAllocationStrategy property is a parameter related to determining the instance type of a spot instance.
In this case, “price-capacity-optimized” is specified for this property.
The meaning of this is explained below.

The price and capacity optimized allocation strategy looks at both price and capacity to select the Spot Instance pools that are the least likely to be interrupted and have the lowest possible price.

Allocation strategies

In this case, the decision will be based on price and capacity availability among the three instance types listed above.

Architecting

Use CloudFormation to build this environment and check its actual behavior.

Create CloudFormation stacks and check the resources in the stacks

Create CloudFormation stacks.
For information on how to create stacks and check each stack, please see the following page.

After reviewing the resources in each stack, information on the main resources created in this case is as follows

  • DNS name for ALB: http://fa-139-ALB-981952103.ap-northeast-1.elb.amazonaws.com
  • ALB target group: fa-139-ALBTargetGroup
  • Launch template: fa-139-LaunchTemplate
  • EC2 Auto Scaling Group: fa-139-AutoScalingGroup

Check the created resource from the AWS Management Console.

Check the Auto Scaling group.

Detail of Auto Scaling 1.

Note the settings regarding the instance type.
In the Launch Template, t3a.nano is specified.
In contrast, the instance type requirement shows that three types are registered, including t3a.nano.
This means that for both on-demand and spot instances, the type will be determined from these three depending on demand conditions and pricing.

The purchase options show that they are set as specified in CloudFormation.
Only one on-demand instance will be created, the rest will be spot instances.
In this configuration, the desired/minimum/maximum number of Auto Scaling groups are all set to 2.
So one on-demand spot instance will be created for each.

Operation Check

Check the EC2 instances created in the Auto Scaling group.

First, check the group’s activity.

Detail of Auto Scaling 2.

You can see that two instances were created in the group.

Check the created instance.

Detail of Auto Scaling 3.

Note the instance type.
One is t3a.nano and the other is t3.nano.
Originally, t3a.nano should be selected according to the Launch Template.
But according to EC2 demand conditions and prices, another instance type listed in the instance type requirements was chosen.

Check the details of the instance (i-0a1472ccedc0518af) on the t3a.nano person.

Detail of EC2 1.

Looking at Lifecycle, it says “normal”.
That means this instance is an on-demand instance.
And the instance type for the on-demand instance is t3a.nano as per the Launch Template.

Then check the details of the instance (i-071d637777a0e0823) on the t3.nano side.

Detail of EC2 2.

Looking at the Lifecycle, it says “spot”.
This means that this instance is a spot instance.
This means that the instance type for the spot instance is not the one specified in the Launch Template, but another type listed in the instance requirements.

In the meantime, let’s access the ALB to which this Auto Scaling group is attached.

$ curl http://fa-139-ALB-981952103.ap-northeast-1.elb.amazonaws.com
instance-id: i-0a1472ccedc0518af

$ curl http://fa-139-ALB-981952103.ap-northeast-1.elb.amazonaws.com
instance-id: i-071d637777a0e0823
Code language: Bash (bash)

I am able to access the two instances through ALB.
In other words, it works fine even when configured to mix on-demand and spot instances in one Auto Scaling group.

Summary

We have created an EC2 Auto Scaling group consisting of on-demand and spot instances.

タイトルとURLをコピーしました