Monitoring — Prometheus and Grafana on AWS
Tags: aws, cloud, prometheus, grafana
Published: 2023-03-13 09:46:22

![Architecture](https://cdn-images-1.medium.com/max/2000/1*V5MzcCE4ML6z1yqrgrK4ZQ.png)

**NOTE**: This article have associated with an article about [Infrastructure as Code — Terraform on AWS](https://dev.to/budionosan/infrastructure-as-code-terraform-on-aws-1cpd).

Hello everyone. The first time, I used **Prometheus** and **Grafana** and hosted to Amazon EC2. [Prometheus](https://prometheus.io/) is open source monitoring system and time series database. [Grafana](https://grafana.com) 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](https://dev.to/budionosan/compute-on-aws-amazon-elastic-compute-cloud-ec2-28c0) **or** with **Terraform** on [this link](https://dev.to/budionosan/infrastructure-as-code-terraform-on-aws-1cpd).

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](https://grafana.com/docs/grafana/v9.0/setup-grafana/installation/rpm/#install-from-yum-repository).

    sudo nano /etc/yum.repos.d/grafana.repo

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

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

![Grafana open source](https://cdn-images-1.medium.com/max/2000/1*wf7XoXrQl2Nsxr_HaPuXKA.jpeg)

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](https://grafana.com/docs/grafana/latest/setup-grafana/sign-in-to-grafana/).

![Public IP in EC2 instance](https://cdn-images-1.medium.com/max/2000/1*kwTMLOUwyzXGG1DzG7kHnQ.jpeg)

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](https://cdn-images-1.medium.com/max/2000/1*kd9muDO6jr50SG4LlPPkyQ.jpeg)

![Grafana](https://cdn-images-1.medium.com/max/2724/1*O4tCOKGL9NWsROc5FQihAg.jpeg)

Before exploring Grafana, install [**Prometheus**](https://prometheus.io/download/) with [this step](https://lightsail.aws.amazon.com/ls/docs/en_us/articles/amazon-lightsail-install-prometheus). 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

After installing Prometheus, extract the Prometheus installed file.

    tar -xvf prometheus-2.37.5.linux-amd64.tar.gz

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](https://cdn-images-1.medium.com/max/2000/1*77ps6e6P-PobcNKsJSY6tg.jpeg)

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

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

![Prometheus](https://cdn-images-1.medium.com/max/2000/1*-PI7zJ-4VOvHmU9eN0fDXA.jpeg)

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](https://prometheus.io/docs/tutorials/getting_started/#show-me-how-it-is-done). Then click **Status** and click **Targets**.

![Prometheus](https://cdn-images-1.medium.com/max/2000/1*xp_PVb0MgB8RlkQrM3e_4w.jpeg)

![Targets](https://cdn-images-1.medium.com/max/2054/1*tmjqOnXDAZIGP00O8sO7Iw.jpeg)

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

![Prometheus metrics](https://cdn-images-1.medium.com/max/2000/1*tOR9h0_EtOl8ghroN0kigw.jpeg)

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

![](https://cdn-images-1.medium.com/max/2000/1*IGkNBAiJ6bImkHR3fX33Sw.jpeg)

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

![](https://cdn-images-1.medium.com/max/2000/1*rhjKbRSJxaw8SkiSJcrmxw.jpeg)

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

![](https://cdn-images-1.medium.com/max/2000/1*GLyfE18FHJKvosYwSTUM7Q.jpeg)

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.

![](https://cdn-images-1.medium.com/max/2000/1*JJUd1UC1wg6CVGE5o0BvVQ.jpeg)

![](https://cdn-images-1.medium.com/max/2000/1*jb29EKyFmQ-idT70Q1hR3A.jpeg)

[**Explore**](https://docs.aws.amazon.com/grafana/latest/userguide/explore.html) 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**.

![](https://cdn-images-1.medium.com/max/2558/1*Uph7Lq_lVJNRwkOOi3pqDg.jpeg)

![Explore](https://cdn-images-1.medium.com/max/2374/1*zfErExhsyKsjGuCEICHRhw.jpeg)

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

![Dashboard section](https://cdn-images-1.medium.com/max/2502/1*4QjAzUe-HrZqmP_48AJN-A.jpeg)

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](https://grafana.com/grafana/dashboards/3662-prometheus-2-0-overview/), click **Copy ID to the dashboard**, and click **Load.**

![Import dashboard](https://cdn-images-1.medium.com/max/2000/1*FOVNQbfeQfQEO1-AoF9xxA.jpeg)

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

![](https://cdn-images-1.medium.com/max/2000/1*FLGpBtRxnF8thgwMfOgSSQ.jpeg)

![Prometheus visualization](https://cdn-images-1.medium.com/max/2560/1*CEc9fvqZZoNNmb3TkolnQQ.jpeg)

![Prometheus visualization](https://cdn-images-1.medium.com/max/2556/1*6aHlvvwKoLZo8UogPoX78Q.jpeg)

![Prometheus visualization](https://cdn-images-1.medium.com/max/2576/1*in_wETT1TqnVCKs79KEbZg.jpeg)

![Prometheus visualization](https://cdn-images-1.medium.com/max/2552/1*Kz6OIxG8yWpnBFXGQDUvVA.jpeg)

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 :)