Lab · Task 4.2 · about 45 minutes
Mix On-Demand and Spot, Then Hibernate on Purpose
Services in this lab
- Amazon EC2in scope
- Amazon EC2 Auto Scalingin scope
- AWS CLIin scope
Lesson for this lab: Cost-Optimized Compute
Goal
Build an Auto Scaling group that keeps an On-Demand floor and runs the rest on Spot, schedule it down to zero, and see EC2 refuse to hibernate an instance that wasn't launched for hibernation.
Before you start
- An AWS account you can experiment in. Use a sandbox account with a budget alert, never a production account.
- Sign in as an IAM Identity Center user or an IAM role that can manage EC2 instances, launch templates and Auto Scaling groups. Don't use the root user.
- AWS CLI v2 configured for that identity (
aws sts get-caller-identityreturns your role, not root). - A default VPC in your chosen Region, with at least two default subnets. Pick one Region and use it throughout; the commands use
<region>where it matters.
The exam point of view: a cost-effective fleet is a layered one. A guaranteed floor stays on On-Demand (the part a Savings Plan would cover), everything above it runs on Spot, non-production goes to zero at night, and hibernation lets a slow-starting instance stop without losing its warmed-up memory. In this lab you build the layers and watch the hibernation trap happen: the setting has to be there at launch.
Resources you will create (all deleted in Clean up):
- One launch template,
lab-compute-cost, for small Graviton (arm64) instances running Amazon Linux 2023. - One Auto Scaling group,
lab-compute-cost-asg, with a mixed instances policy: one On-Demand instance as the base and Spot above it. - Two scheduled actions on that group.
- Two standalone EC2 instances:
lab-no-hibernate(normal launch) andlab-hibernate(hibernation enabled, encrypted root volume), each with its EBS root volume.
No instance gets a public-facing application, and no security group rule is opened; the instances only need to boot.
Steps
0 of 7 steps done
The template uses a public Systems Manager parameter instead of a fixed AMI ID, so it always resolves to the current Amazon Linux 2023 arm64 image in your Region.
Save this as
lt.json:{ "ImageId": "resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-arm64", "InstanceType": "t4g.small", "TagSpecifications": [ { "ResourceType": "instance", "Tags": [{ "Key": "lab", "Value": "compute-cost" }] }, { "ResourceType": "volume", "Tags": [{ "Key": "lab", "Value": "compute-cost" }] } ] }aws ec2 create-launch-template \ --launch-template-name lab-compute-cost \ --launch-template-data file://lt.jsonConsole equivalent: EC2 > Launch Templates > Create launch template. Under Application and OS Images, choose Specify custom value/Systems Manager parameter and enter
resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-arm64. Chooset4g.small, add the tag, leave everything else at its default, and create it.List two default subnets in different Availability Zones:
aws ec2 describe-subnets --filters Name=default-for-az,Values=true \ --query "Subnets[].[SubnetId,AvailabilityZone]" --output tableSave this as
asg.json, replacing the two subnet IDs:{ "AutoScalingGroupName": "lab-compute-cost-asg", "MixedInstancesPolicy": { "LaunchTemplate": { "LaunchTemplateSpecification": { "LaunchTemplateName": "lab-compute-cost", "Version": "$Default" }, "Overrides": [ { "InstanceType": "t4g.small" }, { "InstanceType": "t4g.medium" }, { "InstanceType": "m7g.medium" }, { "InstanceType": "c7g.medium" } ] }, "InstancesDistribution": { "OnDemandBaseCapacity": 1, "OnDemandPercentageAboveBaseCapacity": 0, "SpotAllocationStrategy": "price-capacity-optimized" } }, "CapacityRebalance": true, "MinSize": 0, "MaxSize": 3, "DesiredCapacity": 3, "VPCZoneIdentifier": "<subnet-id-1>,<subnet-id-2>" }aws autoscaling create-auto-scaling-group --cli-input-json file://asg.jsonConsole equivalent: EC2 > Auto Scaling Groups > Create Auto Scaling group, choose the
lab-compute-costtemplate, then under Instance type requirements choose Override launch template and add the four types. Under Instance purchase options, set On-Demand base capacity to 1, On-Demand above base to 0 percent, choose Price capacity optimized and turn on Capacity rebalance. Pick the two subnets and set desired 3, min 0, max 3.MinSizeis 0 on purpose: a group whose minimum is above zero can't be scheduled down to zero without also lowering the minimum.Wait two or three minutes, then run:
aws ec2 describe-instances \ --filters Name=tag:aws:autoscaling:groupName,Values=lab-compute-cost-asg \ Name=instance-state-name,Values=pending,running \ --query "Reservations[].Instances[].[InstanceId,InstanceType,Placement.AvailabilityZone,InstanceLifecycle]" \ --output tableOne row shows
Nonein the last column (the On-Demand base) and two showspot. The Spot instances may be different types and in different Availability Zones: that is the allocation strategy picking the pools that are cheap and least likely to be interrupted. If Spot capacity is short in your Region, the group keeps trying; the On-Demand base is still there, which is the point of having one.Add a recurring action that empties the group every weekday evening, and one that brings it back in the morning. Replace the time zone with yours.
aws autoscaling put-scheduled-update-group-action \ --auto-scaling-group-name lab-compute-cost-asg \ --scheduled-action-name evening-scale-in \ --recurrence "0 19 * * 1-5" --time-zone "Europe/London" \ --min-size 0 --desired-capacity 0 aws autoscaling put-scheduled-update-group-action \ --auto-scaling-group-name lab-compute-cost-asg \ --scheduled-action-name morning-scale-out \ --recurrence "0 7 * * 1-5" --time-zone "Europe/London" \ --desired-capacity 3Console equivalent: select the group, open the Automatic scaling tab, and under Scheduled actions choose Create scheduled action twice, with Recurrence set to Cron.
You don't need to wait for 19:00. To see the effect now, set the desired capacity to 0 by hand:
aws autoscaling set-desired-capacity \ --auto-scaling-group-name lab-compute-cost-asg --desired-capacity 0Launch a standalone instance from the same template, as most people do, without thinking about hibernation:
aws ec2 run-instances --launch-template LaunchTemplateName=lab-compute-cost \ --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=lab-no-hibernate}]' \ --query "Instances[0].InstanceId" --output textNote the instance ID. When it is
running, try to hibernate it. In the console, selectlab-no-hibernateand open Instance state.EXPECTED FAILURE: Hibernate instance is disabled for this instance, while Stop instance is still offered. A plain stop would discard everything in memory. The CLI shows why:
aws ec2 describe-instances --instance-ids <no-hibernate-instance-id> \ --query "Reservations[0].Instances[0].HibernationOptions"The output is
{"Configured": false}. Hibernation is set when an instance is launched, so this instance can't be hibernated; launch a new one with hibernation enabled. This is the trap from the lesson.Hibernation needs three things at launch: the hibernation option, an EBS root volume that is encrypted, and enough room on that volume for the instance's RAM. A
t4g.smallhas 2 GiB of RAM, so a 12 GiB root volume is plenty.Save this as
mapping.json(/dev/xvdais the root device name of the Amazon Linux 2023 AMI):[ { "DeviceName": "/dev/xvda", "Ebs": { "VolumeSize": 12, "VolumeType": "gp3", "Encrypted": true } } ]aws ec2 run-instances --launch-template LaunchTemplateName=lab-compute-cost \ --block-device-mappings file://mapping.json \ --hibernation-options Configured=true \ --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=lab-hibernate}]' \ --query "Instances[0].InstanceId" --output textConsole equivalent: Launch instance from template, then under Configure storage choose Advanced, set the root volume to 12 GiB gp3 with Encrypted set to Yes, and under Advanced details set Stop - Hibernate behavior to Enable.
Wait until the instance's status checks pass, then run:
aws ec2 stop-instances --instance-ids <hibernate-instance-id> --hibernate aws ec2 wait instance-stopped --instance-ids <hibernate-instance-id> aws ec2 start-instances --instance-ids <hibernate-instance-id>While it is hibernated, the instance is in the
stoppedstate and its RAM contents sit on the encrypted root volume. When it starts, the memory is reloaded and its processes resume, which is what makes hibernation useful for applications with a long warm-up.
Verify
-
The group has the layered purchase options and both schedules:
aws autoscaling describe-auto-scaling-groups \ --auto-scaling-group-names lab-compute-cost-asg \ --query "AutoScalingGroups[0].[MixedInstancesPolicy.InstancesDistribution,CapacityRebalance,DesiredCapacity]" aws autoscaling describe-scheduled-actions \ --auto-scaling-group-name lab-compute-cost-asg \ --query "ScheduledUpdateGroupActions[].[ScheduledActionName,Recurrence,DesiredCapacity]" --output tableExpected:
OnDemandBaseCapacity1,OnDemandPercentageAboveBaseCapacity0,SpotAllocationStrategyprice-capacity-optimized,truefor Capacity Rebalancing, desired capacity 0, and two scheduled actions (evening-scale-inwith desired 0,morning-scale-outwith desired 3). -
The group really scaled in: the
describe-instancescommand from step 3 returns no running instances after a few minutes. -
Only one standalone instance is enabled for hibernation:
aws ec2 describe-instances \ --filters Name=tag:Name,Values=lab-no-hibernate,lab-hibernate \ --query "Reservations[].Instances[].[Tags[?Key=='Name']|[0].Value,HibernationOptions.Configured,State.Name]" \ --output tableExpected:
lab-no-hibernateshowsFalseandrunning;lab-hibernateshowsTrueandrunningagain after its restart.
Clean up
Delete everything in dependency order: the group and its instances first, then the standalone instances, then the launch template that both used.
-
Delete the Auto Scaling group.
--force-deleteterminates any instances it still has, and its scheduled actions are deleted with it:aws autoscaling delete-auto-scaling-group \ --auto-scaling-group-name lab-compute-cost-asg --force-deleteIn the console: select the group and choose Actions > Delete.
-
Terminate both standalone instances. Their root volumes are deleted on termination by default, including the volume that held the hibernated RAM:
aws ec2 terminate-instances \ --instance-ids <no-hibernate-instance-id> <hibernate-instance-id> aws ec2 wait instance-terminated \ --instance-ids <no-hibernate-instance-id> <hibernate-instance-id> -
Delete the launch template:
aws ec2 delete-launch-template --launch-template-name lab-compute-cost -
Delete the local files
lt.json,asg.jsonandmapping.json. -
Check that nothing remains:
aws autoscaling describe-auto-scaling-groups \ --auto-scaling-group-names lab-compute-cost-asg \ --query "AutoScalingGroups[].AutoScalingGroupName" aws ec2 describe-instances --filters Name=tag:lab,Values=compute-cost \ Name=instance-state-name,Values=pending,running,stopping,stopped \ --query "Reservations[].Instances[].InstanceId" aws ec2 describe-volumes --filters Name=tag:lab,Values=compute-cost Name=status,Values=available \ --query "Volumes[].VolumeId"Expected:
[]for the group and the instances (the group can take a minute to disappear), and no unattached volumes taggedlab=compute-costleft over from this lab. Filtering by that tag matters:describe-volumeswith onlystatus=availablelists every unattached volume in the account, not just this lab's. In the EC2 console, Spot Requests should show no open request from this lab. If one is still open, cancel it.
Sources
- https://docs.aws.amazon.com/autoscaling/ec2/userguide/create-mixed-instances-group-manual-instance-type-selection.html
- https://docs.aws.amazon.com/autoscaling/ec2/userguide/allocation-strategies.html
- https://docs.aws.amazon.com/autoscaling/ec2/userguide/ec2-auto-scaling-capacity-rebalancing.html
- https://docs.aws.amazon.com/autoscaling/ec2/userguide/scheduled-scaling-create-scheduled-action.html
- https://docs.aws.amazon.com/autoscaling/ec2/userguide/using-systems-manager-parameters.html
- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/hibernating-prerequisites.html
- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enabling-hibernation.html
- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/hibernating-instances.html
- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/troubleshoot-instance-hibernate.html
Last reviewed