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

  1. Go to the AWS Management Console and navigate to the CloudWatch dashboard.
  2. Click on "Alarms" in the left-hand menu.
  3. Click on "Create Alarm".
  4. Choose "Metric" as the alarm type and select the metric that monitors the server's status (e.g. "EC2 Status Check Failed").
  5. Set the alarm threshold to "Greater than 0" to trigger the alarm when the server is down.
  6. Choose "Email" as the notification type and enter your email address.
  7. Click "Create Alarm".

Method 2: Using Amazon CloudWatch Events

  1. Go to the AWS Management Console and navigate to the CloudWatch Events dashboard.
  2. Click on "Create rule".
  3. 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").
  4. Set the event filter to only trigger the rule when the instance is stopped.
  5. Choose "Email" as the notification type and enter your email address.
  6. Click "Create rule".

Method 3: Using AWS Lambda and SNS

  1. Go to the AWS Management Console and navigate to the Lambda dashboard.
  2. Click on "Create function".
  3. Choose "Author from scratch" and select "Node.js" as the runtime.
  4. Write a Lambda function that checks the server's status and sends an email using an SNS topic.
  5. Create an SNS topic and subscribe to it with your email address.
  6. Configure the Lambda function to trigger on the server's status change (e.g. "EC2 instance state change: instance stopped").
  7. 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.