🎯 Mục tiêu Task 6: Thiết lập Amazon Elastic Container Registry (ECR) cho MLOps pipeline:
📥 Input từ các Task trước:
📦 Output:
server/ code → FastAPI API serving predictions trong EKSAmazon ECR (Elastic Container Registry) là dịch vụ Docker container registry được quản lý hoàn toàn bởi AWS, tích hợp sâu với EKS và CI/CD pipeline. ECR cung cấp khả năng lưu trữ, quản lý và triển khai container images một cách an toàn cho MLOps workflow.


Repository Created Successfully:
Sau khi tạo repository, bạn sẽ thấy giao diện như hình dưới với thông tin:
mlops/retail-api<account-id>.dkr.ecr.ap-southeast-1.amazonaws.com/mlops/retail-api
Repository Setup Complete:
API repository đã sẵn sàng cho containerized FastAPI application.
Repository Management Interface:
Trong giao diện quản lý repository, bạn có thể:

mlops/retail-api
Configure API Lifecycle Rules:
Rule 1 - Keep Latest Production Images:
Rule priority: 1
Description: Keep latest 10 production images
Image status: Tagged (wildcard matching)
Image tag filters: v*
Match criteria:
- Count type: imageCountMoreThan
- Count number: 10
Action: expire
Rule 2 - Keep Latest Development Images:
Rule priority: 2
Description: Keep latest 5 development images
Image status: Tagged (wildcard matching)
Image tag filters: dev*, feature*, main*
Match criteria:
- Count type: imageCountMoreThan
- Count number: 5
Action: expire
Rule 3 - Remove Old Untagged Images:
Rule priority: 3
Description: Delete untagged images after 1 day
Image status: Untagged
Match criteria:
- Days since image created: 1
Action: expire
Training Repository Lifecycle Policy:

Check Scan Settings:
View Push Commands:


🎯 ECR Repositories Setup Complete!
Created Repository:
mlops/retail-api: FastAPI prediction service container<account-id>.dkr.ecr.ap-southeast-1.amazonaws.com/mlops/retail-apiTạo server/Dockerfile - Multi-stage build:
# Multi-stage build
FROM python:3.9-slim as builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user -r requirements.txt
# Production stage
FROM python:3.9-slim as production
WORKDIR /app
# Copy dependencies
COPY --from=builder /root/.local /root/.local
# Create non-root user
RUN useradd --create-home --shell /bin/bash apiuser
USER apiuser
# Copy application
COPY . .
# Expose port
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
# Start application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Tạo server/.dockerignore:
# Development files
.git
.gitignore
__pycache__/
*.pyc
.env
*.log
# Editor files
.idea/
.vscode/
# Large files (downloaded at runtime)
*.joblib
*.pkl
model/
# Navigate to server directory
cd retail-price-sensitivity-prediction/server
# Build Docker image
docker build -t mlops/retail-api:latest .
# Test locally
docker run -d --name test -p 8000:8000 mlops/retail-api:latest
curl http://localhost:8000/health
docker stop test && docker rm test
Trong ECR Console:
mlops/retail-apiCác lệnh push commands sẽ như (Windows PowerShell):
# 1. Retrieve an authentication token and authenticate Docker client
(Get-ECRLoginCommand).Password | docker login --username AWS --password-stdin 842676018087.dkr.ecr.ap-southeast-1.amazonaws.com
# 2. Build your Docker image
docker build -t mlops/retail-api .
# 3. Tag your image
docker tag mlops/retail-api:latest 842676018087.dkr.ecr.ap-southeast-1.amazonaws.com/mlops/retail-api:latest
# 4. Push image to ECR
docker push 842676018087.dkr.ecr.ap-southeast-1.amazonaws.com/mlops/retail-api:latest
Hoặc sử dụng AWS CLI:
# 1. Retrieve an authentication token and authenticate Docker client
aws ecr get-login-password --region ap-southeast-1 | docker login --username AWS --password-stdin 842676018087.dkr.ecr.ap-southeast-1.amazonaws.com
# 2. Build your Docker image
docker build -t mlops/retail-api .
# 3. Tag your image
docker tag mlops/retail-api:latest 842676018087.dkr.ecr.ap-southeast-1.amazonaws.com/mlops/retail-api:latest
# 4. Push image to ECR
docker push 842676018087.dkr.ecr.ap-southeast-1.amazonaws.com/mlops/retail-api:latest
Kiểm tra trong AWS Console:
Navigate to ECR Console:
mlops/retail-apiExpected Result:
latest xuất hiện trong danh sách
Kiểm tra bằng CLI:


Kiểm tra bằng console:

Environment Variables:
# Basic configuration
AWS_DEFAULT_REGION=ap-southeast-1
MODEL_BUCKET=mlops-retail-forecast-models
LOG_LEVEL=INFO
PORT=8000
Test Docker Image Locally:
# Test API container locally
docker run -d \
--name retail-api-test \
-p 8000:8000 \
-e AWS_DEFAULT_REGION=ap-southeast-1 \
-e MODEL_BUCKET=mlops-retail-prediction-dev-842676018087 \
842676018087.dkr.ecr.ap-southeast-1.amazonaws.com/mlops/retail-api:latest
# Test health endpoint
curl http://localhost:8000/health
# Test API documentation
open http://localhost:8000/docs
# Clean up
docker stop retail-api-test && docker rm retail-api-test

Hoàn thành! 🎉
ECR registry đã được thiết lập và tích hợp với EKS cluster mlops-retail-cluster. Docker image của retail API đã sẵn sàng để deploy trên Kubernetes trong Task 10.
✅ ECR Repository - mlops/retail-api repository
✅ Container Image - FastAPI prediction service
✅ Cost Optimization - Lifecycle policies, multi-stage builds, ~$0.15/month
🎯 Task 6 Complete - ECR Registry + API Containerization!
✅ ECR Setup: Repository với lifecycle policies & image scanning
✅ Dockerfile: Multi-stage build, non-root user, health checks
✅ Build & Push: Local build → ECR push workflow
✅ Testing: Container verification & API validation
✅ Ready: Sẵn sàng cho EKS deployment trong Task 7
🚀 Next Steps:
� Production Benchmarks Achieved:
Next Step: Task 7: EKS Cluster Setup