Compare instance families and watch an Auto Scaling group replace an instance

short · 35 min · Task statement 3.3

Task

Compare EC2 instance families by what they offer, without launching any of the large ones, then build the smallest possible Auto Scaling group: one free-tier-eligible instance with no public IP address. You will terminate its instance and watch the group launch a replacement on its own, which is the self-healing the lesson describes, and then scale it in to zero before deleting it. At the end you can say which family fits a workload, what minimum, maximum and desired capacity mean, and why a load balancer is a separate job from auto scaling.

Steps

  1. Compare one size from each family. This only reads AWS's catalogue; nothing is launched:
aws ec2 describe-instance-types --instance-types m5.large c5.large r5.large i3.large g4dn.xlarge --query 'InstanceTypes[].[InstanceType,VCpuInfo.DefaultVCpus,MemoryInfo.SizeInMiB,InstanceStorageSupported]' --output table
aws ec2 describe-instance-types --instance-types g4dn.xlarge --query 'InstanceTypes[0].GpuInfo.Gpus[0].Name' --output text

m5 is general purpose, c5 compute optimized, r5 memory optimized (16384 MiB against c5's 4096 for the same 2 vCPUs), i3 storage optimized with local disks, and g4dn accelerated computing (the second command prints T4, its GPU). If a type is not offered in your Region, the command names it; remove it and run again. 2. Find a free-tier-eligible type in this Region:

aws ec2 describe-instance-types --filters Name=free-tier-eligible,Values=true --query 'InstanceTypes[].InstanceType' --output text

If the list includes t3.micro, set TYPE=t3.micro; otherwise set TYPE=t2.micro. Do not pick anything bigger. 3. Write a launch template: the latest Amazon Linux AMI, your free-tier type, and a network interface with no public IP address. The template itself costs nothing:

TYPE=t3.micro
cat > clf-lab14-lt.json <<EOF
{
  "ImageId": "resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64",
  "InstanceType": "$TYPE",
  "NetworkInterfaces": [{"DeviceIndex": 0, "AssociatePublicIpAddress": false}],
  "TagSpecifications": [{"ResourceType": "instance", "Tags": [{"Key": "Name", "Value": "clf-lab14-instance"}]}]
}
EOF
aws ec2 create-launch-template --launch-template-name clf-lab14-lt --launch-template-data file://clf-lab14-lt.json

Change the first line if step 2 told you to use t2.micro. 4. Create the Auto Scaling group across two default subnets, so it spans two Availability Zones. Minimum, maximum and desired capacity are all 1:

SUBNETS=$(aws ec2 describe-subnets --filters Name=default-for-az,Values=true --query "join(',', Subnets[0:2].SubnetId)" --output text)
echo "$SUBNETS"
aws autoscaling create-auto-scaling-group --auto-scaling-group-name clf-lab14-asg --launch-template 'LaunchTemplateName=clf-lab14-lt,Version=$Latest' --min-size 1 --max-size 1 --desired-capacity 1 --vpc-zone-identifier "$SUBNETS"

The echo must show two subnet IDs separated by a comma; if it is empty, see the Notes. Then check every minute or so until the instance is InService:

aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names clf-lab14-asg --query 'AutoScalingGroups[0].Instances[].[InstanceId,AvailabilityZone,LifecycleState]' --output text
  1. Confirm the instance has no public IP address:
aws ec2 describe-instances --filters Name=tag:aws:autoscaling:groupName,Values=clf-lab14-asg Name=instance-state-name,Values=pending,running --query 'Reservations[].Instances[].PublicIpAddress'

It prints []. If it prints an address, go straight to the clean-up. 6. Break it and watch the group heal itself. Copy the instance ID from step 4 and terminate that instance while telling the group to keep its desired capacity:

aws autoscaling terminate-instance-in-auto-scaling-group --instance-id i-0123456789abcdef0 --no-should-decrement-desired-capacity

Put your own instance ID in place of the example. Run the describe-auto-scaling-groups command from step 4 again every minute: within a few minutes a new instance ID appears, possibly in the other Availability Zone, because the desired capacity is still 1. Read the group's own account of what happened:

aws autoscaling describe-scaling-activities --auto-scaling-group-name clf-lab14-asg --query 'Activities[].Description' --output text
  1. Think about what is not here. In a real web application this group would sit behind an Application Load Balancer, which would give users one address, run health checks and send each request only to a healthy instance; the group would register each new instance with it. This lab leaves that piece out because Elastic Load Balancing is charged for every hour a load balancer exists. In your own notes, write which job is auto scaling's (how many instances) and which is the load balancer's (which instance gets each request).

Verify

aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names clf-lab14-asg --query 'length(AutoScalingGroups[0].Instances)'

Expected output: 1, the replacement instance.

aws ec2 describe-instances --filters Name=tag:aws:autoscaling:groupName,Values=clf-lab14-asg Name=instance-state-name,Values=shutting-down,terminated --query 'length(Reservations[].Instances[])'

Expected output: 1, the instance you terminated, which the group replaced without you launching anything.

aws ec2 describe-instances --filters Name=tag:aws:autoscaling:groupName,Values=clf-lab14-asg Name=instance-state-name,Values=pending,running --query 'Reservations[].Instances[].PublicIpAddress'

Expected output: [], no public address.

Cost and clean-up

Charges are possible. Each instance the group runs is a free-tier-eligible type, but it may be charged if this account's Free Tier no longer covers it, and so may its root EBS volume, which is deleted when the instance terminates. There is no public IPv4 address and no load balancer. Launch templates and Auto Scaling groups are not charged for themselves. See the Amazon EC2 pricing page for current terms, and do the clean-up as soon as you have verified.

Created by this lab: the launch template clf-lab14-lt; the Auto Scaling group clf-lab14-asg; the EC2 instances it runs, named clf-lab14-instance, with their root EBS volumes; the CloudShell file clf-lab14-lt.json.

  1. Scale the group in to zero: this is elasticity in the other direction, releasing capacity that is no longer needed. The instance clf-lab14-instance terminates and its root volume is deleted with it:
aws autoscaling update-auto-scaling-group --auto-scaling-group-name clf-lab14-asg --min-size 0 --desired-capacity 0

Run the describe-auto-scaling-groups command from step 4 until it prints nothing. 2. Delete the Auto Scaling group clf-lab14-asg. The --force-delete option also terminates any instance still in it:

aws autoscaling delete-auto-scaling-group --auto-scaling-group-name clf-lab14-asg --force-delete
  1. Delete the launch template clf-lab14-lt and the local file:
aws ec2 delete-launch-template --launch-template-name clf-lab14-lt
rm clf-lab14-lt.json

Check it is gone

Deleting a group takes a minute or two; run these once it has had time:

aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names clf-lab14-asg --query 'length(AutoScalingGroups)'

Expected output: 0.

aws ec2 describe-instances --filters Name=tag:aws:autoscaling:groupName,Values=clf-lab14-asg Name=instance-state-name,Values=pending,running,stopping,stopped,shutting-down --query 'length(Reservations)'

Expected output: 0. Terminated instances stay listed for a while with the state terminated; they are not running and not charged.

aws ec2 describe-launch-templates --filters Name=launch-template-name,Values=clf-lab14-lt --query 'length(LaunchTemplates)'

Expected output: 0.

Notes

If step 4's echo prints nothing, the Region has no default VPC. You can make one in the VPC console under Actions (Create default VPC), which is free, or do this lab in a Region that still has one. The first time an account uses Amazon EC2 Auto Scaling, AWS makes a service-linked role called AWSServiceRoleForAutoScaling for it. That role is AWS's own, costs nothing and is reused by any later Auto Scaling group, so it is left in place.

This is an independent study companion for the AWS Certified Cloud Practitioner (CLF-C02) exam and is not produced by or endorsed by Amazon Web Services. AWS, Amazon Web Services and AWS Certified Cloud Practitioner are trademarks of Amazon.com, Inc. or its affiliates.

This is an independent study companion for AWS Certified Cloud Practitioner CLF-C02 and is not produced by or endorsed by CompTIA.