EC2 Monitoring and Alerting with Prometheus, Node Exporter, Alertmanager, and Grafana

1. Introduction

This guide provides step-by-step instructions for setting up monitoring and alerting on an AWS EC2 instance using Prometheus, Node Exporter, Alertmanager (with Gmail SMTP for email alerts), and Grafana.


2. Launch an EC2 Instance

  1. Log in to your AWS Management Console.
  2. Launch a new EC2 instance (Ubuntu 22.04 LTS recommended).
  3. Choose instance type (t2.micro or higher).
  4. Configure security groups:

3. Install and Configure Prometheus

Create Prometheus user and directories:

sudo useradd --no-create-home --shell /bin/false prometheus
sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheus

Download Prometheus:

cd /tmp
curl -s <https://api.github.com/repos/prometheus/prometheus/releases/latest> | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | wget -qi -tar xvf prometheus-*.tar.gz
cd prometheus-*.linux-amd64
sudo cp prometheus promtool /usr/local/bin/
sudo cp -r consoles/ console_libraries/ /etc/prometheus/ || truesudo cp prometheus.yml /etc/prometheus/prometheus.yml

Screenshot (51).png

Set ownership:

sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
sudo chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool
sudo chmod +x /usr/local/bin/prometheus /usr/local/bin/promtool

Create Prometheus systemd service: