58.13 Project 29: Push to ECR and run a 3-tier app on ECS Fargate with RDS and S3
Friends, today we push images to Amazon ECR and run API plus worker on ECS Fargate with RDS and S3. No EC2 to patch. Type every command yourself.
मित्रांनो, आज आपण images Amazon ECR वर push करतो आणि API व worker ECS Fargate वर RDS व S3 सोबत चालवतो. Patch करायला EC2 नाही. प्रत्येक command स्वतः टाइप करा.
दोस्तों, आज हम images Amazon ECR पर push करते हैं और API व worker ECS Fargate पर RDS व S3 के साथ चलाते हैं. Patch करने को EC2 नहीं. हर command खुद टाइप कीजिए.
What you will learn in this lesson
- Why ECR holds private images
- How Fargate runs tasks without managing servers
- How task roles reach RDS and S3
- How a service keeps desired count
- How to roll a new image tag
1. Start from a real problem
Suppose Razorpay-like teams want containers without SSH boxes.
ECS Fargate runs the task; you manage images and IAM, not the OS.
Indian fintechs and Amazon internal teams use Fargate for many APIs.
Write every command in a notebook as you go. The notebook becomes your interview story: problem, commands, output, mistake, fix, cleanup.
Real companies grade juniors on whether they can repeat the lab without looking at a chat history.
If a command fails, copy the error, fix it, and keep both lines in the notebook.
2. Project skeleton
Step 1: Create the project folder
Why: One folder per project.
What: Everything for this lab lives under ecs-fargate/ so cleanup is one command. Write every command in a notebook as you go; the notebook becomes your interview story.
Example 1: Make folder
Type this:
mkdir -p ecs-fargate/{api,worker,web,db} && cd ecs-fargate && pwd
You will see:
/home/you/ecs-fargate
What it means: Root ready.
Example 2: Gitignore secrets
Type this:
printf '%s\n' '.env' '*.pem' 'node_modules/' > .gitignore && cat .gitignore
You will see:
.env
*.pem
node_modules/
What it means: Secrets stay out of Git.
Example 3: Env template
Type this:
printf '%s\n' 'APP_NAME=ecs-fargate' 'DB_PASSWORD=change-me' > .env.example && cp .env.example .env && sed -i 's/change-me/lab-pass/' .env && grep DB_PASSWORD .env
You will see:
DB_PASSWORD=lab-pass
What it means: Local password set.
Step 2: Write the base Compose file
Why: Services on one network.
What: Start with the backing services, then add the app containers. Pin image tags so yesterday's lab still starts tomorrow.
Example 4: Write compose.yaml
Type this:
printf '%s\n' 'name: ecs-fargate' 'services:' ' db:' ' image: postgres:16-alpine' ' environment:' ' POSTGRES_PASSWORD: ${DB_PASSWORD}' ' POSTGRES_DB: app' ' volumes: [pg-data:/var/lib/postgresql/data]' ' healthcheck:' ' test: ["CMD-SHELL", "pg_isready -U postgres"]' ' interval: 5s' ' retries: 20' 'volumes: {pg-data:}' > compose.yaml && wc -l compose.yaml
You will see:
16 compose.yaml
What it means: Postgres with healthcheck.
Example 5: Start db
Type this:
docker compose up -d db && docker compose ps
You will see:
db ... (healthy)
What it means: Database ready.
Example 6: Pin reminder
Type this:
grep 'image:' compose.yaml
You will see:
image: postgres:16-alpine
What it means: Pinned tag, not latest.
3. Application containers
Step 3: API Dockerfile
Why: Non-root and healthcheck.
What: A small API image that reads env and exposes /healthz. Real companies reject containers that run as root without a reason.
Example 7: Write api/Dockerfile
Type this:
printf '%s\n' 'FROM node:20-alpine' 'WORKDIR /app' 'COPY package.json .' 'RUN npm install --omit=dev' 'COPY server.js .' 'USER node' 'EXPOSE 3000' 'HEALTHCHECK CMD wget -qO- http://127.0.0.1:3000/healthz || exit 1' 'CMD ["node","server.js"]' > api/Dockerfile && grep USER api/Dockerfile
You will see:
USER node
What it means: Non-root.
Example 8: Write api/package.json
Type this:
printf '%s\n' '{' ' "name": "lab-api",' ' "main": "server.js",' ' "dependencies": { "express": "4.19.2" }' '}' > api/package.json && wc -l api/package.json
You will see:
7 api/package.json
What it means: Express for the skeleton.
Example 9: Write api/server.js
Type this:
printf '%s\n' 'const express=require("express"); const app=express();' 'app.get("/healthz",(_,res)=>res.json({ok:true,project:"ecs-fargate"}));' 'app.get("/",(_,res)=>res.json({message:"Push to ECR and run a 3-tier app on ECS Fargate with RDS and S3"}));' 'app.listen(3000,()=>console.log("api on 3000"));' > api/server.js && wc -l api/server.js
You will see:
5 api/server.js
What it means: Health and root route.
Step 4: Add API to Compose
Why: Build and publish a port.
What: depends_on waits for the database healthcheck so the API does not crash-loop on a cold start.
Example 10: Append api service
Type this:
printf '%s\n' ' api:' ' build: ./api' ' ports: ["3000:3000"]' ' environment:' ' DATABASE_URL: postgres://postgres:${DB_PASSWORD}@db:5432/app' ' depends_on:' ' db: {condition: service_healthy}' >> compose.yaml && docker compose up -d --build api
You will see:
... Started
What it means: API up.
Example 11: Hit health
Type this:
curl -s http://localhost:3000/healthz
You will see:
{"ok":true,"project":"ecs-fargate"}
What it means: Healthy.
Example 12: Hit root
Type this:
curl -s http://localhost:3000/
You will see:
{"message":
What it means: Root route works.
4. Worker, gateway and project-specific path
Step 5: Worker container pattern
Why: Slow work off the request.
What: A second Dockerfile runs a loop or queue consumer so the HTTP path stays fast under load.
Example 13: Write worker/Dockerfile
Type this:
printf '%s\n' 'FROM node:20-alpine' 'WORKDIR /app' 'COPY worker.js .' 'USER node' 'CMD ["node","worker.js"]' > worker/Dockerfile && grep USER worker/Dockerfile
You will see:
USER node
What it means: Non-root worker.
Example 14: Write worker/worker.js
Type this:
printf '%s\n' 'console.log("worker for ecs-fargate starting");' 'setInterval(()=>console.log("tick", new Date().toISOString()), 10000);' > worker/worker.js && wc -l worker/worker.js
You will see:
2 worker/worker.js
What it means: Heartbeat worker for the lab.
Example 15: Append worker
Type this:
printf '%s\n' ' worker:' ' build: ./worker' ' environment:' ' DATABASE_URL: postgres://postgres:${DB_PASSWORD}@db:5432/app' ' depends_on: [db]' >> compose.yaml && docker compose up -d --build worker
You will see:
... Started
What it means: Worker running.
Step 6: Web or gateway front door
Why: One published port.
What: nginx fronts the API so browsers and demos hit one host port only.
Example 16: Write web/nginx.conf
Type this:
printf '%s\n' 'server {' ' listen 80;' ' location /api/ {' ' proxy_pass http://api:3000/;' ' }' ' location / {' ' return 200 "ok from web\n";' ' add_header Content-Type text/plain;' ' }' '}' > web/nginx.conf && grep proxy_pass web/nginx.conf
You will see:
proxy_pass http://api:3000/;
What it means: Gateway path.
Example 17: Write web/Dockerfile
Type this:
printf '%s\n' 'FROM nginx:1.27-alpine' 'COPY nginx.conf /etc/nginx/conf.d/default.conf' 'EXPOSE 80' > web/Dockerfile && wc -l web/Dockerfile
You will see:
4 web/Dockerfile
What it means: Static nginx image.
Example 18: Append web
Type this:
printf '%s\n' ' web:' ' build: ./web' ' ports: ["8080:80"]' ' depends_on: [api]' >> compose.yaml && docker compose up -d --build web
You will see:
... Started
What it means: Web on 8080.
Step 7: ECR push and Fargate service
Why: Commit tags.
What: Tag with the Git SHA, push to ECR, set the ECS service to that digest or tag.
Example 19: Login ECR
Type this:
echo 'aws ecr get-login-password | docker login --username AWS --password-stdin ACCOUNT.dkr.ecr.REGION.amazonaws.com'
You will see:
aws ecr get-login-password | docker login --username AWS --password-stdin ACCOUNT.dkr.ecr.REGION.amazonaws.com
What it means: Auth to ECR.
Example 20: Push tag
Type this:
echo 'docker push ACCOUNT.dkr.ecr.REGION.amazonaws.com/api:9f3c2ab'
You will see:
docker push ACCOUNT.dkr.ecr.REGION.amazonaws.com/api:9f3c2ab
What it means: Immutable-ish tag.
Example 21: Task role
Type this:
echo 'Task role needs rds and s3 permissions; no keys in the task definition'
You will see:
Task role needs rds and s3 permissions; no keys in the task definition
What it means: IAM not env keys.
Step 8: Prove the path end to end
Why: Curl the front door.
What: From the laptop, hit web and the proxied API before you call the lab done.
Example 22: Web root
Type this:
curl -s http://localhost:8080/
You will see:
ok from web
What it means: Front door works.
Example 23: Proxied API health
Type this:
curl -s http://localhost:8080/api/healthz
You will see:
{"ok":true,"project":"ecs-fargate"}
What it means: Proxy works.
Example 24: Compose ps
Type this:
docker compose ps
You will see:
api Up worker Up web Up db (healthy)
What it means: All green.
5. Hardening, cloud notes and cleanup
Step 9: Non-root and secret habits
Why: Safer defaults.
What: Run as non-root, keep secrets in env, and never bake passwords into images.
Example 25: API user
Type this:
grep USER api/Dockerfile
You will see:
USER node
What it means: Already non-root.
Example 26: No secrets in image
Type this:
grep -r Password api/ || echo 'no passwords in api source'
You will see:
no passwords in api source
What it means: Env only.
Example 27: Read-only tip
Type this:
echo 'In production set read_only: true and mount tmpfs for /tmp'
You will see:
In production set read_only: true and mount tmpfs for /tmp
What it means: Hardening beyond the classroom.
Step 10: Cloud or VM notes for this project
Why: Where it runs next.
What: This project (Push to ECR and run a 3-tier app on ECS Fargate with RDS and S3) follows the same env contract on a VM, ECS or Swarm. Create a 5 USD billing alarm before any real cloud resource.
Example 28: Same Compose on a VM
Type this:
echo 'Install Docker on Ubuntu, copy the folder, docker compose up -d'
You will see:
Install Docker on Ubuntu, copy the folder, docker compose up -d
What it means: Context deploy is covered in Project 28.
Example 29: IAM not keys
Type this:
echo 'On AWS use a role; never paste long-lived keys into Compose'
You will see:
On AWS use a role; never paste long-lived keys into Compose
What it means: Standing rule for every project.
Example 30: Billing alarm
Type this:
echo '5 USD CloudWatch billing alarm before real cloud resources'
You will see:
5 USD CloudWatch billing alarm before real cloud resources
What it means: Always.
Step 11: Common mistakes
Why: Host networking and latest tags.
What: Using host network hides Compose DNS. Using :latest makes rollbacks hard. Publishing the database port on a laptop is a risk.
Example 31: Use service DNS
Type this:
echo 'api reaches db via hostname db on the Compose network'
You will see:
api reaches db via hostname db on the Compose network
What it means: Not localhost.
Example 32: Pin tags
Type this:
grep 'image:' compose.yaml | head -1
You will see:
image: postgres:16-alpine
What it means: Pinned major tag.
Example 33: Do not publish db
Type this:
grep -A6 'db:' compose.yaml | grep ports || echo 'no host ports on db'
You will see:
no host ports on db
What it means: Database stays internal.
Step 12: Clean up
Why: down -v every time.
What: Remove containers and volumes so the next project starts clean. Delete cloud resources the same day.
Example 34: Stop
Type this:
docker compose down -v
You will see:
... Removed
What it means: Project gone.
Example 35: Disk
Type this:
docker system df | head -5
You will see:
TYPE SIZE
What it means: See reclaimable space.
Example 36: Next
Type this:
echo 'Next: Docker Swarm stack'
You will see:
Next: Docker Swarm stack
What it means: Keep momentum.
Step 13: Health before demos
Why: Always curl first.
What: Do not demo until /healthz returns 200. Interviewers notice when you verify before you claim success.
Example 37: API health code
Type this:
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/healthz
You will see:
200
What it means: Ready.
Example 38: Web health
Type this:
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:8080/
You will see:
200
What it means: Ready.
Example 39: Logs tail
Type this:
docker compose logs api --tail 5
You will see:
api on 3000
What it means: Startup confirmed.
Step 14: Interview story for this project
Why: Say it in one minute.
What: Explain why this project matters, what you built, the mistake you fixed, and how you cleaned up. That order wins interviews.
Example 40: Why
Type this:
echo 'Suppose Razorpay-like teams want containers without SSH boxes.'
You will see:
Suppose Razorpay-like teams want containers without SSH boxes.
What it means: Business reason.
Example 41: What
Type this:
echo 'Push to ECR and run a 3-tier app on ECS Fargate with RDS and S3'
You will see:
Push to ECR and run a 3-tier app on ECS Fargate with RDS and S3
What it means: What you typed.
Example 42: Clean
Type this:
echo 'docker compose down -v and delete cloud resources'
You will see:
docker compose down -v and delete cloud resources
What it means: Bill stays near zero.
Ravindra Bagale's Tip
Tag images with the Git commit, push to ECR, run on Fargate with a task role. Never put AWS keys in task definitions.
Images Git commit ने tag करा, ECR वर push करा, task role सह Fargate वर चालवा. Task definitions मध्ये AWS keys कधीही ठेवू नका.
Images Git commit से tag करें, ECR पर push करें, task role के साथ Fargate पर चलाएं. Task definitions में AWS keys कभी न रखें.
Check yourself
Q1. What is the first folder step?
Create ecs-fargate/ and a .gitignore that ignores .env.
Q2. Where do secrets live?
In .env or a secret store, never in Git.
Q3. How do containers find each other?
Compose service DNS names on the project network.
Q4. What user should app containers use?
A non-root user such as node or app.
Q5. What is the last lab step?
docker compose down -v and delete any cloud resources.
Q6. Why curl /healthz before a demo?
To prove the API is ready before you show features.
Friends, you built the skeleton and the main path for this project. Did you understand? Let us try it once more. If not, re-run the health checks and look at Picture 1. Next: Docker Swarm stack.
मित्रांनो, तुम्ही या project चा skeleton आणि मुख्य path बनवला. नसेल तर health checks पुन्हा चालवा आणि Picture 1 बघा. पुढे: Docker Swarm stack.
दोस्तों, आपने इस project का skeleton और मुख्य path बनाया. नहीं तो health checks फिर चलाएं और Picture 1 देखिए. आगे: Docker Swarm stack.
Lab: try at home
- Create ecs-fargate with api, worker, web and db folders.
- Write compose.yaml with a healthy database.
- Build and run the API with /healthz.
- Add worker and web and prove the proxy path.
- Complete the project-specific path for this lesson.
- Clean up with docker compose down -v.
Recap
- One folder per project.
- Secrets in .env only.
- Non-root containers.
- Healthchecks before depends_on.
- Front door on one port.
- Always clean up.
Revision card
Interview card
Comparison card