Ever wanted to deploy a Hugging Face model to AWS Lambda but got stuck with container builds, cold starts, and model caching? Here's how to do it in under 5 minutes using Scaffoldly.
Create an EFS filesystem named .cache in AWS:
Create your app from the python-huggingface branch:
npx scaffoldly create app --template python-huggingface
Deploy it:
cd my-app && npx scaffoldly deploy
That's it! You'll get a Hugging Face model running on Lambda (using openai-community/gpt2 as an example), complete with proper caching and container deployment.
Pro-Tip: For the EFS setup, you can customize it down to a Single AZ in Burstable mode for even more cost savings. Scaffoldly will match the Lambda Function to the EFS's VPC, Subnets, and Security Group.
✨ Check out the Live Demo and the example code!
Deploying ML models to AWS Lambda traditionally involves:
It's a lot of infrastructure work when you just want to serve a model!
Scaffoldly handles all this complexity with a simple configuration file. Here's a complete application that serves a Hugging Face model (using openai-community/gpt2 as an example):
# app.py from flask import Flask from transformers import pipeline app = Flask(__name__) generator = pipeline('text-generation', model='openai-community/gpt2') @app.route("/") def hello_world(): output = generator("Hello, world,") return output[0]['generated_text']
// requirements.txt Flask ~= 3.0 gunicorn ~= 23.0 torch ~= 2.5 numpy ~= 2.1 transformers ~= 4.46 huggingface_hub[cli] ~= 0.26
// scaffoldly.json { "name": "python-huggingface", "runtime": "python:3.12", "handler": "localhost:8000", "files": ["app.py"], "packages": ["pip:requirements.txt"], "resources": ["arn::elasticfilesystem:::file-system/.cache"], "schedules": { "@immediately": "huggingface-cli download openai-community/gpt2" }, "scripts": { "start": "gunicorn app:app" }, "memorySize": 1024 }
Scaffoldly does some clever things behind the scenes:
Smart Container Building:
Efficient Model Handling:
Lambda-Ready Setup:
Here's output from a npx scaffoldly deploy command I ran on this example:
✅ Costs: ~$0.20/day for AWS Lambda, ECR, and EFS
✅ Cold Start: ~20s for first request (model loading)
✅ Warm Requests: 5-20s (CPU-based inference)
While this setup uses CPU inference (which is slower than GPU), it's an incredibly cost-effective way to experiment with ML models or serve low-traffic endpoints.
Want to use a different model? Just update two files:
npx scaffoldly create app --template python-huggingface
cd my-app && npx scaffoldly deploy
Scaffoldly supports private and gated models via the HF_TOKEN environment variable. You can add your Hugging Face token in several ways:
# app.py from flask import Flask from transformers import pipeline app = Flask(__name__) generator = pipeline('text-generation', model='openai-community/gpt2') @app.route("/") def hello_world(): output = generator("Hello, world,") return output[0]['generated_text']
// requirements.txt Flask ~= 3.0 gunicorn ~= 23.0 torch ~= 2.5 numpy ~= 2.1 transformers ~= 4.46 huggingface_hub[cli] ~= 0.26
The token will be automatically used for both downloading and accessing your private or gated models.
Scaffoldly even generates a GitHub Action for automated deployments:
// scaffoldly.json { "name": "python-huggingface", "runtime": "python:3.12", "handler": "localhost:8000", "files": ["app.py"], "packages": ["pip:requirements.txt"], "resources": ["arn::elasticfilesystem:::file-system/.cache"], "schedules": { "@immediately": "huggingface-cli download openai-community/gpt2" }, "scripts": { "start": "gunicorn app:app" }, "memorySize": 1024 }
The complete example is available on GitHub:
scaffoldly/scaffoldly-examples#python-huggingface
And you can create your own copy of this example by running:
generator = pipeline('text-generation', model='your-model-here')
You can see it running live (though responses might be slow due to CPU inference):
Live Demo
Scaffoldly is Open Source, and welcome contributions from the community.
What other models do you want to run in AWS Lambda? Let me know in the comments!
The above is the detailed content of Deploy Hugging Face Models to AWS Lambda in teps. For more information, please follow other related articles on the PHP Chinese website!