AWS Builder Center
Your Deployment Is Done. But What Did It Leave Behind?

Your Deployment Is Done. But What Did It Leave Behind?

Meet ECS Guardian, a serverless AWS tool that scans Amazon ECR and ECS to identify outdated container images, unused task definition revisions, and potentially idle Fargate services. It generates an SNS email report with estimated cost-saving opportunities, automatically reports findings every week, and keeps deletion under human control through an explicit cleanup mode.reporting and human-controlled cleanup.

Series: Safe Container Lifecycle Management on AWS with ECS Guardian (3 articles)

  1. 2
    Your Deployment Is Done. But What Did It Leave Behind? This article

📘 Introduction

Container platforms accumulate waste quietly. Every deployment pushes a new image to Amazon ECR and registers a new ECS task definition revision. Over months, old images pile up and incur storage charges, hundreds of stale revisions clutter the console, and services that nobody uses keep running on Fargate around the clock.
ECS Guardian is a serverless housekeeping tool that finds this waste and reports on it. A single Lambda function scans ECR and ECS, works out what is still in use, and emails a cost-savings report through SNS. It runs in two modes:
  • Report mode (dry run): lists everything it would remove and deletes nothing.
  • Cleanup mode: deletes unused images and deregisters stale task definition revisions, but only when a human triggers it.
Idle ECS services are only ever reported, never touched automatically. The Guardian is conservative by design: it never deletes an image that a running task or a retained task definition still references.
This is Part 1 of a three-part series and covers the idea and the design. Part 2 walks through building it step by step in the AWS console, and Part 3 closes with the results, the security model and the lessons learned.

🧠 The Problem: Waste That Builds Up Quietly

Three kinds of waste collect on a container platform, and each one bills you differently.
  • Old ECR images: every deployment pushes another image, and ECR storage is billed per image, so a repository that is never pruned only grows.
  • Stale task definition revisions: every deployment also registers a new revision. They cost little on their own, but hundreds of them clutter the console and make it hard to see what is live.
  • Idle ECS services: a service nobody uses keeps running on Fargate around the clock and keeps billing.
No single item looks expensive, which is exactly why this waste goes unnoticed until someone goes looking for it.

🎯 Aim of the Project

The project aims to:
  • Automatically detect unused ECR images that are older than a threshold and are not among the newest N images in their repository.
  • Detect stale ECS task definition revisions beyond the newest N per family that are not used by any service or running task.
  • Identify idle ECS services, meaning those whose maximum CPU stays below a threshold over a set number of days, and estimate their monthly cost.
  • Protect anything in use by checking running tasks, service task definitions, and kept revisions before flagging an image.
  • Estimate potential monthly savings and deliver the findings as an email report through SNS.
  • Keep a human in the loop: nothing is deleted until cleanup mode is invoked, and a  guardian:ignore=true  tag exempts any repository or service.
  • Run the report automatically every week through EventBridge Scheduler.

💡 How the Guardian Decides

A resource is flagged only when it fails every protection check. The rules differ by resource type:
Resource
Flagged when
Never flagged when
ECR image
Older than  IMAGE_AGE_DAYS  and outside the newest  KEEP_LATEST_IMAGES  in its repository
Referenced by a running task, a task definition used by a service, or a kept task definition revision
Task definition revision
Outside the newest  KEEP_TASKDEF_REVISIONS  in its family
Used by a service or a running task
ECS service
Maximum CPU below  IDLE_CPU_PERCENT  for  IDLE_DAYS  days (reported only, with an estimated monthly cost)
Never touched, only listed in the report
Any repository or service tagged  guardian:ignore=true  is skipped entirely.
The thresholds are Lambda environment variables ( IMAGE_AGE_DAYS ,  KEEP_LATEST_IMAGES ,  KEEP_TASKDEF_REVISIONS ,  IDLE_CPU_PERCENT ,  IDLE_DAYS ), so the same code runs with aggressive values in a demo and conservative values in production.
Cleanup mode recalculates everything at the moment it runs. It deletes what is unused right now, not what an older report listed.

