Deploy a Grafana dashboard with Docker on AWS EC2
Tags: aws, docker, devops
Published: 2024-05-07 11:23:28

Monitoring system performance and logs with Grafana is indeed straightforward, especially when working with Docker environments. Grafana seamlessly integrates with Docker, providing prebuilt dashboards that instantly visualize key metrics and logs from your containers and hosts.

## Launch EC2 instance
To embark on your AWS journey, you'll need a server instance, and AWS offers the perfect playground with its free tier. Launch a Linux instance effortlessly, and let the experimentation begin!

Click on launch EC2 instance and select **t2.micro** to use the free tier.

![Launch EC2 Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ggzmdk16wcf0cb6n71s.png)

Here we need some more configurations:
- Create a key pair (for the login) and download it in a safe place;
- Add a security group or create a new one;
-Allow traffic from both ssh and internet.

Now you can launch your EC2 instance 

### Open ports
For these project we need to open these ports:
- **3000** for Grafana
- **9090** for Prometheus
- **3100** for Loki
- **9100** for node-exporter

![EC2 port settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h7dprsbb1m9xhzn09wcd.png)

To do this:
- navigate to the EC2 Dashboard and select your EC2 instance
- click on the security group associated and the click edit
- configure the inbound rule selecting **Custom TCP Rule** as the type. Enter the port number and select **Anywhere** as the source.
- Repeat the last step for all the four ports

## Install Docker
With your Linux instance up and running, it's time to unlock the power of containerization by installing Docker. This game-changing platform will enable you to effortlessly deploy and manage your applications, including the highly coveted Grafana dashboard.

### Connect via ssh
In your terminal run
```
ssh -i "<path_to_your_key.pem>" ec2-user@<Public IPv4 DNS>
```
Type yes and you are inside your instance!

![SSH Connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2pinneuqoixf3zi9tau.png)

### Install Docker and Docker Compose
To install Docker run:
```
sudo yum update
sudo yum install -y docker
```
Then we can install Docker Compose. Run:

```
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose
```

### Run Docker service
If everything since here went fine you only need to start the Docker daemon. To do this run:

```
sudo systemctl start docker
sudo systemctl enable docker
```

## Copy the project
You can download the source code from the repo and copy it into the EC2 with **scp**, or clone directly into the EC2 instance, and check if `git` is installed. 

To clone the repo run:
```
git clone https://github.com/maiobarbero/grafana-prometheus-loki.git
```

### Edit the configuration's files
Now with **vim** edit the configuration file, in particular if you are using [this repository](https://github.com/maiobarbero/grafana-prometheus-loki) edit `config/prometheus/prometheus-config.yml` and replace localhost with your private IP4.

## Run the application
Here we are, the last step. Inside your project folder run
```
docker-compose up -d
```
And that's it.

Go to your-public-ip:3000 and you have your Grafana instance!!!


---

_Cover image by <a href="https://unsplash.com/@chrisliverani?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Chris Liverani</a> on <a href="https://unsplash.com/photos/turned-on-flat-screen-monitor-dBI_My696Rk?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Unsplash</a>_