Saturday, November 29, 2025

Pushing Docker App to AWS ECR for Lambda Deployment Utilizing AWS CLI




Pushing Docker App to AWS ECR for Lambda Deployment Utilizing AWS CLI

On this tutorial, you’ll learn to arrange AWS CLI for safe authentication, construct a Docker picture in your FastAPI AI inference app, and push it to AWS Elastic Container Registry (ECR). This ensures your containerized software is prepared for seamless deployment to AWS Lambda, enabling scalable, serverless AI inference.

This lesson is the final of a 4-part sequence on AWS Lambda:

  1. Introduction to Serverless Mannequin Deployment with AWS Lambda and ONNX
  2. Changing a PyTorch Mannequin to ONNX for FastAPI (Docker) Deployment
  3. FastAPI Docker Deployment: Making ready ONNX AI Fashions for AWS Lambda
  4. Pushing Docker App to AWS ECR for Lambda Deployment Utilizing AWS CLI (this tutorial)

To learn to arrange AWS CLI, containerize your FastAPI AI app, and push it to AWS ECR for Lambda deployment, simply maintain studying.

Searching for the supply code to this publish?

Leap Proper To The Downloads Part

Setting Up AWS CLI and Pushing the FastAPI AI App to AWS ECR for Lambda Deployment

Within the earlier lesson, we containerized our FastAPI AI inference app and examined it regionally utilizing Docker, making certain constant execution throughout environments. Since AWS Lambda helps container-based deployment, this step was important for our serverless setup.

Key Takeaways from Lesson 3:

  • Constructed a FastAPI-based AI inference server that hundreds an ONNX mannequin for picture classification.
  • Ran the app regionally with Uvicorn to validate performance.
  • Created a Dockerfile to package deal the FastAPI app right into a container.
  • Constructed and examined the Docker container, making certain the app runs easily inside it.
  • Verified the FastAPI inference endpoint utilizing Postman, cURL, and browser requests.

By the top of Lesson 3, we had a totally useful FastAPI AI inference server operating inside a Docker container.

Earlier than deploying it to AWS Lambda, we have to retailer the container in AWS Elastic Container Registry (ECR) so Lambda can entry it.


What Is AWS CLI and Why Do We Want It?

The AWS Command Line Interface (AWS CLI) is a robust software that permits builders to work together with AWS companies immediately from the command line moderately than the AWS Administration Console. It gives a unified command-line software for effectively managing AWS assets.

AWS CLI helps you execute instructions for AWS companies like Lambda, ECR, S3, IAM, EC2, API Gateway, and extra with out manually navigating the AWS Console.

AWS CLI is very helpful for:

  • Automation: Automate repetitive AWS duties utilizing scripts.
  • Sooner Execution: Operating CLI instructions is faster than clicking by the AWS UI.
  • Infrastructure as Code (IaC): The AWS CLI integrates with instruments (e.g., Terraform and AWS CloudFormation).
  • Distant Server Administration: Run instructions from any system with out opening the AWS Administration Console.

Why Do We Want AWS CLI in This Deployment?

On this lesson, we are going to use AWS CLI to:

  • Authenticate with AWS by configuring credentials and permissions.
  • Create a personal AWS Elastic Container Registry (ECR) to retailer our Docker picture.
  • Push the FastAPI AI inference app’s Docker picture to AWS ECR so AWS Lambda can entry it.
  • Confirm our saved Docker photos inside ECR with out utilizing the AWS Console.
  • Work together with AWS Lambda later for deployment and testing.

Utilizing AWS CLI will simplify our deployment course of, making it extra environment friendly and repeatable. As a substitute of manually organising AWS companies by the console, we will execute a number of instructions to realize the identical outcomes.

Determine 1 outlines the end-to-end AWS CLI workflow for deploying our FastAPI software to AWS ECR.

Determine 1: AWS CLI workflow for constructing and pushing the FastAPI Docker picture to AWS ECR (supply: picture by the creator)

Why Is AWS CLI Higher Than Utilizing the AWS Console?

