Skip to content
New Year BoostTop up
Apixo
Blog
news· 3 min read· via Towards AI

Building a Receipt Extraction API With Gemma 4, SageMaker, and ECS

Learn how to build an automated receipt extraction API using Google Gemma 4, vLLM, Amazon SageMaker, and Amazon ECS on AWS Fargate.

Building a Receipt Extraction API With Gemma 4, SageMaker, and ECS

Extracting structured key-value pairs—such as store names, fluctuating date formats (day/month/year versus month/day/year), and varying labels for prices like "total" or "grand total"—remains a persistent hurdle in automated document processing. Manual data entry introduces human typos and omitted fields. To resolve these inconsistencies, developer Budiono Santoso detailed an end-to-end architecture pairing Google's open-weight Gemma 4 model with the vLLM inference engine, deploying the solution using Amazon SageMaker and Amazon Elastic Container Service (ECS).

Provisioning Gemma 4 on SageMaker with vLLM

Deploying open models on AWS requires navigating default infrastructure restrictions. AWS accounts begin with a quota of zero for all SageMaker g-series GPU instances (including g5, g6, and g7), meaning teams must submit a quota increase request before provisioning. For this setup, Santoso opted for the ml.g6.2xlarge instance in the Oregon (us-west-2) region. At $1.222 per hour for 8 vCPUs and 32 GiB of memory, it costs only roughly ten cents more than the smaller g6.xlarge ($1.1267 for 4 vCPUs and 16 GiB) while doubling compute resources. By comparison, comparable g5 family options cost $1.408 (g5.xlarge) and $1.515 (g5.2xlarge), while a g7.2xlarge reaches $3.15 per hour.

The deployment workflow begins in SageMaker Studio by creating a JupyterLab space and enabling Docker on the Studio domain. After cloning the project repository, a shell script transfers the container image (vLLM version 0.19.1-gpu-py312-cu129-ubuntu22.04-sagemaker-v1.0) to a private Amazon Elastic Container Registry (ECR) repository named vllm-gemma-4. To automate infrastructure provisioning, Terraform scripts (iam.tf, main.tf, and sagemaker.tf) attach the necessary ECR policies to the SageMaker execution role and launch the ml.g6.2xlarge endpoint until its deployment status transitions to "inservice."

Deploying FastAPI on ECS Express and Standard Modes

With the inference endpoint operational, the frontend extraction API uses FastAPI packaged into a separate container. When building images inside SageMaker Studio, Santoso highlights a common failure point that requires installing sagemaker-studio-image-build, a CLI extension that delegates Docker builds to AWS CodeBuild. Preparing the environment necessitates granting the SageMaker execution role permissions across AWS CodeBuild, CloudWatch Logs, ECR, S3, IAM, ECS, VPC, and Auto Scaling, along with configuring trust relationships for both sagemaker.amazonaws.com and codebuild.amazonaws.com.

Once pushed to ECR under receipt-extraction-gemma-4, the API container can be orchestrated on serverless AWS Fargate via Amazon ECS using two distinct approaches:

  • ECS Express Mode: Designed for fast deployment with minimal initial configuration, requiring only the container image URI and execution roles while delegating underlying setup to AWS. Custom VPC integration ensures control over network isolation, connecting through an Application Load Balancer to monitor target group health.
  • ECS Standard Mode: Intended for full infrastructure control, requiring explicit creation of task definitions, VPC networking, auto-scaling policies, and load balancer configurations.

Both implementations include an IAM task role explicitly granted permissions to invoke the SageMaker endpoint. Once running, the service exposes /health (returning 200 OK) and /predict endpoints, accessible via Swagger UI at /docs to process sample receipt photos into structured data.

What it means for developers

This architecture demonstrates the operational overhead required to run self-hosted, open-weight models in enterprise cloud environments. While open-weight options like Gemma 4 combined with optimized serving runtimes like vLLM eliminate third-party per-token fees, they shift the engineering burden entirely onto infrastructure management. Engineers must balance GPU quotas, Docker cross-compilation within SageMaker Studio, IAM role chaining, Application Load Balancers, and continuous hourly compute costs—even during traffic lulls.

For production environments with predictable, high-volume processing demands, maintaining dedicated instances like the g6.2xlarge on SageMaker alongside Fargate-managed APIs delivers predictable performance and strict data governance within a custom VPC. However, for development teams testing receipt extraction or prototyping document intelligence without configuring Terraform modules, GPU quotas, and ECS clusters, managed model routing offers a far faster path forward. Developers can try top AI models cheaply through one API at https://apixoai.online, allowing quick benchmarking between open-weight and proprietary vision-language models before committing to dedicated cloud infrastructure.


Source: Build Receipt Extraction API using Amazon SageMaker, Amazon ECS, vLLM and Google Gemma 4 — Towards AI. Written by the Apixo team from that report.

#ai-news#aws#sagemaker#gemma#vllm#ecs
Try it with your own tools

One key for Claude, GPT, GLM, DeepSeek and more. Pay per token with crypto.

Get your API key

Keep reading