Introduction
In this comprehensive guide, we’ll take you through the process of setting up an automated system to restart an EC2 instance when its status check fails using AWS CloudFormation. By creating a CloudWatch Alarm that specifically monitors “Status Check Failure” metrics and integrating it with Simple Notification Service (SNS), we can receive notifications and trigger an automatic restart. This ensures the health and availability of your EC2 instances, especially in scenarios where status check failures may impact performance and reliability.
Prerequisites
- AWS Account
- Basic knowledge of AWS CloudFormation
CloudFormation Template Overview
The CloudFormation template includes parameters, resources, and outputs. Key components:
- Parameters: Customize with the existing EC2 instance ID.
- Resources: Define CloudWatch Alarm, SNS Topic, and SNS Subscription.
- Outputs: Display the EC2 instance ID.
Launch the CloudFormation Stack
- Access AWS CloudFormation.
- Choose “Create Stack” > “With new resources.”
- Upload the provided template.
AWSTemplateFormatVersion: '2010-09-09'
Description: Template to Create a CloudWatch alarm for EC2 instance status check failure
Parameters:
ExistingInstanceId:
Type: String
Description: ID of the existing EC2 instance
Resources:
SnsTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: "EC2StatusCheckFailureTopic"
SnsSubscription:
Type: AWS::SNS::Subscription
Properties:
Protocol: email
TopicArn: !Ref SnsTopic
Endpoint: aaaaaa@gmail.com # Replace with your email address
InstanceStatusAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: InstanceStatusAlarm
Namespace: AWS/EC2
MetricName: StatusCheckFailed
Dimensions:
- Name: InstanceId
Value: !Ref ExistingInstanceId
Statistic: Maximum
Period: 300
EvaluationPeriods: 1
Threshold: 0.99
ComparisonOperator: GreaterThanOrEqualToThreshold
AlarmActions:
- Ref: 'SnsTopic'
- !Sub 'arn:aws:automate:${AWS::Region}:ec2:reboot'
Dimensions:
- Name: 'InstanceId'
Value: !Ref ExistingInstanceId
DependsOn: SnsSubscription
- Fill in parameters, including the
ExistingInstanceId
. - Click through and “Create stack.”
CloudWatch Alarm for Status Check Failure
The InstanceStatusAlarm
is set to monitor the "StatusCheckFailed" metric. If it exceeds 0.99 for 300 seconds, the alarm triggers.
Simple Notification Service (SNS)
The template creates SnsTopic
and SnsSubscription
to your email. Replace the placeholder with your email for notifications.
Triggering Auto-Restart on Status Check Failure
- Wait for stack creation.
- Intentionally stop the EC2 instance or let it fail a status check.
- CloudWatch Alarm detects failure, triggers SNS notification.
- Check email for SNS notification.
Conclusion:
With this CloudFormation template, you’ve set up a quick and efficient system to automatically restart EC2 instances on status check failures. By integrating CloudWatch Alarms and SNS, you ensure prompt responses to keep your instances running smoothly.
Take charge of your AWS instance health! Deploy our CloudFormation template for automated EC2 restarts on status check failures. Swiftly monitor, get alerts, and enhance reliability. Elevate your AWS game — deploy now!