Configuring AWS CloudWatch to send alerts using Amazon SNS if an EC2 instance goes down involves several steps. Here’s a step-by-step guide to achieve this:
Step 1: Create an SNS Topic
- Navigate to the Amazon SNS Console:
- Go to the Amazon SNS console.
- Create a Topic:
- Click on Create topic.
- Choose the Standard type.
- Enter a name for the topic (e.g.,
EC2-Instance-Down-Alerts
). - Click Create topic.
- Create a Subscription:
- After creating the topic, click on Create subscription.
- Select the newly created topic.
- Choose a protocol (e.g., Email, SMS, HTTP/S).
- Enter the endpoint (e.g., your email address if you chose Email).
- Click Create subscription.
- Confirm Subscription:
- If you chose Email as the protocol, check your email and confirm the subscription.
Step 2: Create a CloudWatch Alarm
- Navigate to the CloudWatch Console:
- Go to the Amazon CloudWatch console.
- Create an Alarm:
- Click on Alarms in the left navigation pane.
- Click Create alarm.
- Select a Metric:
- Click on Select metric.
- Choose EC2 metrics.
- Navigate to Per-Instance Metrics.
- Select the StatusCheckFailed_Instance metric for your desired EC2 instance. This metric indicates a failure of the instance status check.
- Configure Alarm:
- Specify the threshold type (Static).
- Set the condition:
- Whenever StatusCheckFailed_Instance is >= 1 for 1 consecutive period(s) of 5 minutes.
- Click Next.
- Add Notification:
- Under the Notification section, click on Add notification.
- For Send a notification to, select your SNS topic (e.g.,
EC2-Instance-Down-Alerts
). - Click Next.
- Name and Description:
- Enter a name and description for the alarm (e.g.,
EC2_Instance_Down_Alarm
). - Click Next.
- Enter a name and description for the alarm (e.g.,
- Review and Create:
- Review the settings and click Create alarm.
Step 3: Verify the Alarm and Notification
- Test the Setup:
- You can stop the EC2 instance temporarily to test if the alarm triggers and sends a notification.
- Go to the EC2 console, select your instance, and click Instance state > Stop instance.
- Check Notifications:
- Once the instance is stopped and fails the status check, CloudWatch should trigger the alarm.
- You should receive an alert via your configured SNS subscription (e.g., an email if you set up email notifications).
Step 4: Automate Recovery (Optional)
If you want to automatically recover the instance when it goes down, you can create an additional CloudWatch alarm that triggers the recovery action.
- Create a Recovery Alarm:
- Follow the steps to create another CloudWatch alarm.
- Select the same StatusCheckFailed_Instance metric.
- Set the threshold condition.
- Under Actions, choose Recover this instance.