📘 Introduction
Part 1 covered the idea and the guardrails, and Part 2 built the Guardian, ran it, and tore it down. This final part covers what is left: the IAM model that keeps the Guardian's reach small, how to tune its thresholds for your own environment, where it falls short, and where it could go next.
There is no new setup here. This post is about understanding, trusting and extending what you already built.
🔐 Security and Least-Privilege Considerations
ECS Guardian can delete resources, so its permissions matter as much as its logic. The guardrails in Part 1 control what the code decides to do. The IAM policy controls what the code is able to do, even if something goes wrong.
- Read access is broad, write access is narrow. Most actions are
Describe* and List* calls. The only destructive actions are ecr:BatchDeleteImage and ecs:DeregisterTaskDefinition , and both run only in cleanup mode.
- Notifications are limited to one topic.
sns:Publish is scoped to the ARN of ecs-guardian-reports , so the function cannot publish to any other topic.
- No access to workloads. The role cannot stop tasks, update services, or modify clusters. Idle services are reported, never changed.
- Separate execution roles. The Lambda role (
ContainerGuardianRole ) is distinct from ecsTaskExecutionRole , which only the Fargate tasks use.
The practical effect is a small worst case. If the function ever flagged too much, the most it could do is delete images and deregister task definition revisions. It has no permission to touch running tasks or services, and its report can only go to one topic.
The full policy is lambda-execution-policy.json in the repository, so you can review exactly what you are granting before you attach it.
🎚️ Tuning the Thresholds
The five thresholds are the Guardian's dials. Part 2 used test values to make the demo obvious, then switched to production starting points. The right setting for your own environment depends on what you are willing to trade.
Setting | Raise it and... | Lower it and... |
|---|
IMAGE_AGE_DAYS | Recent images stay protected for longer, which leaves more time to notice a bad release | Savings arrive sooner, but fresh images become eligible sooner |
KEEP_LATEST_IMAGES | More rollback targets stay protected, and more images keep billing | You pay for fewer images, with fewer fallbacks |
KEEP_TASKDEF_REVISIONS | A longer revision history is preserved | The console is tidier, with fewer old revisions to return to |
IDLE_CPU_PERCENT | More services count as idle, including lightly used ones | Only the quietest services are reported |
IDLE_DAYS | A longer window filters out services with weekly or occasional load | Detection is faster, with more false positives |
Because an image is flagged only when it is both old enough and outside the newest N, raising either of those two settings protects more images. That lets you be strict on one dial and relaxed on another, for example a long age threshold with a small keep-latest count.
🧱 Where the Guardian Has Limits
A tool that deletes things should be honest about what it cannot know.
- Low CPU is a signal, not proof. The idle check looks at maximum CPU over a window. A service can do useful work with little CPU, or serve rare but important requests, so findings are candidates for a person to confirm. This is why idle services are only ever reported.
- Savings are estimates. The monthly figures come from the Fargate price variables you set, not from your actual bill, so use them to prioritize rather than to forecast.
- Removing a tag may not save money. ECR bills per unique image, and multiple tags can point to the same image digest. Deleting one tag while another still points to the image does not reduce storage cost.
- One deployment covers one region. ECR and ECS are regional, so as built in Part 2 the Guardian covers the region you deployed it in.
- A report only helps if it is read. The Guardian leaves the final decision with a person by design, which means someone has to open the weekly email and act on it.
🚀 What Comes Next
These are ideas for extending the Guardian, not features of the current version.
- Multi-region coverage: run the Guardian once per region, or extend it to loop over a list of regions.
- Richer idle detection: combine CPU with other signals, such as memory or network and request metrics, to reduce false positives.
- More notification channels: send the report to Slack alongside the email, so findings land where the team already works.
- A comparison with ECR lifecycle policies: lifecycle policies can expire images by rule, but they do not look at which images running tasks or task definitions still use. A side-by-side write-up of the two approaches would make a good follow-up post.
📌 Wrapping Up the Series
ECS Guardian started as a question: once a deployment is done, what does it leave behind? Over three posts, that became a design in Part 1, a working build in Part 2, and here, an honest look at its permissions, its dials and its limits. The thread through all of it is restraint: a tool that is allowed to do very little, decides carefully, and leaves the final call with a person.
If you build your own version, I would love to hear what it finds in your account. Thank you for following the series.
🤝 Let’s Connect
Open to ideas, feedback, and meaningful collaboration:
🌐 Explore More
Explore my projects, technical articles, and cloud learning journey through these platforms:
Loading article