Installing Grafana on AWS EC2

Installing Grafana onto and EC2 instance

Devin Moreland
All Things DevOps
6 min readAug 3, 2022

--

What is Grafana?

Per Grafana “Grafana is a complete observability stack that allows you to monitor and analyze metrics, logs, and traces. It allows you to query, visualize, alert on and understand your data no matter where it is stored. Create, explore, and share beautiful dashboards with your team and foster a data-driven culture”. This is a very useful tool to visualize different logs, errors, or metrics from your instances.

How To Install Grafana

Step 1: Create an IAM Role and Policy

  • Go to IAM
  • Create Policy
  • Insert the policy below into the JSON

This policy will allow us permission to list and get Cloudwatch metrics and to describe our EC2 instances. Then;

  • Create Role
  • EC2
  • Choose Grafanca policy
  • Name it and Create a Role

That will create our role with the Grafana policy attached.

Step 2: Create a Security Group

Our Security Group will need ports 22, 80, and 3000 open, Port 3000 is for Grafana

  • Go to Security Groups
  • Add Port 22, 80, and 3000 for our inbound rules pointing to all CIDR ranges.

Step 3: Create EC2 Instance

We need to install Grafana on our server first, so let's provision an EC2 instance

  • Go to EC2
  • Create EC2
  • Choose a public subnet
  • Give it our Grafana security group

Now SSH into our instance

  • Update your packages with

We need to add a repository for grafana so our OS will know where it is.

Add the text below to the repo file. This will install the open-source Grafana.

Step 4: Install Grafana

Now we will install Grafana. If you wish to actually test Grafana skip to Section Two below before you install.

The next command will reload the system

Then we will start our server and check our service with the following two commands.

Our final command will ensure that Grafana will start up automatically if we stop and restart our instance.

Test Grafana

To test our server we will need to grab our public IPv4 and add a :3000 at the end, ie. 10.90.80.10:3000, and insert it into our browser URL. This will bring you to a login screen and our Username and Password will be admin. You will be prompted to create a new password.

We now have Grafana installed, and have logged into the Grafana web server. From here we can add Dashboard and Data Sources.

Section Two

Kill your instance, restart a new one and start over from here if you install Grafana above. This next step will install Grafana along with an application that we can use to test its metrics. The reason for reprovisioning is that Grafana and what will be installed from the repo will overlap and there will be port issues.

This will use docker to install the grafana app and Grafana.

Once in Grafana you can click on the Tutorial Data Source and Dashboard, this will instruct you how to load up an application to your instance to further test with Grafana. This is what I followed and you can follow along using it instead.

Step 1: Install Git

To start you will need to update your server and then install git.

Clone this repo from grafana, and change the directory into tutorial-environment.

Step 2: Install Docker

We will need to install Docker so do this by running the first command, start it using the second command, and check its running by the third command. The following three commands will install docker-compose and put it in the correct path on our system. Not you may not have to but on Amazon Linux 2 it makes us do this or you cannot run the commands.

Make sure all our services are up;

Step 3: Using Grafana

Then we can access our application which is a Grafana News app, this app will allow us to make some API calls and generate some errors so we can see Grafana in action. Go to your AWS console, grab your public IP and enter it in the URL with 8081, like so 10.10.10.10:8081. If you get here and cannot access your website make sure you open it up on port 3000!

Once in the Grafana news app enter a name and in the URL enter example.com. Then vote for the title in the top right.

Now we need to actually get to the Grafana website, so we will need to grab our public IPv4 from the AWS console and add a :3000 at the end, ie. 10.90.80.10:3000, and insert it into our browser URL. This will bring you to a login screen and our Username and Password will be admin. You will be prompted to create a new password.

Our sample app will store metrics in Prometheus, and to see these metrics we need to add Prometheus as a data source. Hover over the gear icon and select Data Sources, then add a data source. Choose Prometheus, then in URL type http://prometheus:9090, scroll to the bottoms, and select save&test.

After this go to the compass on the left sidebar, which is Grafana explore, and enter the following into the space next to the metrics browser;

Hit shift+enter and we can then see some of our metrics!

Conclusion!

I'm not going to set and copy down all the Grafana instructions here, however, I did follow their instructions here in Tutorial Data Source and Dashboard and they are very well organized. We did show however how to install Grafana onto EC2, in two different ways, and how to log into Grafana and set up our first metric.

Below is two bootstrap scripts you can add to your UserData when building your EC2 if you wish to use them. Top if for the Grafana and Grafana app, bottom is for Grafana only.

--

--