This project deploys a FastAPI application that classifies images as either cats or dogs using a deep learning model. The application is containerized and deployed to AWS Lambda.
- Install AWS CLI:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
- Create an IAM User:
- Go to AWS Console > IAM > Users > Create User
- Create a new policy with the following permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudformation:*",
"ecr:*",
"ssm:*",
"s3:*",
"iam:*"
],
"Resource": "*"
}
]
}
- Attach the policy to your user
- Create access keys for the user
- Configure AWS CLI:
aws configure
Enter your AWS access key ID, secret access key, default region, and output format.
- Install Node.js (if not already installed)
- Install AWS CDK:
npm install -g aws-cdk
- Install development requirements:
pip install -r requirements-dev.txt
- Convert PyTorch model to ONNX:
# Place your PyTorch model (model.pt) in the project root
python export_onnx.py
This will create model.onnx
in your project directory.
- Install application requirements:
pip install -r requirements.txt
Run the FastAPI application locally:
uvicorn app:app --reload
Visit http://localhost:8000 to test the application.
or use the client to test the application:
python client.py
Build the Docker image locally:
docker build -t cat-dog-classifier .
Test the Docker container:
docker run -p 8000:8000 cat-dog-classifier
- Bootstrap CDK (first time only):
cdk bootstrap
- Deploy the application:
cdk deploy
After deployment, CDK will output the Lambda function URL. You can use this URL to access your application.
To remove the deployed resources:
cdk destroy
- If
cdk bootstrap
fails, verify your AWS credentials and permissions - If the Docker build fails, ensure all required files are in place
- If the Lambda deployment fails, check CloudWatch logs for details
- The Lambda function is configured with 8GB of memory and a 5-minute timeout
- The endpoint is public (no authentication required)
- The model expects images to be resized to 160x160 pixels