Alert server down mail in aws
In AWS, you can set up an alert to notify you via email when a server is down. Here are the steps:
Method 1: Using Amazon CloudWatch
- Go to the AWS Management Console and navigate to the CloudWatch dashboard.
- Click on "Alarms" in the left-hand menu.
- Click on "Create Alarm".
- Choose "Metric" as the alarm type and select the metric that monitors the server's status (e.g. "EC2 Status Check Failed").
- Set the alarm threshold to "Greater than 0" to trigger the alarm when the server is down.
- Choose "Email" as the notification type and enter your email address.
- Click "Create Alarm".
Method 2: Using Amazon CloudWatch Events
- Go to the AWS Management Console and navigate to the CloudWatch Events dashboard.
- Click on "Create rule".
- Choose "Event pattern" as the rule type and select the event that triggers when the server is down (e.g. "EC2 instance state change: instance stopped").
- Set the event filter to only trigger the rule when the instance is stopped.
- Choose "Email" as the notification type and enter your email address.
- Click "Create rule".
Method 3: Using AWS Lambda and SNS
- Go to the AWS Management Console and navigate to the Lambda dashboard.
- Click on "Create function".
- Choose "Author from scratch" and select "Node.js" as the runtime.
- Write a Lambda function that checks the server's status and sends an email using an SNS topic.
- Create an SNS topic and subscribe to it with your email address.
- Configure the Lambda function to trigger on the server's status change (e.g. "EC2 instance state change: instance stopped").
- Click "Create function".
Example Lambda function code (Node.js)
exports.handler = async (event) => {
const ec2 = new AWS.EC2({ region: 'your-region' });
const instanceId = 'your-instance-id';
const params = { InstanceIds: [instanceId] };
const response = await ec2.describeInstances(params).promise();
const instance = response.Reservations[0].Instances[0];
if (instance.State.Name === 'stopped') {
const sns = new AWS.SNS({ region: 'your-region' });
const topicArn = 'your-sns-topic-arn';
const params = { Message: 'Server is down', TopicArn: topicArn };
await sns.publish(params).promise();
}
return { statusCode: 200 };
};
Note: Replace "your-region", "your-instance-id", and "your-sns-topic-arn" with your actual AWS resources.