
Deploy ML Models Serverlessly with AWS SageMaker & Lambda
Deploy ML Models Serverlessly with AWS SageMaker & Lambda
Deploy ML Models Serverlessly with AWS SageMaker & Lambda
Deploying a machine learning model to production often comes with a common challenge: how do you make your model accessible via an API without paying 24/7 for idle EC2 instances? Traditional dedicated instances incur continuous costs even when no predictions are being requested.
In this article, I will demonstrate how to deploy a custom-trained TensorFlow image classification model using the Fashion MNIST dataset in a cost-effective, serverless manner using Amazon SageMaker Serverless Inference and AWS Lambda.
- GitHub Repository: https://github.com/Ishara-Vithanage/sagemaker-mlconfig
Step 1: Model Export & S3 Artifact Upload
As the first step, we need to export our trained TensorFlow model into a
.tar.gz format.Why do we package it as .tar.gz ?
We are utilizing Standard S3 Artifact Deployment. SageMaker's native hosting containers require model weights and configuration files to be bundled in a compressed tarball so they can pull and extract the model files directly into the inference container at startup.
Note: This standard artifact approach is highly recommended for small to medium-sized models (such as computer vision or traditional tabular models). For Large Language Models (LLMs), which often feature multi-gigabyte or terabyte weights, this approach is impractical due to initialization timeouts and memory constraints, LLMs typically require specialized streaming loaders, multi-GPU tensor parallel configurations, or dedicated engine runtimes like vLLM.
Uploading to Amazon S3
After exporting your model into the required SavedModel directory structure (containing a versioned folder like
1/ with your saved_model.pb and variables/ directory), compress it into model.tar.gz .- Create a new Amazon S3 Bucket via the AWS Management Console, keeping all settings default.
- Upload your compressed
model.tar.gzfile into the bucket.
- Your bucket layout should look similar to this:

Step 2: Create SageMaker Model
Why do we need to create a SageMaker Model?
While your.tar.gzfile lives in S3, SageMaker needs an explicit abstraction layer, the SageMaker Model resource, to tie your raw artifact together with a pre-built Docker inference container optimized for serving TensorFlow models.
- Go to the Amazon SageMaker AI console, navigate to the left sidebar, and click Inference > Models (or Deployable Models).
- Click Create model.
- Model name: Give your model a descriptive name (e.g.,
tf-fashion-model).
- IAM role: Create a new IAM role or use an existing one that grants SageMaker permissions to access your S3 model artifacts.
- Container definition 1: Select Provide model artifacts and inference image location.
- Inference image location: Provide the ECR image URI for TensorFlow inference. For example:
763104351884.dkr.ecr.us-east-1.amazonaws.com/tensorflow-inference:2.8-cpu-py39(This managed AWS Deep Learning Container comes pre-installed with TensorFlow Serving, eliminating the need to write custom web servers).
- Model artifacts S3 URL: Paste the S3 path pointing directly to your
model.tar.gzfile.
- Keep all other settings default and click Create model.

Step 3: Create Endpoint Configuration
Why do we need an Endpoint Configuration?
An endpoint configuration acts as the blueprint for your deployment infrastructure. It defines how the model should be hosted, specifying whether it runs on dedicated provisioned instances or a serverless scaling tier, along with resource sizing parameters.
- In the SageMaker sidebar, go to Endpoint configurations and click Create endpoint configuration.
- Name: Enter an endpoint configuration name (e.g.,
tf-serverless-config).
- Endpoint type: Select Serverless.
- Production variants: Provide a variant name (or keep the default) and select your previously created model.
- Serverless configuration:
- Memory size: Set to
1GB(sufficient for lightweight computer vision models like Fashion MNIST).
- Max concurrency: Set to
10(controls how many concurrent container instances SageMaker can spin up to handle sudden traffic spikes).
- Keep other settings default and click Create endpoint configuration.
Step 4: Create SageMaker Endpoint
What is a SageMaker Endpoint?
The endpoint is the live, operational HTTPS URL endpoint that client applications or backend services call to send data payloads and receive real-time inferences.
- In the sidebar, click Endpoints and then click Create endpoint.
- Endpoint type: Select Single model.
- Endpoint name: Provide a unique name (e.g.,
tf-serverless-endpoint).
- Endpoint configuration: Select Use an existing endpoint configuration and choose the configuration created in Step 3 (
tf-serverless-config).
- Click Create endpoint.
- Wait a few moments for the status to change from
CreatingtoInService.

