Deploy amazon app in aws ec2 using terraform as a infrastructure building tool and then we automate the whole process using the jenkins which allows us continuous integration and continuous deployment (ci-cd) and then we setup Promotheus and grafana for monitering
Pre-requisite
Let’s do it
before do anything →
- open your terminal and make a separate folder for amazon →mkdir amazon
- cd amazon
- clone the github repo
- git clone https://github.com/Aakibgithuber/Amazon-app-Deployment-using-terraform-and-jenkins.git
Completion steps →
Step 1 → Setup Terraform and configure aws on your local machine
Step 2 → Building a simple Infrastructure from code using terraform
Step 3 → Setup Sonarqube and jenkins
Step 4 → ci-cd pipeline
Step5 →Monitering via Prmotheus and grafana
Step 6 → Terraform Destroy
Step 1 → Setup Terraform and configure aws on your local machine
1. Setup Terraform
To install terraform copy and paste the below commands
sudo su
snap install terraform --classic
which terraform
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:728/0*Z4ACcqtjcrDyBJoM.png?resize=582%2C59&ssl=1)
2. Configure aws
- create an IAM user
- go to your aws account and type IAM
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*xvD7TivSXDF58h2iLa2a3A.png?resize=700%2C389&ssl=1)
3. click on user →create user
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:320/1*-MXoomhqAZkFv2k4Lc6KHA.png?resize=256%2C364&ssl=1)
4. Give a name to your user and tick on provide user access to management console and then click on I want an IAM user option
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*EeuPzZvgffuM0wzAYjYUHg.png?resize=700%2C394&ssl=1)
5. choose a password for your user →click next
6. Attach the policies directly to your iam user → click next
note →I will provide the administrator accesss for now but we careful while attaching the policies on your workapce
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*xexNQgf1Eo3KNdXfWgXP5A.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*RI1N7eH_WheuI8DTe1MXrA.png?resize=700%2C394&ssl=1)
7. click on create user
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*Jr9IfZferOL7LgBq6YQSEw.png?resize=700%2C394&ssl=1)
8. download your password file if it is autogenerated otherwise it is your’s choice
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*O62uJqOqt3c39xRbE-V-Qw.png?resize=700%2C179&ssl=1)
9. Now click on your IAM user →security credentials
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*knpX5GXpK_wNNJwcv6gVew.png?resize=700%2C81&ssl=1)
10. scroll down to access keys and create an access keys
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*BqqdRPLpSZ8AxXHysq4sMg.png?resize=700%2C271&ssl=1)
11.choose aws cli from the options listed
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*Xor_c7K7vCnHOvpofbbZig.png?resize=700%2C394&ssl=1)
12. click next and download you csv file for username and password
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*L03FEkZmbFTJqeo3HVaxCQ.png?resize=700%2C394&ssl=1)
13. go to your terminal and type →aws configure
14. Now it is ask to your access key and secret key for this open your csv file and paste the access and secret key and remain everything default
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*KqTqXKRJwMdOCviqmXDHuQ.png?resize=700%2C77&ssl=1)
15. Now you are ready to configure aws from your terminal
Step 2 → Building a simple Infrastructure from code using terraform
- go to folder → cd JENKINS-TF
- there are three files present main.tf, install_jenkins.sh , provider.tf
- open the file →vim Main.tf
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*x9Uix22CyBaYeTPzp_MWRw.png?resize=700%2C303&ssl=1)
4. change this section → ami = # your ami id , key_name= #your key pair if any
Now run terraform commands →
- main.tf includes userdata which links install_jenkins.sh file on which execution install jenkins,docker,trivy,and start the sonarqube container on port 9000
resource "aws_security_group" "Jenkins-sg" {
name = "Jenkins-Security Group"
description = "Open 22,443,80,8080,9000"
# Define a single ingress rule to allow traffic on all specified ports
ingress = [
for port in [22, 80, 443, 8080, 9000, 3000] : {
description = “TLS from VPC”
from_port = port
to_port = port
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
ipv6_cidr_blocks = []
prefix_list_ids = []
security_groups = []
self = false
}
]
egress {
from_port = 0
to_port = 0
protocol = “-1”
cidr_blocks = [“0.0.0.0/0”]
}
tags = {
Name = “Jenkins-sg”
}
}
resource “aws_instance” “web” {
ami = “ami-0c7217cdde317cfec”
instance_type = “t2.large”
key_name = “my key”
vpc_security_group_ids = [aws_security_group.Jenkins-sg.id]
user_data = templatefile(“./install_jenkins.sh”, {})
tags = {
Name = “amazon clone”
}
root_block_device {
volume_size = 30
}
}
terraform init
terraform validate
terraform plan
terraform apply --auto-approve
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:2400/1*yKjnK04NWutlhPkK7WI74Q.png?resize=500%2C1080&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:2400/1*l1Z7wxyQD_o8DKGKSkmkPw.png?resize=500%2C1080&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*CKEIoVf8Rc6JriO59ZorIQ.png?resize=700%2C394&ssl=1)
Go to your aws console and checkout the ec2 instances
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*n1UNUkUhfdBJcPSsJ9wJHQ.png?resize=700%2C119&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*NY8um8dnJUdzHjpXXszYow.png?resize=700%2C394&ssl=1)
Here we see amazon app instance is created by terraform with the given configuration
Step 3 → Setup Sonarqube and jenkins
1.Sonarqube →
copy the public ip of your machine
- go to your browser and type →<publicip>:9000
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*Qi4SGSAtGscMS1iap2Payw.png?resize=700%2C166&ssl=1)
2. iniatially username and password is admin
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:714/1*wuay_1QDWxjvmVCnwAbaTw.png?resize=571%2C595&ssl=1)
3. update your password
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*t5nBWcyfInJIeoCPlL2Rmw.png?resize=700%2C394&ssl=1)
4. welcome window of Sonarqube
2. Jenkins →
- on browser type →<public_ip>:8080
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*WGIcxVSbmNknpTu4avVG2w.png?resize=700%2C394&ssl=1)
2. for this go to your ec2 and connect it
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*c3WGgr3wLvduJNJmHiQaeg.png?resize=700%2C394&ssl=1)
3. run the below commands
sudo su
cat /var/lib/jenkins/secrets/initialAdminPassword
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*CuaeOYSHkG037YGOonIbyg.png?resize=700%2C49&ssl=1)
output is your password and paste it to your jenkins
4. Install the suggested plugins
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*oRx3qKP5B5rCgN7hI_6gIg.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*-1Njxkd31IubyNuaDCwoyQ.png?resize=700%2C394&ssl=1)
5. Setup your jenkins user
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*lJEv5jK4jvNkBW9--ix5vg.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*daGLWXIleNJzr9AY_g3rGw.png?resize=700%2C394&ssl=1)
Step 4 → ci-cd pipeline
1. Install Plugins listed below
1 Eclipse Temurin Installer (Install without restart)
2 SonarQube Scanner (Install without restart)
3 NodeJs Plugin (Install Without restart)
4. owasp →The OWASP Plugin in Jenkins is like a “security assistant” that helps you find and fix security issues in your software. It uses the knowledge and guidelines from the Open Web Application Security Project (OWASP) to scan your web applications and provide suggestions on how to make them more secure. It’s a tool to ensure that your web applications are protected against common security threats and vulnerabilities.
5. Prometheus metrics →to moniter jenkins on grafana dashboard
6. Download all the docker realated plugins
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*AUA05l9_F72VjFgl.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*Rkx9jQMneA5ZGKUEZ7_ywQ.png?resize=700%2C394&ssl=1)
2. add credentials of Sonarqube and Docker
1st we genrate a token for sonarqube to use in jenkins credentials as secret text
a. setup sonarqube credentials
- go to http://publicip:9000
- now enter your username and password
- click on security →users →token →generate token
- token_name==jenkins
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*zz3sxJcpimO7xkEa.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*bgyTKRVXvQc-XUbmDxwd4g.png?resize=700%2C270&ssl=1)
4. copy the token and go to your jenkins →manage jenkins →credentials →global →add credentials
5. select secret text from dropdown
6. secret text ==your token , id =jenkins →click on create
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*DUNes_a_msbWxcNX2mTwuw.png?resize=700%2C394&ssl=1)
b. setup projects in sonarqube for jenkins
- go to your sonarqube server
- click on projects
- in the name field type Amazon
- click on set up
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:623/1*tTVfwZ-LT_y2GaEUR1o8xg.png?resize=498%2C493&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:545/0*1MHIySBAixgC0_x8.png?resize=436%2C217&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*rh_8VZmOTRz-gJX5mpwy6g.png?resize=700%2C228&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:1945/1*yz51rtTQ3D-UmMALoai-xA.png?resize=626%2C525&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:2400/1*doJBagzoL06tF2MG56WQkQ.png?resize=375%2C1080&ssl=1)
Sonarqube project for jenkins is setup now
c. Setup docker credentials
- go to your jenkins →manage jenkins →credentials →global →add credentials
- provide your username and password of your dockerhub
- id==docker
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*2glrih5625vcJXNN3bfdkw.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*aky-OHIidV-a-YNYXVlBLA.png?resize=700%2C144&ssl=1)
3. Now we are going to setup tools for jenkins
go to manage jenkins → tools
a. add jdk
- click on add jdk and select installer adoptium.net
- choose jdk 17.0.8.1+1version and in name section enter jdk 17
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*kJdlagVkFUyNnuqG.png?resize=700%2C394&ssl=1)
b. add node js
- click on add nodejs
- enter node16 in name section
- choose version nodejs 16.2.0
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*nLELaU6-HQ-xqV30.png?resize=700%2C394&ssl=1)
c. add docker →
- click on add docker
- name==docker
- add installer ==download from docker.com
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*aOPB64oVyLC4xvq8.png?resize=700%2C394&ssl=1)
d. add sonarqube →
- add sonar scanner
- name ==sonar-scanner
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*5ssZ8U0JXzYEp30H.png?resize=700%2C394&ssl=1)
e. add owasp dependency check →
Adding the Dependency-Check plugin in the “Tools” section of Jenkins allows you to perform automated security checks on the dependencies used by your application
- add dependency check
- name == DP-Check
- from add installer select install from github.com
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*RiXbBn9jnp3AbOUl.png?resize=700%2C394&ssl=1)
Step 4 →Configure global setting for sonarube
- go to manage jenkins →Configure global setting →add sonarqube servers
- name ==sonar-server
- server_url==http://public_ip:9000
- server authentication token == jenkins →it is created in sonarqube security configurations
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*xGV7ILKT65-3Mo-BL5XXsA.png?resize=700%2C319&ssl=1)
4. let’s run the Pipeline →
- go to new item →select pipeline →in the name section type amazon-pipeline
- scroll down to the pipeline script and copy paste the following code
pipeline{
agent any
tools{
jdk 'jdk17'
nodejs 'node16'
}
environment {
SCANNER_HOME=tool 'sonar-scanner'
}
stages {
stage('clean workspace'){
steps{
cleanWs()
}
}
stage('Checkout from Git'){
steps{
git branch: 'main', url: 'https://github.com/Aakibgithuber/Amazon-app-Deployment-using-terraform-and-jenkins.git'
}
}
stage("Sonarqube Analysis "){
steps{
withSonarQubeEnv('sonar-server') {
sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Amazon \
-Dsonar.projectKey=Amazon '''
}
}
}
stage("quality gate"){
steps {
script {
waitForQualityGate abortPipeline: false, credentialsId: 'jenkins'
}
}
}
stage('Install Dependencies') {
steps {
sh "npm install"
}
}
stage('OWASP FS SCAN') {
steps {
dependencyCheck additionalArguments: '--scan ./ --disableYarnAudit --disableNodeAudit', odcInstallation: 'DP-Check'
dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
}
}
stage('TRIVY FS SCAN') {
steps {
sh "trivy fs . > trivyfs.txt"
}
}
stage("Docker Build & Push"){
steps{
script{
withDockerRegistry(credentialsId: 'docker', toolName: 'docker'){
sh "docker build -t amazon-clone ."
sh "docker tag amazon-clone aakibkhan1212/amazon-clone:latest "
sh "docker push aakibkhan1212/amazon-clone:latest "
}
}
}
}
stage("TRIVY"){
steps{
sh "trivy image aakibkhan1212/amazon-clone:latest > trivyimage.txt"
}
}
stage('Deploy to container'){
steps{
sh 'docker run -d --name amazon-clone -p 3000:3000 aakibkhan1212/amazon-clone:latest'
}
}
}
}
3. click apply and save
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:400/1*13t_jxiMDAmjeGtTtKYx1g.png?resize=320%2C433&ssl=1)
4. click on build now → it will take about 10–15 min
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*aTVUd6VN-GiKqBDG3ZXI8A.png?resize=700%2C394&ssl=1)
5. you could checkout the console output
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*I8bl2yrYyFUyDRfGQzDs0Q.png?resize=700%2C394&ssl=1)
6. owasp dependency check result
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*2bzM4MFEvvHhRwSQd2jIVg.png?resize=700%2C394&ssl=1)
7. After a lot of error we did it
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*TFNGK-IE24rN07lap9Vb3Q.png?resize=700%2C394&ssl=1)
8. Now our docker image is build,push and deployed into a container and our app is live and running on port no. 3000
to check →http://<your-public-ip>:3000
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*FukOSlWObI03DbLUrAHMHA.png?resize=700%2C394&ssl=1)
Here’s is our amazon app
Step5 →Monitering via Prmotheus and grafana
- Prometheus is like a detective that constantly watches your software and gathers data about how it’s performing. It’s good at collecting metrics, like how fast your software is running or how many users are visiting your website.
- Grafana, on the other hand, is like a dashboard designer. It takes all the data collected by Prometheus and turns it into easy-to-read charts and graphs. This helps you see how well your software is doing at a glance and spot any problems quickly.
In other words, Prometheus collects the information, and Grafana makes it look pretty and understandable so you can make decisions about your software. They’re often used together to monitor and manage applications and infrastructure.
1. Setup another server or EC2 for moniterning
- go to ec2 console and launch an instance having a base image ofu buntu and with t2.medium specs because Minimum Requirements to Install Prometheus :
- 2 CPU cores.
- 4 GB of memory.
- 20 GB of free disk space.
2. Installing Prometheus:
- First, create a dedicated Linux user for Prometheus and download Prometheus:
sudo useradd — system — no-create-home — shell /bin/false prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.47.1/prometheus-2.47.1.linux-amd64.tar.gz
2. Extract Prometheus files, move them, and create directories:
tar -xvf prometheus-2.47.1.linux-amd64.tar.gz
cd prometheus-2.47.1.linux-amd64/
sudo mkdir -p /data /etc/prometheus
sudo mv prometheus promtool /usr/local/bin/
sudo mv consoles/ console_libraries/ /etc/prometheus/
sudo mv prometheus.yml /etc/prometheus/prometheus.yml
3. Set ownership for directories:
useradd prometheus
sudo chown -R prometheus:prometheus /etc/prometheus/ /data/
4. Create a systemd unit configuration file for Prometheus:
sudo nano /etc/systemd/system/prometheus.service
Add the following code to the prometheus.service
file:
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=500
StartLimitBurst=5[Service]
User=prometheus
Group=prometheus
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/data \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries \
--web.listen-address=0.0.0.0:9090 \
--web.enable-lifecycle[Install]
WantedBy=multi-user.target
b. press →ctrl+o #for save and then ctrl+x #for exit from the file
Here’s a explanation of the key parts in this above file:
User
andGroup
specify the Linux user and group under which Prometheus will run.ExecStart
is where you specify the Prometheus binary path, the location of the configuration file (prometheus.yml
), the storage directory, and other settings.web.listen-address
configures Prometheus to listen on all network interfaces on port 9090.web.enable-lifecycle
allows for management of Prometheus through API calls.
5. Enable and start Prometheus:
sudo systemctl enable prometheus
sudo systemctl start prometheus
sudo systemctl status prometheus
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*PjoKfleZlBFhoF_k.png?resize=700%2C394&ssl=1)
Now go to your security group of your ec2 to enable port 9090 in which prometheus will run
go to → http://public_ip:9090 to see the webpage of prometheus
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*BhvNom4i9aAljRkB.png?resize=700%2C394&ssl=1)
3. Installing Node Exporter:
Node exporter is like a “reporter” tool for Prometheus, which helps collect and provide information about a computer (node) so Prometheus can monitor it. It gathers data about things like CPU usage, memory, disk space, and network activity on that computer.
A Node Port Exporter is a specific kind of Node Exporter that is used to collect information about network ports on a computer. It tells Prometheus which network ports are open and what kind of data is going in and out of those ports. This information is useful for monitoring network-related activities and can help you ensure that your applications and services are running smoothly and securely.
Run the following commands for installation
- Create a system user for Node Exporter and download Node Exporter:
sudo useradd — system — no-create-home — shell /bin/false node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
2. Extract Node Exporter files, move the binary, and clean up:
tar -xvf node_exporter-1.6.1.linux-amd64.tar.gz
sudo mv node_exporter-1.6.1.linux-amd64/node_exporter /usr/local/bin/
rm -rf node_exporter*
3. Create a systemd unit configuration file for Node Exporter:
sudo nano /etc/systemd/system/node_exporter.service
add the following code to the node_exporter.service
file:
provide more detailed information about what might be going wrong. For example:
[Unit]
Description=Node Exporter
After=network.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=default.target
4. Enable and start Node Exporter:
sudo useradd -m -s /bin/bash node_exporter
sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter
sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter
sudo systemctl status node_exporter
node exporter service is now running
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*5F9vrcISLib8QYmE.png?resize=700%2C394&ssl=1)
You can access Node Exporter metrics in Prometheus.
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*kGYzNQlCocP4EhATV9hxUA.png?resize=700%2C394&ssl=1)
5. Configure Prometheus Plugin Integration:
- go to your EC2 and run →
cd /etc/prometheus
2. you have to edit the prometheus.yml file to moniter anything
scrape_configs:
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
- job_name: 'jenkins'
metrics_path: '/prometheus'
static_configs:
- targets: ['<your-jenkins-ip>:<your-jenkins-port>']
add the above code with proper indentation like this →
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*YbmsoXtZCUhaH9rM.png?resize=700%2C394&ssl=1)
press esc+:wq to save and exit
a. Check the validity of the configuration file →
promtool check config /etc/prometheus/prometheus.yml
o/p →success
b. Reload the Prometheus configuration without restarting →
curl -X POST http://localhost:9090/-/reload
go to your prometheus tab again and click on status and select targets you will there is three targets present as we enter in yaml file for moniterning
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*7IVQKAJTRdASUKba.png?resize=700%2C394&ssl=1)
prometheus targets dashboard
5. Setup Grafana
Install Dependencies:
sudo apt-get update
sudo apt-get install -y apt-transport-https software-properties-common
Add the GPG Key for Grafana:
wget -q -O — https://packages.grafana.com/gpg.key | sudo apt-key add -
Add the repository for Grafana stable releases:
echo “deb https://packages.grafana.com/oss/deb stable main” | sudo tee — a /etc/apt/sources.list.d/grafana.list
Update the package list , install and start Grafana:
sudo apt-get update
sudo apt-get -y install grafana
sudo systemctl enable grafana-server
sudo systemctl start grafana-server
sudo systemctl status grafana-server
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*IJ9SQoOd_T5ySYw8.png?resize=700%2C394&ssl=1)
Now go to your ec2 security group and open port no. 3000 in which grafana runs
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*-sHhvEpQIiE-fB2P.png?resize=700%2C394&ssl=1)
Go and browse http://public_ip:3000 to access your grafana web interface
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*8cYgzKVkMeolLr1t.png?resize=700%2C394&ssl=1)
username = admin, password =admin
6. Add Prometheus Data Source:
To visualize metrics, you need to add a data source.
Follow these steps:
- Click on the gear icon (⚙️) in the left sidebar to open the “Configuration” menu.
- Select “Data Sources.”
- Click on the “Add data source” button.
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*MzjDuvGCpPadp98o.png?resize=700%2C138&ssl=1)
- Choose “Prometheus” as the data source type.
- In the “HTTP” section:
- Set the “URL” to
http://localhost:9090
(assuming Prometheus is running on the same server). - Click the “Save & Test” button to ensure the data source is working.
7. Import a Dashboard
Importing a dashboard in Grafana is like using a ready-made template to quickly create a set of charts and graphs for monitoring your data, without having to build them from scratch.
- Click on the “+” (plus) icon in the left sidebar to open the “Create” menu.
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:371/0*ZttyzKSa1meKybhp.png?resize=297%2C184&ssl=1)
- Select “Dashboard.”
- Click on the “Import” dashboard option.
- Enter the dashboard code you want to import (e.g., code 1860).
- Click the “Load” button.
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*p6sFchb1ZwFOROW2.png?resize=700%2C670&ssl=1)
- Select the data source you added (Prometheus) from the dropdown.
- Click on the “Import” button.
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*eoEtL7NcwUQsU-8m.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*-fpU-VFEyiYiDSgQwfg4Ew.png?resize=700%2C394&ssl=1)
8. Configure global setting for promotheus
go to manage jenkins →system →search for promotheus — apply →save
9. import a dashboard for jenkins
- Click on the “+” (plus) icon in the left sidebar to open the “Create” menu.
- Select “Dashboard.”
- Click on the “Import” dashboard option.
- Enter the dashboard code you want to import (e.g., code 9964).
- Click the “Load” button.
- Select the data source you added (Prometheus) from the dropdown.
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*BMg6vicppbF-jK7QR30QEA.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*FukOSlWObI03DbLUrAHMHA.png?resize=700%2C394&ssl=1)
Step 6 → Terraform Destroy
- go to your terminal and run
terraform destroy
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*Zma-KJnE9O24gvIV3gWygw.png?resize=700%2C394&ssl=1)