DEV Community

Budiono Santoso for AWS Community Builders

Posted on • Edited on

Monitoring — Prometheus and Grafana on AWS

Architecture

NOTE: This article have associated with an article about Infrastructure as Code — Terraform on AWS.

Hello everyone. The first time, I used Prometheus and Grafana and hosted to Amazon EC2. Prometheus is open source monitoring system and time series database. Grafana is open source analytics and monitoring system.

When you have a web application, either CPU or memory passes 100% and your web application is crash but you don’t know how to fix this problem. The solution for this problem — is always to monitor your web application so that when having a problem and can fast fix it. Tools that can monitor — Prometheus and Grafana.

Before installing Prometheus and Grafana, create an EC2 instance from the AWS console on this link or with Terraform on this link.

After creating an EC2 instance, install Grafana. Use Amazon Linux 2 for OS. You can choose Grafana open source or Grafana enterprise. For this tutorial, choose Grafana open source. Install Grafana from the YUM repository.

sudo nano /etc/yum.repos.d/grafana.repo
Enter fullscreen mode Exit fullscreen mode

After sudo nano, copy and paste below this code.

[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
Enter fullscreen mode Exit fullscreen mode

After copy-paste, press Ctrl + X then Y, and ENTER. Now can install Grafana. When asked to choose [y/N] — choose always y. After Grafana has already been installed, start Grafana and check Grafana status.

sudo yum install grafana

sudo systemctl daemon-reload
sudo systemctl start grafana-server
sudo systemctl status grafana-server
Enter fullscreen mode Exit fullscreen mode

Grafana open source

After Grafana is now running, enable Grafana — sudo systemctl enable grafana-server. Then open EC2, search your EC2 instance with Grafana installed and copy public IPv4 address — publicIP:3000. But why 3000? Because for open Grafana need port 3000 based on this link.

Public IP in EC2 instance

Fill the username and password — admin and admin, then log in. Can skip the password or change the password. For this tutorial, skip the password. Welcome to Grafana finally.

Grafana

Grafana

Before exploring Grafana, install Prometheus with this step. For this tutorial, install Prometheus only without installing other tools.

curl -LO https://github.com/prometheus/prometheus/releases/download/v2.37.5/prometheus-2.37.5.linux-amd64.tar.gz
Enter fullscreen mode Exit fullscreen mode

After installing Prometheus, extract the Prometheus installed file.

tar -xvf prometheus-2.37.5.linux-amd64.tar.gz
Enter fullscreen mode Exit fullscreen mode

Edit the prometheus.yml — sudo nano /etc/prometheus/prometheus.yml

When see targets, change localhost to a public IPv4 address on the EC2 instance that Prometheus installed.

prometheus.yml

After this, press Ctrl + X then Y and ENTER. Edit the prometheus.service — sudo nano /etc/systemd/system/prometheus.service

[Unit]
Description=PromServer
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \n    --config.file /etc/prometheus/prometheus.yml \n    --storage.tsdb.path /var/lib/prometheus/ \n    --web.console.templates=/etc/prometheus/consoles \n    --web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

After this, press Ctrl + X then Y and ENTER. After Prometheus has already been installed, start Prometheus and check Prometheus status.

sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl status prometheus
Enter fullscreen mode Exit fullscreen mode

Prometheus

After Prometheus is now running, enable Prometheus — sudo systemctl enable prometheus. Then open EC2, search your EC2 instance with Prometheus installed and copy the public IPv4 address — publicIP:9090. But why 9090? Because for opening Prometheus need port 9090 based on this link. Then click Status and click Targets.

Prometheus

Targets

Go to the Prometheus link and show the result like below this.

Prometheus metrics

Back to Grafana. Go to Configuration and click Data sources.

Click Add data source, search Prometheus, and click Prometheus to settings Prometheus.

Fill in the URL-based Prometheus link but delete /metrics like this screenshot.

Choose Prometheus and version 2.37.x because I install Prometheus version 2.37.5. Click Save & test. If successful, show a notification Data source is working. Then go to Explore section.

Explore means can focus on the query without thinking about visualization. Focus on the metrics you want to display. Try metric — prometheus_http_requests_total and click Run query.

Explore

If want create dashboard, go to dashboard section and click Import.

Dashboard section

Importing a dashboard is fast step than creating a new dashboard, but instead, creating a new dashboard that can see the metrics that needs. Import dashboard can upload JSON files or via Grafana.com dashboard. Import via Grafana.com on this link, click Copy ID to the dashboard, and click Load.

Import dashboard

Change name and folder if needed. Data source from Prometheus. Click Import and wait until the dashboard is showing.

Prometheus visualization

Prometheus visualization

Prometheus visualization

Prometheus visualization

After trying Prometheus and Grafana, I feel two tools are very useful for learning monitoring. Coming soon, I try Amazon Managed Service for Prometheus and Amazon Managed Service for Grafana with Amazon EKS. Stay tuned. Thank you very much :)

Top comments (0)