Sub topic 16
Monitoring instances and status checks, and controlling cost
This chapter assumes you can launch an instance with the AWS CLI and understand the instance lifecycle. It covers how to tell whether an instance is healthy, then how to pay less for it.
Status checks
EC2 runs two automated checks every minute on each running instance.
| Check | What it tests | Typical fix |
|---|---|---|
| System status | The AWS host and its network | Stop and start the instance (moves it to new hardware), or use auto recovery |
| Instance status | The guest OS, kernel, and network configuration | Reboot, or fix the OS configuration |
aws ec2 describe-instance-status \
--instance-ids i-0123456789abcdef0 \
--query 'InstanceStatuses[].[InstanceId,SystemStatus.Status,InstanceStatus.Status]' \
--output tableCloudWatch metrics
EC2 publishes metrics to CloudWatch in the AWS/EC2 namespace, including CPUUtilization, NetworkIn, NetworkOut, and StatusCheckFailed. Basic monitoring reports every 5 minutes at no charge. Detailed monitoring reports every minute for an extra fee. Enable it with aws ec2 monitor-instances --instance-ids i-0123456789abcdef0.
Query average CPU for the last hour:
aws cloudwatch get-metric-statistics \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--dimensions Name=InstanceId,Value=i-0123456789abcdef0 \
--start-time "$(date -u -d '1 hour ago' +%Y-%m-%dT%H:%M:%SZ)" \
--end-time "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
--period 300 \
--statistics AverageAn alarm can act on a metric. This one recovers the instance when the system check fails twice in a row. Change the Region in the action ARN to match yours.
aws cloudwatch put-metric-alarm \
--alarm-name tutorial-web-recover \
--namespace AWS/EC2 \
--metric-name StatusCheckFailed_System \
--dimensions Name=InstanceId,Value=i-0123456789abcdef0 \
--statistic Maximum \
--period 60 \
--evaluation-periods 2 \
--threshold 1 \
--comparison-operator GreaterThanOrEqualToThreshold \
--alarm-actions arn:aws:automate:us-east-1:ec2:recoverPurchasing options
All four options run the same instances. They differ in price and commitment.
| Option | Commitment | Best for |
|---|---|---|
| On-Demand | None, billed per second | Short, unpredictable, or new workloads |
| Savings Plans | A spend per hour for 1 or 3 years | Steady usage that may change size or family |
| Reserved Instances | 1 or 3 years for a specific configuration | Steady usage on a known instance type |
| Spot | None, but AWS can reclaim capacity | Fault-tolerant work such as batch jobs |
Spot instances can cost up to 90% less than On-Demand. AWS gives a two-minute interruption notice before it reclaims one, so the workload must tolerate losing the instance.
Controlling cost
- Start On-Demand, watch utilization, then commit to Savings Plans for the stable baseline.
- Right-size. An instance that averages 5% CPU can usually move to a smaller type.
- Stop instances you do not need. A stopped instance has no compute charge, but its EBS volumes still do.
- Tag instances so you can see spending per project, and set an AWS Budgets alert.
Exercise
An instance passes the instance status check but fails the system status check. Name one action that can fix it and explain why it works.
Show answer
Stop and then start the instance. A stop and start usually moves it to a different physical host, which clears a problem with the underlying hardware. A plain reboot keeps it on the same host. An alarm with the recover action also fixes it automatically.