🏛️ Architecture Diagram and Components



The design has a small control path and a small demo workload to scan.
  • Amazon EventBridge Scheduler ( ecs-guardian-weekly ): triggers the Guardian every Monday at 9:00 AM IST with the payload  {"mode": "report"} .
  • AWS Lambda ( container-guardian ): a Python function that scans ECR, ECS and CloudWatch, works out which resources are unused, optionally deletes them, and publishes the report.
  • Amazon CloudWatch Metrics: supply  AWS/ECS   CPUUtilization  data. A service whose maximum CPU stays below the threshold for  IDLE_DAYS  is reported as idle, with an estimated monthly cost.
  • Amazon SNS ( ecs-guardian-reports ) with an email subscription: delivers the findings (unused images, stale task definitions, idle services and estimated savings).
  • Amazon CloudWatch Logs ( /aws/lambda/container-guardian ): stores the Lambda execution logs for troubleshooting.
  • IAM role ( ContainerGuardianRole ): gives the Lambda least-privilege access, covered in Part 3.
  • Resource tag ( guardian:ignore=true ): exempts any ECR repository or ECS service from scanning and cleanup.
The demo workload that the Guardian scans:
  • Amazon ECR ( guardian-demo-app ): a private repository holding four demo images,  v1  to  v4 .
  • ECS task definitions ( guardian-demo-task ): six revisions, where revisions 1 to 4 use  v1  to  v4  and revisions 5 and 6 use  v2 .
  • ECS cluster ( guardian-demo-cluster ) and service ( guardian-demo-svc ): a Fargate service running one task on the  v2  image, which proves the Guardian never deletes an image that is in use.
  • IAM role ( ecsTaskExecutionRole ): lets the demo Fargate tasks pull images from ECR and write logs.

🛡️ Safety Guardrails

The Guardian is conservative by design. Several independent layers must all agree before anything is removed.
1. Report before delete. The default mode is report, a dry run that deletes nothing. Cleanup runs only when a person invokes it with  {"mode": "cleanup"} , and it recalculates everything at that moment rather than replaying an old report.
2. Keep-latest-N rules.  KEEP_LATEST_IMAGES  always protects the newest N images in each repository.  KEEP_TASKDEF_REVISIONS  always protects the newest N revisions in each task definition family.
3. Age threshold. An image must be at least  IMAGE_AGE_DAYS  old before it can be flagged, so recent pushes are never touched.
4. Usage protection. An image is never flagged if it is referenced by a running task, a task definition used by a service, or a task definition revision that is being kept. In the demo, this is why  v2  survives (it is running and used by revisions 5 and 6) and  v4  survives (it is the newest).
5. The  guardian:ignore=true  tag. Add this tag to any ECR repository or ECS service to exempt it from scanning and cleanup.
6. Idle services are never touched. Services with low CPU are only listed in the email, along with an estimated monthly cost. Stopping or scaling them is always a human decision.

➡️ Up Next

With the design principles and safety guardrails in place, it’s time to bring ECS Guardian to life. In Part 2, I’ll walk through the hands-on implementation in the AWS console, from configuring the SNS notifications and Lambda function to setting up the weekly EventBridge schedule. We’ll also test how the Guardian identifies potential cleanup candidates and clean up the demo resources afterward.
Stay tuned for the next part, where the architecture moves from design to implementation!

🤝 Let’s Connect

Open to ideas, feedback, and meaningful collaboration:

🌐 Explore More

Explore my projects, technical articles, and cloud learning journey through these platforms:

Thank you for reading! I hope this project inspires you to take a closer look at the resources running in your own AWS environment. Until the next part, keep building, keep learning, and keep exploring.

Series: Safe Container Lifecycle Management on AWS with ECS Guardian (3 articles)

  1. 2
    Your Deployment Is Done. But What Did It Leave Behind? This article
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article