Step 5: Create AWS Lambda Function
As the final step, we will build a secure serverless backend bridge using AWS Lambda. The Lambda function uses the AWS SDK (
boto3 ) to accept incoming image payloads, format them for TensorFlow Serving, invoke the SageMaker endpoint, and translate raw probabilities into user-friendly classifications.- Go to the AWS Lambda Console and click Create function.
- Select Author from scratch.
- Function name: Give your function a name (e.g.,
invoke-sagemaker-model).
- Runtime: Select Python 3.x (e.g., Python 3.9 or 3.11).
- Click Create function.
- In the code editor, replace the default code with your handler script using the provided sample code link. Ensure you configure an environment variable named
ENDPOINT_NAMEset to your live endpoint name (tf-serverless-endpoint).
- Click Deploy.
- Go to the Test tab, create a new test event, and paste a sample image payload matching your model's expected input dimensions (e.g., a normalized
28x28x1tensor structured under"instances"):
{ "body": "{\"instances\": [[[[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.5], [0.8], [0.9], [0.9], [0.5], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.9], [1.0], [1.0], [1.0], [1.0], [1.0], [0.9], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.5], [1.0], [0.5], [0.0], [0.0], [0.0], [0.5], [1.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.9], [1.0], [0.0], [0.0], [0.0], [0.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.9], [1.0], [0.0], [0.0], [0.0], [0.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.5], [1.0], [0.0], [0.0], [0.0], [0.0], [0.5], [1.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.9], [1.0], [1.0], [1.0], [1.0], [1.0], [0.9], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.5], [0.8], [0.9], [0.9], [0.5], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]], [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]]]]}"}- Save the test event and click Test. You will receive a clean, structured JSON response indicating the predicted clothing item, its confidence score, and the complete probability distribution across all Fashion MNIST classes:
{ "statusCode": 200, "body": "{\n \"prediction_summary\": {\n \"item\": \"Sandal\",\n \"confidence_percentage\": 92.45\n },\n \"all_probabilities\": {\n \"T-shirt/top\": 0.0, \n \"Trouser\": 0.0, \n \"Pullover\": 0.0, \n \"Dress\": 0.0, \n \"Coat\": 0.0, \n \"Sandal\": 0.92445, \n \"Shirt\": 0.0, \n \"Sneaker\": 0.07552, \n \"Bag\": 0.0, \n \"Ankle boot\": 0.0\n }\n}"}
Exposing Your Model to External Applications
Right now, your model is fully deployable and invokable internally via AWS Lambda. If you want to integrate this machine learning model with an external web or mobile application, you can easily make it publicly accessible by placing Amazon API Gateway in front of your Lambda function. By configuring a REST or HTTP API resource linked to your Lambda handler, API Gateway provides a secure public URL that accepts standard HTTPS POST requests from client apps, routes them through Lambda, and returns real-time predictions seamlessly.
While this demonstration focused on an image classification model using the Fashion MNIST dataset, the exact same serverless architecture can be adapted for virtually any machine learning model, whether you are deploying tabular data estimators, text sentiment analyzers, or predictive regression models.
Conclusion
By combining Amazon S3, SageMaker Serverless Inference, and AWS Lambda, you have successfully built a robust, scalable, and completely serverless machine learning pipeline. Best of all, because SageMaker Serverless Inference automatically scales your container instances down to zero when idle, you only pay for the exact compute time consumed during active invocations, ensuring your deployment remains entirely cost-effective for low-to-moderate traffic workloads.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article