Skip to content

Commit 80e3996

Browse files
committed
Configured app.py and requirements.txt for monitoring
1 parent ad6f847 commit 80e3996

16 files changed

Lines changed: 1087 additions & 1043 deletions

File tree

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
1-
name: Pipeline for architecture
2-
3-
on:
4-
push:
5-
branches:
6-
- main
7-
8-
jobs:
9-
build_and_push:
10-
runs-on: ubuntu-latest
11-
12-
defaults:
13-
run:
14-
working-directory: Backend
15-
16-
steps:
17-
- name: Checkout Repository
18-
uses: actions/checkout@v4
19-
20-
- name: Setup python
21-
uses: actions/setup-python@v5
22-
with:
23-
python-version: "3.12"
24-
cache: pip
25-
26-
- name: Python version
27-
run: python --version
28-
29-
- name: Install Requirements
30-
run: pip install -r requirements.txt
31-
32-
- name: Verify app loads
33-
run: python -c "from app import app"
34-
35-
- name: Login to DockerHub
36-
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
37-
38-
- name: Build docker Image
39-
run: |
40-
docker build \
41-
-t lavanyasharma07/backend-terraform-three-tier-aws:${{ github.sha }} \
42-
-t lavanyasharma07/backend-terraform-three-tier-aws:latest \
43-
.
44-
45-
- name: Smoke Test Docker Image
46-
run: |
47-
docker run -d --name test-app -p 5000:5000 \
48-
lavanyasharma07/backend-terraform-three-tier-aws:${{ github.sha }}
49-
sleep 20
50-
curl -v http://localhost:5000/health
51-
docker logs test-app
52-
docker rm -f test-app
53-
54-
- name: Push Docker image
55-
run: |
56-
docker push lavanyasharma07/backend-terraform-three-tier-aws:${{ github.sha }}
1+
name: Pipeline for architecture
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build_and_push:
10+
runs-on: ubuntu-latest
11+
12+
defaults:
13+
run:
14+
working-directory: Backend
15+
16+
steps:
17+
- name: Checkout Repository
18+
uses: actions/checkout@v4
19+
20+
- name: Setup python
21+
uses: actions/setup-python@v5
22+
with:
23+
python-version: "3.12"
24+
cache: pip
25+
26+
- name: Python version
27+
run: python --version
28+
29+
- name: Install Requirements
30+
run: pip install -r requirements.txt
31+
32+
- name: Verify app loads
33+
run: python -c "from app import app"
34+
35+
- name: Login to DockerHub
36+
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
37+
38+
- name: Build docker Image
39+
run: |
40+
docker build \
41+
-t lavanyasharma07/backend-terraform-three-tier-aws:${{ github.sha }} \
42+
-t lavanyasharma07/backend-terraform-three-tier-aws:latest \
43+
.
44+
45+
- name: Smoke Test Docker Image
46+
run: |
47+
docker run -d --name test-app -p 5000:5000 \
48+
lavanyasharma07/backend-terraform-three-tier-aws:${{ github.sha }}
49+
sleep 20
50+
curl -v http://localhost:5000/health
51+
docker logs test-app
52+
docker rm -f test-app
53+
54+
- name: Push Docker image
55+
run: |
56+
docker push lavanyasharma07/backend-terraform-three-tier-aws:${{ github.sha }}
5757
docker push lavanyasharma07/backend-terraform-three-tier-aws:latest
Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
1-
name: Pipeline for architecture
2-
3-
on:
4-
push:
5-
branches:
6-
- main
7-
8-
jobs:
9-
build_and_push:
10-
runs-on: ubuntu-latest
11-
12-
defaults:
13-
run:
14-
working-directory: Frontend
15-
16-
steps:
17-
- name: Checkout Repository
18-
uses: actions/checkout@v4
19-
20-
- name: Setup python
21-
uses: actions/setup-python@v5
22-
with:
23-
python-version: "3.12"
24-
cache: pip
25-
26-
- name: Python version
27-
run: python --version
28-
29-
- name: Install Requirements
30-
run: pip install -r requirements.txt
31-
32-
- name: Verify app loads
33-
run: python -c "from app import app"
34-
35-
- name: Login to DockerHub
36-
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
37-
38-
- name: Build docker Image
39-
run: |
40-
docker build \
41-
-t lavanyasharma07/frontend-terraform-three-tier-aws:${{ github.sha }} \
42-
-t lavanyasharma07/frontend-terraform-three-tier-aws:latest \
43-
.
44-
45-
- name: Smoke Test Docker Image
46-
run: |
47-
docker run -d --name test-app -p 5000:5000 \
48-
lavanyasharma07/frontend-terraform-three-tier-aws:${{ github.sha }}
49-
sleep 20
50-
curl -v http://localhost:5000/health
51-
docker logs test-app
52-
docker rm -f test-app
53-
54-
- name: Push Docker image
55-
run: |
56-
docker push lavanyasharma07/frontend-terraform-three-tier-aws:${{ github.sha }}
1+
name: Pipeline for architecture
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build_and_push:
10+
runs-on: ubuntu-latest
11+
12+
defaults:
13+
run:
14+
working-directory: Frontend
15+
16+
steps:
17+
- name: Checkout Repository
18+
uses: actions/checkout@v4
19+
20+
- name: Setup python
21+
uses: actions/setup-python@v5
22+
with:
23+
python-version: "3.12"
24+
cache: pip
25+
26+
- name: Python version
27+
run: python --version
28+
29+
- name: Install Requirements
30+
run: pip install -r requirements.txt
31+
32+
- name: Verify app loads
33+
run: python -c "from app import app"
34+
35+
- name: Login to DockerHub
36+
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
37+
38+
- name: Build docker Image
39+
run: |
40+
docker build \
41+
-t lavanyasharma07/frontend-terraform-three-tier-aws:${{ github.sha }} \
42+
-t lavanyasharma07/frontend-terraform-three-tier-aws:latest \
43+
.
44+
45+
- name: Smoke Test Docker Image
46+
run: |
47+
docker run -d --name test-app -p 5000:5000 \
48+
lavanyasharma07/frontend-terraform-three-tier-aws:${{ github.sha }}
49+
sleep 20
50+
curl -v http://localhost:5000/health
51+
docker logs test-app
52+
docker rm -f test-app
53+
54+
- name: Push Docker image
55+
run: |
56+
docker push lavanyasharma07/frontend-terraform-three-tier-aws:${{ github.sha }}
5757
docker push lavanyasharma07/frontend-terraform-three-tier-aws:latest