Desk 1: Evaluating AWS CLI and AWS Console for managing AWS companies (supply: by the creator)

What Is ECR (Elastic Container Registry)?

AWS Elastic Container Registry (ECR) is a totally managed container picture registry. It permits builders to retailer, handle, and deploy Docker container photos with out sustaining their very own container repositories. ECR integrates seamlessly with AWS companies equivalent to AWS Lambda, ECS (Elastic Container Service), and EKS (Elastic Kubernetes Service), making it straightforward to deploy containerized functions within the AWS cloud.

With AWS ECR, you get:

  • Scalability: Robotically scales together with your container picture storage wants.
  • Safety: Presents fine-grained entry management with AWS IAM (Id and Entry Administration).
  • Reliability: Extremely accessible storage for container photos throughout AWS areas.
  • Deep AWS Integration: Works with AWS companies like Lambda, ECS, and CodeBuild.

As a substitute of utilizing public registries (e.g., Docker Hub), AWS ECR gives a safe, personal strategy to handle container photos inside AWS, lowering exterior dependencies and bettering deployment safety.


Public vs. Non-public Repositories in AWS ECR

AWS ECR helps two forms of repositories: Public and Non-public. Understanding their variations helps in choosing the proper possibility in your deployment.

Desk 2: Comparability of public vs. personal ECR repositories for FastAPI AI mannequin deployment (supply: by the creator)

Since public repositories are accessible to anybody, they’re greatest suited to open-source initiatives or sharing container photos with the developer group. Nevertheless, AWS Lambda doesn’t assist public repositories, that means personal ECR repositories are obligatory for Lambda-based deployments.

For safe, production-ready AI inference, personal repositories guarantee managed entry, safety, and seamless integration with AWS Lambda.

Determine 2: AWS ECR Non-public Repository for FastAPI AI Inference App (supply: by the creator)

Why Are We Utilizing a Non-public AWS ECR Repository?

On this tutorial, we’re utilizing a personal ECR repository as a result of:

  • Necessary for AWS Lambda: Lambda solely helps personal ECR repositories, not public ones.
  • Safety and Entry Management: Solely authenticated AWS customers (through IAM roles) can push/pull photos.
  • Restricted Entry: The AI mannequin shouldn’t be made publicly accessible.
  • Seamless Integration with AWS Lambda: Ensures our FastAPI AI inference mannequin will be deployed securely.
  • Compliance and Privateness: Important for enterprise functions that require strict information safety.

Since AWS Lambda doesn’t assist public ECR repositories, we should use a personal repository to retailer and deploy our containerized AI mannequin. This ensures safe entry, prevents unauthorized use, and integrates easily with AWS Lambda.


What’s Subsequent? Setting Up AWS CLI and Pushing the FastAPI AI App to AWS ECR for Lambda Deployment

Now that we perceive AWS CLI, its function in managing AWS companies, AWS ECR for storing container photos, and why we use a personal repository for safety, it’s time to arrange AWS CLI and push our FastAPI AI app to AWS ECR.

Within the upcoming sections, we are going to:

  • Set up AWS CLI and arrange authentication with AWS credentials.
  • Confirm AWS CLI entry to make sure it could possibly work together with AWS companies.
  • Create a personal ECR repository to securely retailer our FastAPI container picture.
  • Construct and push the FastAPI AI app Docker picture to AWS ECR for Lambda deployment.

As soon as our containerized AI mannequin is efficiently saved in ECR, we shall be able to arrange and deploy it to AWS Lambda within the subsequent part. Let’s start!


Configuring Your Improvement Surroundings

To observe this information, you want Docker put in in your machine. Since we’re deploying a containerized AI inference mannequin, Docker shall be used to construct, check, and push our picture to AWS Elastic Container Registry (ECR).

Obtain and set up Docker from:

After set up, confirm that Docker is operating by executing:

$ docker --version

It’s best to see an output just like:

Docker model 25.0.1, construct 29cf629

Should you’re new to Docker, check with our information on Getting Began with Docker.


Want Assist Configuring Your Improvement Surroundings?

