Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Build and Push Docker Image to ECR

on:
push:
branches:
- main # Runs when code is pushed to the main branch

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Set Up AWS CLI
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Login to Amazon ECR
run: |
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.ECR_REGISTRY }}

- name: Build and Tag Docker Image
run: |
IMAGE_TAG=$(git rev-parse --short HEAD)
docker build -t ${{ secrets.ECR_REPO }}:$IMAGE_TAG -f fastapi-service/Dockerfile fastapi-service/
docker tag ${{ secrets.ECR_REPO }}:$IMAGE_TAG ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:$IMAGE_TAG
docker tag ${{ secrets.ECR_REPO }}:$IMAGE_TAG ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:latest

- name: Push Docker Image to ECR
run: |
IMAGE_TAG=$(git rev-parse --short HEAD)
docker push ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:$IMAGE_TAG
docker push ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:latest
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use a lightweight Python image
FROM python:3.9-slim

# Set the working directory
WORKDIR /app

# Copy only the necessary files first
COPY requirements.txt .

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the FastAPI application files
COPY . .

# Expose the application port
EXPOSE 8000

# Run the FastAPI application using Uvicorn
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
5 changes: 5 additions & 0 deletions fastapi-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install fastapi uvicorn
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
20 changes: 20 additions & 0 deletions fastapi-service/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: fastapi-service
spec:
replicas: 2
selector:
matchLabels:
app: fastapi-service
template:
metadata:
labels:
app: fastapi-service
spec:
containers:
- name: fastapi-service
image: 503147168047.dkr.ecr.ap-south-1.amazonaws.com/fastapi-service:latest # Updated with ECR details
ports:
- containerPort: 8000
imagePullPolicy: Always # Ensures the latest image is pulled on deployment
7 changes: 7 additions & 0 deletions fastapi-service/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
return {"message": "Hello from FastAPI in Kubernetes!"}
12 changes: 12 additions & 0 deletions fastapi-service/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: fastapi-service
spec:
selector:
app: fastapi-service
ports:
- protocol: TCP
port: 80
targetPort: 8000
type: LoadBalancer # Exposes service externally
19 changes: 19 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from fastapi import FastAPI

# Create a FastAPI instance
app = FastAPI()

# Define a root endpoint
@app.get("/")
def read_root():
return {"message": "Hello from FastAPI running on Kubernetes!"}

# Example endpoint with a parameter
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "query": q}

# Health check endpoint (useful for Kubernetes readiness and liveness probes)
@app.get("/health")
def health_check():
return {"status": "healthy"}
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fastapi
uvicorn
1 change: 1 addition & 0 deletions test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hi