Running an ML Farming Platform on AWS: Lambda, S3, API Gateway, and DynamoDB
How to move a FastAPI and React machine-learning platform, with Prophet climate forecasts, Random Forest fertilizer advice, and Naive Bayes text mining, onto serverless AWS. Covers which service replaces each local component and why SQLite had to go.
The starting point
AGRI-INSIGHT is an agricultural decision-support platform with three modules:
- Climate forecasting with Prophet
- Fertilizer recommendation with Random Forest
- Text mining with TF-IDF + Naive Bayes
Locally it runs as a React 19 + Vite frontend, a FastAPI backend, and a SQLite database. That is fine for development, but it ties everything to one machine. This article walks through how each piece maps onto AWS.
Architecture overview
Local component | AWS service | Why |
|---|---|---|
React + Vite build | Amazon S3 + CloudFront | Static hosting with a global CDN, no server to manage |
FastAPI backend | AWS Lambda (container image) + Amazon API Gateway | Pay only when requests arrive; scales automatically |
Trained models (Prophet, Random Forest, Naive Bayes) | Amazon S3 | Versioned model artifacts loaded by the backend |
SQLite | Amazon DynamoDB | Durable, serverless storage that works with Lambda |
Model retraining | Amazon EventBridge + AWS Lambda / Amazon SageMaker | Scheduled retraining as new data arrives |
Logs and monitoring | Amazon CloudWatch | Request logs, errors, and latency in one place |
Access control | AWS IAM | Least-privilege permissions for each component |
Frontend: S3 and CloudFront
The Vite build produces static files. Upload them to an S3 bucket and put CloudFront in front. Users get fast loading from edge locations, and the bucket stays private behind CloudFront. There is no frontend server to patch or scale.
Backend: FastAPI on Lambda with API Gateway
FastAPI can run on Lambda using an ASGI adapter such as Mangum. Package the app as a Lambda container image, which matters here because libraries like Prophet and scikit-learn are too large for a standard zip deployment. API Gateway receives the HTTP requests and forwards them to the function.
Each module becomes an endpoint (forecast, recommend, classify), and Lambda scales each one with traffic. For a platform with bursty usage, such as farmers checking forecasts in the morning, you pay per request instead of for an idle server.
Models: load from S3
Store each trained model as a versioned file in S3. The Lambda function loads the model at cold start and keeps it in memory for subsequent requests. Updating a model means uploading a new file and pointing the function at it, with no code redeploy.
Why SQLite had to go
SQLite writes to a local file, and Lambda's local storage is temporary and not shared between instances. Data written there can disappear, and concurrent function instances cannot see each other's writes. The platform's records therefore moved to DynamoDB, which is durable, serverless, and scales with the same pay-per-use model as Lambda.
This was the biggest design change in the move. It is a common trap when taking a local prototype to serverless.
Keeping models fresh
Climate and agricultural data change over time. An EventBridge schedule can trigger a retraining job: a Lambda function for light models like Naive Bayes, or a SageMaker job if the data grows. The new model file is written to S3 and picked up by the API.
Security and operations
- Give each Lambda function its own IAM role with access only to the buckets and tables it needs.
- Keep the model bucket private and serve the frontend only through CloudFront.
- Use CloudWatch logs and alarms to catch errors and slow responses.
When to use a managed NLP service instead
The text-mining module uses TF-IDF + Naive Bayes because it is cheap, fast, and explainable. If document volume or language variety grows, Amazon Comprehend is a managed alternative that removes the need to maintain the classifier. A good approach is to keep the lightweight model first and move to the managed service only when scale demands it.
Takeaway
A machine-learning prototype becomes a scalable product by replacing each local piece with a managed service: S3 and CloudFront for the frontend, Lambda and API Gateway for the API, S3 for models, DynamoDB for data, and EventBridge for retraining. The one piece that does not carry over unchanged is the database, so plan that migration first.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article