Having hassle configuring your growth setting? Need entry to pre-configured Jupyter Notebooks operating on Google Colab? Be sure you be a part of PyImageSearch College — you may be up and operating with this tutorial in a matter of minutes.

All that mentioned, are you:

  • Brief on time?
  • Studying in your employer’s administratively locked system?
  • Desirous to skip the trouble of combating with the command line, package deal managers, and digital environments?
  • Able to run the code instantly in your Home windows, macOS, or Linux system?

Then be a part of PyImageSearch College immediately!

Achieve entry to Jupyter Notebooks for this tutorial and different PyImageSearch guides pre-configured to run on Google Colab’s ecosystem proper in your net browser! No set up required.

And better of all, these Jupyter Notebooks will run on Home windows, macOS, and Linux!


Mission Construction

Earlier than pushing our Docker picture to AWS ECR, let’s shortly assessment which information from our mission are literally used on this lesson. The listing construction beneath is shared throughout all classes, however only some elements are required at this stage:

$ tree . -L 1
resnet-aws-serverless-classifier/
├── src/
│   ├── server.py            # Included in Docker picture – FastAPI inference server
│   ├── convert.py           # Not used right here – utilized in Lesson 2 for ONNX export
│   ├── onnxt.py             # Not used – ONNX Runtime CLI examples (later classes)
│   ├── onnx_local.py        # Not used – Native ONNX runtime exams (later)
│   └── __init__.py
├── fashions/
│   ├── resnetv2_50.onnx     # Used – ONNX mannequin loaded contained in the Docker picture
│   └── imagenet_classes.txt # Used – Label file for mannequin predictions
├── information/
│   ├── cat.jpg / canine.jpg    # Non-obligatory – Used to check the API regionally earlier than pushing to ECR
│   ├── cat_base64.txt       # Not used – for Lambda JSON payloads (later)
│   ├── occasion.json / payload.json / response.json  # Not used – Lambda exams (later)
├── exams/                   # Not used on this lesson
├── frontend/                # Not used – utilized in Classes 7–8
├── Dockerfile               # Not used – Lambda container construct (subsequent lesson)
├── Dockerfile.native         # Used – builds the native FastAPI Docker picture
├── docker-compose.native.yml # Not used but
├── dev.sh                   # Non-obligatory helper script
├── necessities.txt         # Used – put in inside Docker picture
├── requirements-local.txt   # Used – FastAPI/Docker growth dependencies
├── .dockerignore
├── .gitignore
└── README.md

We solely use:

  • src/server.py
  • fashions/resnetv2_50.onnx
  • fashions/imagenet_classes.txt
  • information/cat.jpg, information/canine.jpg (non-obligatory)
  • Dockerfile.native
  • necessities.txt, requirements-local.txt

The remaining information belong to future classes (Lambda testing, ONNX benchmarking, Subsequent.js UI, and so forth.).


AWS CLI Set up and Setup

Earlier than deploying our AI mannequin to AWS Lambda, we have to configure the AWS Command Line Interface (CLI) to authenticate requests to AWS companies (e.g., ECR and Lambda). The AWS CLI permits us to work together with AWS companies immediately from the terminal, making duties like managing Lambda capabilities, pushing Docker photos to AWS ECR, and organising API Gateway a lot simpler.

On this part, we are going to:

  • Set up AWS CLI in your native machine.
  • Arrange AWS credentials to authenticate with AWS companies.
  • Confirm entry to AWS Lambda, ECR, and different required companies.

Step 1: Set up AWS CLI

The AWS CLI is offered for Home windows, macOS, and Linux. Comply with the official AWS CLI set up information based mostly in your OS.

Alternatively, you’ll be able to set up it immediately utilizing the command line:

macOS:

curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /

Linux:

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/set up
Determine 3: Putting in the AWS CLI v2 on macOS utilizing the official installer package deal (supply: picture by the creator)

After set up, confirm the AWS CLI model:

Determine 4: Verifying the AWS CLI set up with the aws --version command (supply: picture by the creator)

