Skip to content

disable to deploy with too Onnx files

0

I’m trying to deploy a FastAPI project using Docker and GitLab CI/CD.

However, the project includes large ONNX model files, which are too heavy to push to Git.
So far, I've been manually pushing them with docker build from my local machine, but it's time-consuming and not scalable.

To solve this, I created an S3 bucket and want to automatically download the ONNX files from S3 when the container starts.

I wrote code in main.py using boto3 to download files from S3 during the app's startup event, but it seems like the function is not even being called when deployed via ECS Fargate.

What I tried:

  • I used @app.on_event("startup") to download models from S3.
  • It works locally (uvicorn main:app) but not on ECS.
  • I confirmed that the environment variables like S3_MODEL_BUCKET, S3_MODEL_FILES, and S3_MODEL_DIR are set correctly.
  • I'm using gunicorn with uvicorn.workers.UvicornWorker as the CMD in Docker.
  • No error logs related to the startup function are shown, and the download function doesn't seem to be triggered at all.

What’s the best practice to:

  1. Download model files from S3 when a FastAPI app starts in ECS Fargate?
  2. Ensure the startup event runs properly in a production Docker environment?

Any advice or examples would be greatly appreciated!

2 Answers
0

Based on the information provided, it seems you're facing an issue with downloading ONNX model files from S3 during your FastAPI application startup in an ECS Fargate environment. This is a common challenge when working with large model files that shouldn't be included in Git repositories.

For downloading model files from S3 when a FastAPI app starts in ECS Fargate, here are some best practices:

  1. Use an entrypoint script instead of FastAPI startup events: Rather than relying on FastAPI's startup event, create a shell script as your Docker entrypoint that downloads the models from S3 before starting the application. This ensures the models are available before the application starts.

Example entrypoint.sh:

#!/bin/bash
# Download models from S3
python download_models.py
# Then start the application
exec gunicorn -k uvicorn.workers.UvicornWorker main:app
  1. Implement a health check endpoint: Create a health check endpoint in your FastAPI app that verifies the models are loaded correctly. This helps ECS determine if your container is truly ready to serve requests.

  2. Use AWS DataSync for larger transfers: For very large model files, consider using AWS DataSync to manage object transfers between your S3 bucket and the container filesystem.

  3. Consider using S3 mounting solutions: For production environments, you might want to explore solutions that can mount S3 buckets as local filesystems, allowing your application to access the models without downloading them first.

  4. Implement proper error handling and retries: When downloading from S3, implement robust error handling and retry mechanisms to handle temporary network issues.

  5. Use IAM roles for ECS tasks: Ensure your ECS task has the appropriate IAM role with permissions to access the S3 bucket containing your models.

The issue with your current approach might be related to how gunicorn workers initialize. When using multiple workers, each worker might be trying to download the models independently, or the startup event might not be firing as expected in the containerized environment.
Sources
Use the AWS InfluxDB migration script to migrate your InfluxDB OSS 2.x data to Amazon Timestream for InfluxDB | AWS Database Blog
Amazon S3 on Outposts | On-Premises Object Storage | AWS

answered a year ago

0

Hello.

Why not use EFS to host your model data instead of embedding it directly in your container?
https://repost.aws/articles/ARLakw4VYTTnCdFV33wOssxg/aws-re-invent-2024-supercharge-your-ai-and-ml-workloads-on-amazon-ecs

The following session video provides a demonstration of an application hosted on EFS, which may be helpful.
https://www.youtube.com/watch?v=6YfG7ca4wWM

EXPERT

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.