Effortless Image Deployment: Pushing from Local to AWS ECR [Part-1]

Priyanka Pandey
3 min readJan 31, 2024

--

In my previous articles, I’ve delved into ECS deployment and its seamless integration with various services. However, before leveraging ECS, the initial step is building a Docker image in ECR. This can be accomplished through two primary methods:

  1. Local Setup
  2. Code Build

In this piece, I’ll guide you through the process of pushing data from your local environment using AWS CLI. Additionally, I’ll demonstrate how to automate this process with shell scripts for both Mac and Windows systems.

What is Amazon Elastic Container Registry (ECR)

Amazon Elastic Container Registry (Amazon ECR) provides a fully managed container registry that ensures high-performance hosting, enabling dependable deployment of application images and artifacts across various environments. It simplifies deployment processes and oversees image lifecycle policies for efficient management.

Cost

Let’s explore the anticipated expenses associated with AWS ECR. Given that I am located in India, the nearest AWS region is ap-south-1. To store 100 GB of data per month, let’s ascertain the incurred costs.

100 GB per month x 0.10 USD = 10.00 USD

Elastic Container Registry pricing (monthly): 10.00 USD

Source-https://calculator.aws/#/createCalculator/ECR

Pushing Image from AWS CLI

Initially, setting up the AWS CLI (Command Line Interface) requires a series of steps:

  1. Install AWS CLI: You can get the detailed info of installing AWS CLI in all operating system here.
  2. Configure AWS CLI:Run the following command and follow the prompts:
aws configure

You’ll be prompted to enter:

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default region name

Add the details and configuration, you can verify it by using below command.

aws sts get-caller-identity

Now to push an image to Amazon Elastic Container Registry (ECR), you can use the AWS CLI. All the instruction are mentioned inside “View push commands”

Shell script
While the method outlined above accomplishes the task, it can be time-consuming and requires manually entering commands for pushing each image. To streamline this process, we’ll create a shell script that automates image pushing with minimal modifications required.

Linux/Mac


# The name of our project
project_name=word-count

# The location of your docker file
cd /Users/priyanka/Documents/mlops/docker_demo

chmod +x /Users/priyanka/Documents/mlops/docker_demo/serve

account=$(aws sts get-caller-identity --query Account --output text)

# Get the region defined in the current configuration (default to ap-south-1 if none defined)
region=$(aws configure get region)
region=${region:-ap-south-1}

fullname="${account}.dkr.ecr.${region}.amazonaws.com/${project_name}:v1"

# If the repository doesnt exist in ECR, create it.
aws ecr describe-repositories --repository-names "${project_name}" > /dev/null 2>&1

if [ $? -ne 0 ]
then
aws ecr create-repository --repository-name "${project_name}" > /dev/null
fi

# Get the login creds from ECR and execute it directly
aws ecr get-login-password --region ${region}|docker login --username AWS --password-stdin ${fullname}

# Build the docker image locally and push it to ECR

docker build -t ${project_name} .
docker tag ${project_name} ${fullname}

docker push ${fullname}

Windows


REM Name of project-> ECR
set project_name=word-count

#docker file location change

cd C:\Users\priyanka\mlops\docker_demo

REM make serve executable skip if not required
icacls C:\Users\priyanka\mlops\docker_demo\serve /grant Users:F

for /f "tokens=1" %a in ('aws sts get-caller-identity --query Account --output text') do set account=%a

REM Region, defaults to ap-south-1
for /f "tokens=1" %a in ('aws configure get region') do set region=%a
if "%region%"=="" set region=ap-south-1

set fullname=%account%.dkr.ecr.%region%.amazonaws.com/%project_name%:v1

REM If the repository doesn't exist in ECR, create it.
aws ecr describe-repositories --repository-names "%project_name%" > nul 2>&1

if NOT %ERRORLEVEL%==0 (
aws ecr create-repository --repository-name %project_name% --image-scanning-configuration scanOnPush=true > nul
)

REM Get the login command from ECR and execute it directly
aws ecr get-login-password --region %region% | docker login --username AWS --password-stdin %fullname%

REM Build the docker image locally with the image name and then push it to ECR
REM with the full name.
docker build -t %project_name% .
docker tag %project_name% %fullname%
docker push %fullname%

With these commands you will be able to push the image in ECR.

--

--

Priyanka Pandey

Embark on a journey as we delve into scalable solutions and cloud-driven excellence with my insights on MLOps, Data Engineering, and AWS Cloud.