AWS Builder Center

Running Lightweight LLMs and Embeddings on AWS Lambda: Serverless AI Without the Dedicated GPU Price Tag

Serverless ML bridges the gap between static rule-based scripts and expensive dedicated GPU clusters. By pairing quantized models with optimized container images on AWS Lambda, developers can build scalable, responsive intelligent applications while maintaining a true pay-per-request pricing structure

Dedicated GPU instances on Amazon EC2 or SageMaker endpoints provide high throughput, but they carry a continuous hourly cost that makes little sense for intermittent workloads, internal tools, or student prototypes.
With AWS Lambda supporting up to 10 GB of ephemeral memory and container images up to 10 GB, running optimized, small-footprint AI models directly on serverless compute has become a practical and cost-effective reality.
Here are the key technical strategies for deploying lightweight NLP models and embedding pipelines on AWS Lambda with minimal latency.
Quantization and Model Pruning
Full-precision models (FP32 or FP16) consume significant memory and compute cycles during inference. Using quantized representations—such as 4-bit (AWQ/GPTQ) or 8-bit quantization via libraries like bitsandbytes or llama.cpp bindings—drastically reduces memory consumption. This allows sub-3B parameter models or dense embedding models to execute comfortably within standard CPU RAM limits.
Package Dependencies with Container Images
Standard Lambda zip packages cap dependencies at 250 MB unzipped, which is insufficient for PyTorch, NumPy, and model weights combined. By building a custom Docker container image based on the AWS Lambda Python base image:
Model weights can be pre-baked into the image layers to eliminate download latency during initialization.
Unnecessary sub-libraries (like CUDA drivers, audio/vision tooling, or developer packages) can be stripped using multi-stage Docker builds.
Leverage Provisioned Concurrency for Cold Starts
Loading a 1 GB model into memory and warming up the runtime adds noticeable latency on the first invocation. If consistent, sub-second latency is required:
Configure Provisioned Concurrency on the Lambda function to keep a small baseline of execution environments initialized.
For purely asynchronous background jobs (such as automated document indexing or batch tagging), standard on-demand concurrency is sufficient and eliminates idle billing entirely.
Cache Embeddings with DynamoDB or ElastiCache
Avoid redundant compute by implementing an exact-match embedding cache. Hashing the input query text and performing a key lookup against Amazon DynamoDB prior to calling the model function saves compute time and prevents unnecessary Lambda execution cycles
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article