How can this be accomplished?
Create an Amazon CloudWatch alarm for the EC2 instance with basic monitoring. Add an action to restart the instance.
Create an Amazon CloudWatch alarm for the EC2 instance with detailed monitoring. Add an action to restart the instance.
Create an AWS Lambda function to restart the EC2 instance, invoked on a scheduled basis every 2 minutes.
Create an AWS Lambda function to restart the EC2 instance, invoked by EC2 health checks.
Explanations:
Basic monitoring for CloudWatch does not provide the granularity needed to detect CPU utilization exceeding a threshold over a specific time period. It does not track metrics at the same level of detail as detailed monitoring, which can accurately trigger alarms based on sustained high CPU usage.
Detailed monitoring provides the ability to set an alarm that tracks CPU utilization and can trigger an action, such as restarting the instance, if the utilization remains high (100%) for more than 2 minutes. This is the appropriate solution for automating instance restarts based on sustained high CPU usage.
While a Lambda function could restart the instance, running it on a scheduled basis every 2 minutes does not ensure timely response to CPU usage spikes. If the instance has high CPU usage just after a Lambda execution, it would not be addressed until the next scheduled invocation, which could lead to downtime longer than desired.
Using EC2 health checks may not directly correlate to high CPU utilization issues. While health checks can monitor the instance’s status, they do not specifically monitor CPU usage, which is the problem needing resolution. Additionally, health checks may not trigger within the 2-minute requirement if the instance remains operational but is CPU-bound.