Step 2: Create AWS Entry and Secret Keys

To make use of AWS CLI, we have to present authentication credentials. This requires an AWS Entry Key ID and AWS Secret Entry Key.

The place to Discover Your AWS Entry and Secret Key?

  • Log in to your AWS Administration Console.
  • Click on in your Profile Title (top-right nook).
  • Choose “Safety Credentials” from the dropdown menu.
  • Scroll right down to the “Entry Keys” part.
  • Click on “Create Entry Key” → A brand new key pair (Entry Key ID & Secret Entry Key) shall be generated.
  • Copy and retailer these keys securely (as AWS won’t present them once more).

Vital: AWS doesn’t advocate creating entry keys for the foundation person because of safety dangers. Ideally, it’s best to make an IAM person with particular permissions. Nevertheless, since this information is concentrated on AWS Lambda deployment, we are going to cowl IAM roles & greatest practices individually in a future lesson.

Safety Observe:

  • Don’t share your AWS credentials publicly or commit them to GitHub.
  • Should you ever suspect your credentials are compromised, delete the entry key instantly from the AWS Safety Credentials web page.

Step 3: Configure AWS CLI with Your Credentials

After putting in AWS CLI, we have to configure it with the mandatory credentials to entry AWS companies.

Run the next command to start out the configuration course of:

aws configure

You may be prompted to enter the next particulars:

AWS Entry Key ID [None]: XXXXXXXXXXXXXXXXXXXX
AWS Secret Entry Key [None]: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Default area title [None]: ap-south-1
Default output format [None]: json

AWS Entry Key ID and Secret Entry Key will be obtained from the AWS IAM console underneath “My Safety Credentials”.

Set the Default area title to the AWS area the place you intend to deploy (e.g., ap-south-1) and select the Default output format as json, desk, or textual content (default: json).

Tip: You’ll be able to all the time replace credentials later by operating the command beneath, however guarantee they match those within the AWS Console.

aws configure set aws_access_key_id NEW_ACCESS_KEY
aws configure set aws_secret_access_key NEW_SECRET_KEY

Step 4: Confirm AWS CLI Entry

To verify AWS CLI is accurately arrange, run the next instructions:

Verify AWS Id

aws sts get-caller-identity

Anticipated output:

{
    "UserId": "AIDXXXXXXXXXXX",
    "Account": "123456789012",
    "Arn": "arn:aws:iam::123456789012:person/root"
}

If this command fails, be sure that your AWS credentials are accurately configured.

Verify Present AWS Lambda Features

aws lambda list-functions

Since we haven’t created any Lambda capabilities but, the response must be:

{
    "Features": []
}

Verify AWS ECR Repositories

aws ecr describe-repositories

If no repositories exist but, you’ll see an empty listing:

{
    "repositories": []
}

This confirms that AWS CLI is correctly configured and able to work together with AWS companies.

Now that AWS CLI is ready up, we are going to construct our FastAPI Docker picture regionally and push the container picture to AWS ECR for Lambda deployment.


Constructing and Pushing the Docker Picture to AWS ECR

Now that we’ve got AWS CLI configured, it’s time to construct our FastAPI Docker picture and push it to AWS Elastic Container Registry (ECR). AWS ECR is a totally managed container registry that permits us to retailer, handle, and deploy Docker container photos. Since AWS Lambda helps container-based deployments, we are going to use ECR to retailer our mannequin’s container picture earlier than linking it to Lambda.


Step 1: Create an AWS ECR Repository

Earlier than we will push our Docker picture, we have to create a personal ECR repository to retailer the container.

Run the next command to create a brand new ECR repository:

aws ecr create-repository --repository-name pis-fastapi-ai-inference --region ap-south-1

If the repository is created efficiently, you will note output just like the next:

