DevSecOps - 1
DevSecOps - 1
Setting Up EC2
1. EC2 Instance Launch:
3. Elastic IP Association:
Update and Clone: First, update the instance and clone the
application code from GitHub.
git clone <URL>
The command sudo usermod -aG docker $USER adds the current
user to the docker group. It allows the user to run Docker
commands without needing sudo privileges every time.
Docker container
Accessing the app without TMDB API Key
1. TMDB?
TMDB (The Movie Database) is an online database that
provides movie and TV show data through an API. We can use
it to fetch information like movie titles, ratings, cast, etc.
• Go to TMDB.
Install OpenJDK :
Verify Java Installation & Install Jenkins
# Jenkins
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc ht
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring
sudo apt-get update
sudo apt-get install jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
Jenkins UI
To unlock Jenkins for the �rst time, get the initial password:
Name: Give a name to the tool, e.g., DP-Check (this name will
be referenced in the pipeline).
Save con�guration.
pipeline {
agent any
tools {
jdk 'jdk17'
nodejs 'node16'
}
environment {
SCANNER_HOME = tool 'sonar-scanner'
}
stages {
stage('Clean Workspace') {
steps {
cleanWs() // Cleans the workspace befor
}
}
stage('Checkout from Git') {
steps {
git branch: 'main', url: '<URL>'
}
}
stage('Install Dependencies') {
steps {
sh "npm install" // Install Node.js dep
}
}
stage('OWASP Dependency-Check Scan') {
steps {
// Run Dependency-Check scan on the pro
dependencyCheck additionalArguments
Docker
Docker Commons
Docker Pipeline
Docker API
docker-build-step
This command will pull the o�cial SonarQube Docker image and
run it in detached mode ( -d ), mapping the container's port 9000
to the EC2 instance's port 9000.
IP>:8080 .
Click Save.
Click Save.
pipeline {
agent any
environment {
SONARQUBE = 'SonarQube' // Name of SonarQube se
}
stages {
stage('Build') {
steps {
script {
// Your build steps here
}
}
}
stage('SonarQube Analysis') {
steps {
script {
// Run SonarQube Scanner for analys
withSonarQubeEnv('SonarQube'
sh 'mvn clean install sonar:son
}
}
}
}
}
}
SonarQube Analysis
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: '<URL>'
}
}
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv('sonar-server'
sh '''
$SCANNER_HOME/bin/sonar-scanner -Ds
'''
}
}
}
stage('Quality Gate') {
steps {
script {
waitForQualityGate abortPipeline
}
}
}
stage('Install Dependencies') {
steps {
sh "npm install"
}
}
stage('OWASP FS Scan') {
steps {
dependencyCheck additionalArguments
dependencyCheckPublisher pattern
}
}
stage('TRIVY FS Scan') {
steps {
sh "trivy fs . > trivyfs.txt"
}
}
stage('Docker Build & Push') {
steps {
script {
withDockerRegistry(credentialsId:
sh "docker build --build-arg TM
sh "docker tag netflix sirishas
sh "docker push sirishassss/net
}
}
}
}
stage('TRIVY Image Scan') {
steps {
sh "trivy image sirishassss/netflix:lat
}
}
stage('Deploy to container') {
steps {
sh 'docker run -d -p 8081:80 sirishasss
}
}
}
}
Pipeline
Phase 4: Monitoring
We can set up Prometheus and Grafana on the same EC2 instance
as Jenkins server. However, if the instance starts running slowly
because it’s handling both Jenkins and the monitoring tools, it’s
better to separate them.
Installing Prometheus:
First, create a dedicated Linux user for Prometheus and
download Prometheus:
[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_libra
--web.listen-address=0.0.0.0:9090 \
--web.enable-lifecycle
[Install]
WantedBy=multi-user.target
User and Group specify the Linux user and group under
which Prometheus will run.
Extract Node Exporter �les, move the binary, and clean up:
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=500
StartLimitBurst=5
[Service]
User=node_exporter
Group=node_exporter
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/node_exporter --collector.logi
[Install]
WantedBy=multi-user.target
Prometheus Con�guration:
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
prometheus-ip>:9090/targets
Install Grafana
Grafana is an open-source data visualization and monitoring
platform that allows users to create interactive dashboards. It
integrates with various data sources, including Prometheus, to
visualize time-series data, metrics, and logs for monitoring
applications, infrastructure, and services.
ip>:3000
We’ll be prompted to log in to Grafana. The default username is
“admin,” and the default password is also “admin.”
When we log in for the �rst time, Grafana will prompt us to change
the default password for security reasons. Follow the prompts to
set a new password.
Click the “Save & Test” button to ensure the data source is
working.
Step 10: Import a Dashboard:
Click on the “+” (plus) icon in the le� sidebar to open the
“Create” menu.
Select “Dashboard.”
Cluster
First, we’ll need to install the AWS CLI on our machine. We can do
this by running the following commands:
aws configure
AWS Access Key ID: We can �nd this in our AWS account
(under IAM > Users > [your user] > Security Credentials >
Access keys).
AWS Secret Access Key: This is also available under IAM user’s
security credentials.
Now the AWS CLI is set up, we can use it to access EKS cluster by
updating kubecon�g �le. Run the following command:
aws eks update-kubeconfig --name "Cluster-Name"
Installing Helm
Helm is a package manager for Kubernetes that simpli�es the
deployment and management of applications and services. It
allows us to easily install and con�gure applications using “charts”
(pre-con�gured Kubernetes resources). To install Prometheus
Node Exporter (and other services) on Kubernetes cluster, we’ll
need to have Helm install
Argocd installation
LB
Argocd
4. Access the Application
• To Access the app make sure port 30007 is open in our security
group and then open a new tab paste NodeIP:30007, our app
should be running.
Cleanup
Cleanup AWS EC2 Instances: