You are currently viewing Comprehensive Guide: Installing Prometheus and Grafana on Linux AMI for Node.js API Monitoring

Comprehensive Guide: Installing Prometheus and Grafana on Linux AMI for Node.js API Monitoring

To install Prometheus and Grafana on a Linux AMI (Amazon Machine Image) for monitoring a Node.js API, follow these steps:

1. Launch an EC2 Instance:

Start by launching an Amazon EC2 instance with a Linux AMI of your choice (e.g., Amazon Linux 2). Ensure that your security group allows access to necessary ports (e.g., 22 for SSH, 3000 for Grafana, and 9090 for Prometheus).

2. Connect to the EC2 Instance:

SSH into your EC2 instance using the private key associated with your instance:

ssh -i /path/to/your/private/key.pem ec2-user@your-instance-ip

3. Install Prometheus

# Download Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.30.0/prometheus-2.30.0.linux-amd64.tar.gz

# Extract the archive
tar -xvzf prometheus-2.30.0.linux-amd64.tar.gz

# Rename the extracted directory
mv prometheus-2.30.0.linux-amd64 prometheus

# Create a Prometheus configuration file (prometheus.yml)
vim prometheus/prometheus.yml

Add the following basic configuration to prometheus.yml:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'nodejs-api'
    static_configs:
      - targets: ['localhost:3000'] # Replace with your Node.js API host and port on which API is running

4. Start Prometheus:

cd prometheus
./prometheus --config.file=prometheus.yml

Prometheus should now be running and scraping metrics from your Node.js API.

5. Install Grafana:

# Download Grafana
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.1.2.linux-amd64.tar.gz

# Extract the archive
tar -xvzf grafana-enterprise-8.1.2.linux-amd64.tar.gz

# Rename the extracted directory
mv grafana-8.1.2 grafana

6. Start Grafana:

cd grafana/bin
./grafana-server

Grafana should now be running. Access the Grafana web interface by navigating to http://your-instance-ip:3000 in your web browser. Log in with the default credentials (admin/admin) and change the password.

7. Add Prometheus as a Data Source:

  • Log in to the Grafana web interface.
  • Click on the gear icon (⚙️) on the left sidebar to open the Configuration menu.
  • Click on “Data Sources.”
  • Click on “Add data source.”
  • Choose “Prometheus.”
  • Configure the HTTP settings (URL should be http://localhost:9090 by default).

8. Create a Dashboard in Grafana:

  • Click on the “+” icon on the left sidebar to add a new dashboard.
  • Click on “Add new panel.”
  • Choose a visualization type and select the Prometheus data source you added earlier.
  • Configure the query to display the desired metrics from Prometheus.
  • Customize the panel settings and save the panel.
  • Build your dashboard by adding more panels as needed.

Leave a Reply