Skip to content

Commit 7da002f

Browse files
authored
Merge pull request #1 from cva-i/deployment-setup
Deployment setup
2 parents f29582a + 8a448f8 commit 7da002f

10 files changed

Lines changed: 129 additions & 3010 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Deploy Lambda Service
2+
3+
on:
4+
push:
5+
branches: [ master, main ]
6+
workflow_dispatch:
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v3
15+
16+
- name: Set up Docker Buildx
17+
uses: docker/setup-buildx-action@v2
18+
19+
- name: Configure AWS credentials
20+
uses: aws-actions/configure-aws-credentials@v1
21+
with:
22+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
23+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
24+
aws-region: ${{ secrets.AWS_REGION }}
25+
26+
- name: Login to Amazon ECR
27+
id: login-ecr
28+
uses: aws-actions/amazon-ecr-login@v1
29+
30+
# Set ECR image URI as environment variables
31+
- name: Set ECR image URIs
32+
id: set-image-uris
33+
run: |
34+
ECR_REGISTRY="${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com"
35+
ECR_REPOSITORY="${{ secrets.ECR_REPOSITORY }}"
36+
IMAGE_TAG_LATEST="$ECR_REGISTRY/$ECR_REPOSITORY:latest"
37+
IMAGE_TAG_SHA="$ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }}"
38+
echo "image_uri_latest=$IMAGE_TAG_LATEST" >> $GITHUB_OUTPUT
39+
echo "image_uri_sha=$IMAGE_TAG_SHA" >> $GITHUB_OUTPUT
40+
echo "image_tags=$IMAGE_TAG_SHA,$IMAGE_TAG_LATEST" >> $GITHUB_OUTPUT
41+
42+
- name: Build and push Docker image
43+
uses: docker/build-push-action@v4
44+
with:
45+
context: .
46+
file: ./Dockerfile.lambda
47+
platforms: linux/arm64
48+
push: true
49+
tags: ${{ steps.set-image-uris.outputs.image_tags }}
50+
51+
- name: Update Lambda function
52+
run: |
53+
aws lambda update-function-code \
54+
--function-name ${{ secrets.LAMBDA_FUNCTION_NAME }} \
55+
--image-uri ${{ steps.set-image-uris.outputs.image_uri_latest }} \
56+
--region ${{ secrets.AWS_REGION }}
57+
58+
- name: Wait for Lambda update
59+
run: |
60+
aws lambda wait function-updated \
61+
--function-name ${{ secrets.LAMBDA_FUNCTION_NAME }} \
62+
--region ${{ secrets.AWS_REGION }}
63+
64+
- name: Test Lambda function
65+
run: |
66+
echo "Lambda function updated successfully!"
67+
echo "Image deployed: ${{ steps.set-image-uris.outputs.image_uri_latest }}"
68+
echo "Function URL: $(aws lambda get-function-url-config --function-name ${{ secrets.LAMBDA_FUNCTION_NAME }} --query 'FunctionUrl' --output text --region ${{ secrets.AWS_REGION }} || echo 'No function URL configured')"

Dockerfile.lambda

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#! Dockerfile
2+
3+
FROM public.ecr.aws/lambda/python:3.13
4+
5+
COPY src/ ${LAMBDA_TASK_ROOT}/src/
6+
COPY pyproject.toml poetry.lock ${LAMBDA_TASK_ROOT}/
7+
8+
RUN microdnf install -y poppler poppler-utils
9+
10+
RUN pip install --no-cache-dir poetry && \
11+
poetry config virtualenvs.create false && \
12+
poetry install --without dev --no-interaction --no-ansi
13+
14+
CMD ["src.handler.handler"]
File renamed without changes.

docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ version: '3.8'
22

33
services:
44
pdf-converter:
5-
build: .
5+
build:
6+
context: .
7+
dockerfile: Dockerfile.local
68
ports:
79
- "8069:8000"
810
environment:

hui

Lines changed: 0 additions & 1 deletion
This file was deleted.

poetry.lock

Lines changed: 17 additions & 3002 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ uvicorn = ">=0.27.1"
1515
pdf2image = ">=1.17.0"
1616
pydantic = ">=2.6.0"
1717
python-multipart = ">=0.0.9"
18-
mangum = ">=0.17.0"
1918
pyyaml = ">=6.0.1"
2019
poppler-utils = {version = "*", platform = "linux"}
21-
magnum = "^19.0.0"
20+
mangum = "^0.19.0"
2221

2322
[tool.poetry.group.dev.dependencies]
2423
python-dotenv = ">=1.0.0"

res

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/app/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from fastapi import FastAPI, HTTPException, Response
22
from pydantic import BaseModel
33
from typing import List
4-
from app.utils import pdf_to_pngs
4+
from src.app.utils import pdf_to_pngs
55
import base64
66
import yaml
77

@@ -14,10 +14,12 @@
1414
redoc_url="/redoc"
1515
)
1616

17+
1718
class PDFRequest(BaseModel):
1819
pdf: str
1920
dpi: int = 200
2021

22+
2123
@app.post("/convert", response_model=List[str])
2224
async def convert_pdf(request: PDFRequest):
2325
"""
@@ -35,6 +37,7 @@ async def convert_pdf(request: PDFRequest):
3537
except Exception as e:
3638
raise HTTPException(status_code=400, detail=str(e))
3739

40+
3841
@app.get("/openapi.yaml", include_in_schema=False)
3942
async def get_openapi_yaml():
4043

src/handler.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
from mangum import Mangum
2-
from app.main import app
2+
from src.app.main import app
3+
import logging
34

4-
handler = Mangum(app)
5+
# Configure logging
6+
logging.basicConfig(level=logging.INFO)
7+
logger = logging.getLogger()
8+
9+
def handler(event, context):
10+
"""Lambda function handler with improved error handling"""
11+
logger.info(f"Received event: {event}")
12+
13+
# Initialize Mangum with sensible defaults for Lambda
14+
asgi_handler = Mangum(app, lifespan="off")
15+
16+
try:
17+
return asgi_handler(event, context)
18+
except Exception as e:
19+
logger.exception(f"Error processing request: {str(e)}")
20+
return {
21+
"statusCode": 500,
22+
"body": "Internal Server Error",
23+
"headers": {"Content-Type": "text/plain"}
24+
}

0 commit comments

Comments
 (0)