{
    "repository": {
        "repositoryArn": "arn:aws:ecr:ap-south-1:123456789012:repository/pis-fastapi-ai-inference",
        "registryId": "123456789012",
        "repositoryName": "pis-fastapi-ai-inference",
        "repositoryUri": "123456789012.dkr.ecr.ap-south-1.amazonaws.com/pis-fastapi-ai-inference",
        "createdAt": "2025-03-14T04:47:157000+05:30",
        "imageTagMutability": "MUTABLE",
        "imageScanningConfiguration": { "scanOnPush": false }
    }
}

Now you can view the newly created repository within the AWS console underneath the Non-public Registry part of ECR, as proven in Determine 5.

Determine 5: Viewing the newly created Amazon ECR personal repository within the AWS Administration Console (supply: picture by the creator)

Note the repositoryUri (e.g., 123456789012.dkr.ecr.ap-south-1.amazonaws.com/pis-fastapi-ai-inference). We’ll use this URI to tag and push our Docker picture.

Should you ever have to listing your ECR repositories, run:

aws ecr describe-repositories

Step 2: Authenticate Docker with AWS ECR

To push photos to AWS ECR, we have to authenticate Docker with AWS CLI. Run the next command:

aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-south-1.amazonaws.com

If profitable, it’s best to see:

Login Succeeded

Step 3: Construct the FastAPI Docker Picture

Now, let’s construct the Docker picture for our FastAPI AI inference mannequin.

Navigate to your mission root listing (the place your Dockerfile is situated) and run:

docker construct -t fastapi-ai-inference .

The above command does the next:

  • Makes use of the Dockerfile within the present listing (.) to construct the picture.
  • Tags the picture as fastapi-ai-inference.
  • Installs all required dependencies and units up the FastAPI app.

As soon as full, confirm that the picture was constructed efficiently:

docker photos

Anticipated output:

REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
fastapi-ai-inference     newest    2d3a3f27c1a1   2 minutes in the past   450MB

Step 4: Tag the Docker Picture for AWS ECR

Earlier than pushing the Docker picture to Amazon ECR, it should be tagged with the complete ECR repository URI in order that Docker is aware of which distant registry to add it to.

Run:

docker tag fastapi-ai-inference:newest 123456789012.dkr.ecr.ap-south-1.amazonaws.com/pis-fastapi-ai-inference:newest

You’ll be able to confirm the tag utilizing:

docker photos

It’s best to now see two tagged references to the identical picture — one native tag and one ECR tag, as proven in Determine 6.

Determine 6: Itemizing regionally constructed Docker photos earlier than pushing to Amazon ECR (supply: by the creator)

Step 5: Push the Docker Picture to AWS ECR

Now, let’s push the tagged picture to AWS ECR in order that AWS Lambda can pull it later.

docker push 123456789012.dkr.ecr.ap-south-1.amazonaws.com/pis-fastapi-ai-inference:newest

This course of would possibly take a couple of minutes relying in your web pace, and as soon as full, it’s best to see output like:

The push refers to repository [123456789012.dkr.ecr.ap-south-1.amazonaws.com/pis-fastapi-ai-inference]
newest: Pushed

Step 6: Confirm the Picture in AWS ECR

To verify that the picture is efficiently saved in AWS ECR, run:

aws ecr list-images --repository-name pis-fastapi-ai-inference

Anticipated output:

{
    "imageIds": [
        {
            "imageTag": "latest",
            "imageDigest": "sha256:3a4d6b..."
        }
    ]
}

Alternatively, you’ll be able to verify manually within the AWS Console by opening the AWS Administration Console, navigating to ECR (Elastic Container Registry), and clicking the pis-fastapi-ai-inference repository. It’s best to see your Docker picture listed, as proven in Determine 7.

Through AWS Console:

Determine 7: Viewing Docker photos contained in the Amazon ECR repository console (supply: picture by the creator)

Through AWS CLI:

Determine 8: Itemizing ECR photos utilizing the AWS CLI with aws ecr list-images (supply: by the creator)

In case your photos belong to totally different initiatives or serve varied functions, it’s greatest to create a brand new repository for every.


What’s subsequent? We advocate PyImageSearch College.

Course info:
86+ complete courses • 115+ hours hours of on-demand code walkthrough movies • Final up to date: November 2025
★★★★★ 4.84 (128 Rankings) • 16,000+ College students Enrolled