Backend/Dockerfile

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
FROM python:3.12-slim
2-
3-
WORKDIR /app
4-
5-
COPY requirements.txt .
6-
7-
RUN pip install --no-cache-dir -r requirements.txt
8-
9-
COPY . .
10-
11-
EXPOSE 5000
12-
13-
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
14-
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:5000/health')"
15-
16-
CMD ["sh", "-c", "gunicorn -w ${WORKERS:-1} -b 0.0.0.0:5000 app:app"]
1+
FROM python:3.12-slim
2+
3+
WORKDIR /app
4+
5+
COPY requirements.txt .
6+
7+
RUN pip install --no-cache-dir -r requirements.txt
8+
9+
COPY . .
10+
11+
EXPOSE 5000
12+
13+
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
14+
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:5000/health')"
15+
16+
CMD ["sh", "-c", "gunicorn -w ${WORKERS:-1} -b 0.0.0.0:5000 app:app"]

Backend/app.py

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,56 @@
1-
from flask import Flask
2-
import os
3-
import redis
4-
5-
redis_client = redis.Redis(
6-
host=os.getenv("REDIS_HOST", "localhost"),
7-
port=int(os.getenv("APP_REDIS_PORT", 6379)),
8-
decode_responses=True
9-
)
10-
app = Flask(__name__)
11-
12-
@app.route("/")
13-
def home():
14-
count = redis_client.incr("visitor_count")
15-
return f"""
16-
<h1>AWS Production Flask App!</h1>
17-
<p>Visitor count: {count}</p>
18-
<p>Backend App is running on port 5000</p>
19-
"""
20-
21-
@app.route("/health")
22-
def health():
23-
try:
24-
redis_client.ping()
25-
return "Application Healthy"
26-
except Exception as e:
27-
return f"Application Unhealthy: {str(e)}"
28-
29-
@app.route("/about")
30-
def about():
31-
return "Built by Harshu during Cloud Engineering Journey"
32-
33-
if __name__ == "__main__":
34-
app.run(host="0.0.0.0", port=5000)
1+
from flask import Flask, Response
2+
import os
3+
import redis
4+
from prometheus_client import Counter, Histogram, generate_latest, Gauge, CONTENT_TYPE_LATEST # type: ignore[import]
5+
import time
6+
7+
REQUEST_COUNT = Counter("http_requests_total", "Total HTTP Requests", ["method", "endpoint", "http_status"])
8+
REQUEST_LATENCY = Histogram("http_request_duration_seconds", "HTTP Request Latency", ["method", "endpoint"])
9+
ACTIVE_REQUESTS = Gauge("active_requests", "Number of Active Requests")
10+
11+
redis_client = redis.Redis(
12+
host=os.getenv("REDIS_HOST", "localhost"),
13+
port=int(os.getenv("APP_REDIS_PORT", 6379)),
14+
decode_responses=True
15+
)
16+
app = Flask(__name__)
17+
18+
@app.route("/")
19+
def home():
20+
ACTIVE_REQUESTS.inc()
21+
start_time = time.time()
22+
23+
try:
24+
REQUEST_COUNT.labels(method="GET", endpoint="/", http_status=200).inc()
25+
count = redis_client.incr("visitor_count")
26+
except Exception as e:
27+
return f"Error occurred: {str(e)}"
28+
29+
finally:
30+
latency = time.time() - start_time
31+
REQUEST_LATENCY.observe(latency)
32+
ACTIVE_REQUESTS.dec()
33+
return f"""
34+
<h1>AWS Production Flask App!</h1>
35+
<p>Visitor count: {count}</p>
36+
<p>Backend App is running on port 5000</p>
37+
"""
38+
39+
@app.route("/health")
40+
def health():
41+
try:
42+
redis_client.ping()
43+
return "Application Healthy"
44+
except Exception as e:
45+
return f"Application Unhealthy: {str(e)}"
46+
47+
@app.route("/about")
48+
def about():
49+
return "Built by Harshu during Cloud Engineering Journey"
50+
51+
if __name__ == "__main__":
52+
app.run(host="0.0.0.0", port=5000)
53+
54+
@app.route("/metrics")
55+
def metrics():
56+
return Response(generate_latest(), 200, {"Content-Type": CONTENT_TYPE_LATEST})

Backend/requirements.txt

38 Bytes
Binary file not shown.

Frontend/Dockerfile

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
FROM python:3.12-slim
2-
3-
WORKDIR /app
4-
5-
COPY requirements.txt .
6-
7-
RUN pip install --no-cache-dir -r requirements.txt
8-
9-
COPY . .
10-
11-
EXPOSE 5000
12-
13-
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
14-
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:5000/health')"
15-
16-
CMD ["sh", "-c", "gunicorn -w ${WORKERS:-1} -b 0.0.0.0:5000 app:app"]
1+
FROM python:3.12-slim
2+
3+
WORKDIR /app
4+
5+
COPY requirements.txt .
6+
7+
RUN pip install --no-cache-dir -r requirements.txt
8+
9+
COPY . .
10+
11+
EXPOSE 5000
12+
13+
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
14+
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:5000/health')"
15+
16+
CMD ["sh", "-c", "gunicorn -w ${WORKERS:-1} -b 0.0.0.0:5000 app:app"]

0 commit comments

Comments
 (0)