I strongly consider that for those who had the best instructor you would grasp pc imaginative and prescient and deep studying.

Do you assume studying pc imaginative and prescient and deep studying must be time-consuming, overwhelming, and sophisticated? Or has to contain advanced arithmetic and equations? Or requires a level in pc science?

That’s not the case.

All you want to grasp pc imaginative and prescient and deep studying is for somebody to clarify issues to you in easy, intuitive phrases. And that’s precisely what I do. My mission is to alter training and the way advanced Synthetic Intelligence subjects are taught.

Should you’re severe about studying pc imaginative and prescient, your subsequent cease must be PyImageSearch College, probably the most complete pc imaginative and prescient, deep studying, and OpenCV course on-line immediately. Right here you’ll learn to efficiently and confidently apply pc imaginative and prescient to your work, analysis, and initiatives. Be a part of me in pc imaginative and prescient mastery.

Inside PyImageSearch College you may discover:

  • &verify; 86+ programs on important pc imaginative and prescient, deep studying, and OpenCV subjects
  • &verify; 86 Certificates of Completion
  • &verify; 115+ hours hours of on-demand video
  • &verify; Model new programs launched often, making certain you’ll be able to sustain with state-of-the-art strategies
  • &verify; Pre-configured Jupyter Notebooks in Google Colab
  • &verify; Run all code examples in your net browser — works on Home windows, macOS, and Linux (no dev setting configuration required!)
  • &verify; Entry to centralized code repos for all 540+ tutorials on PyImageSearch
  • &verify; Simple one-click downloads for code, datasets, pre-trained fashions, and so forth.
  • &verify; Entry on cellular, laptop computer, desktop, and so forth.

Click on right here to hitch PyImageSearch College


Abstract

On this lesson, we explored the basics of the AWS CLI and its function in effectively managing AWS companies from the terminal. We additionally lined AWS Elastic Container Registry (ECR), a managed service for storing container photos, and the important thing variations between private and non-private ECR repositories, highlighting why a personal repository is your best option for safe AI mannequin deployment.

With this foundational information, we proceeded to arrange AWS CLI, configure credentials, and confirm entry. We then created a personal AWS ECR repository, constructed a Docker picture for our FastAPI AI inference app, and efficiently pushed it to AWS ECR.

At this stage, our containerized AI software is securely saved in AWS ECR and able to be deployed to AWS Lambda. Within the subsequent lesson, we are going to arrange AWS Lambda, hyperlink it to our ECR picture, and check our deployed FastAPI AI mannequin.


Quotation Info

Singh, V. “Pushing Docker App to AWS ECR for Lambda Deployment Utilizing AWS CLI,” PyImageSearch, P. Chugh, S. Huot, A. Sharma, and P. Thakur, eds., 2025, https://pyimg.co/mzxbn

@incollection{Singh_2025_Pushing-Docker-App-AWS-ECR-Lambda-Deployment-Utilizing-AWS-CLI,
  creator = {Vikram Singh},
  title = {{Pushing Docker App to AWS ECR for Lambda Deployment Utilizing AWS CLI}},
  booktitle = {PyImageSearch},
  editor = {Puneet Chugh and Susan Huot and Aditya Sharma and Piyush Thakur},
  yr = {2025},
  url = {https://pyimg.co/mzxbn},
}

To obtain the supply code to this publish (and be notified when future tutorials are printed right here on PyImageSearch), merely enter your electronic mail tackle within the kind beneath!

Obtain the Supply Code and FREE 17-page Useful resource Information

Enter your electronic mail tackle beneath to get a .zip of the code and a FREE 17-page Useful resource Information on Laptop Imaginative and prescient, OpenCV, and Deep Studying. Inside you may discover my hand-picked tutorials, books, programs, and libraries that can assist you grasp CV and DL!

The publish Pushing Docker App to AWS ECR for Lambda Deployment Utilizing AWS CLI appeared first on PyImageSearch.


Related Articles

Latest Articles