Skip to main content

From Five Python Scripts to a Real AWS Tool: tagaudit

Python for DevOps, built the practical way: five automation scripts, then a tested, CI-checked CLI that caught a real untagged EC2 instance.

Updated 5 min readMarkdown
From Five Python Scripts to a Real AWS Tool: tagaudit

I launched a server on AWS and left off one tag on purpose. A tool I'd built caught it in about a second.

That's where this module ended. It didn't start anywhere near there.

Python nearly lost me

I started this module the way I'd started most of the others: concepts first, then commands. For Python, that didn't work. Three explanations in, I was reading about nested dictionaries and import behaviour before I'd ever watched Python do a single thing. It felt like noise.

So I changed how I was learning. I opened the Python prompt and typed one line at a time.

"8" + "8" gave me '88'.

"32" > "8" gave me False.

Those two lines taught me more than any table. When Python reads a file, every value arrives as text. Text gets glued together instead of added, and it's compared letter by letter, so "32" loses to "8" because 3 comes before 8. If you don't convert at the door with int(), a 32GB database server gets flagged as underpowered and someone wastes a morning "fixing" it.

From there it moved quickly: lists, dictionaries, loops, if/else, functions. Small pieces, each one tried before it was explained.

Phase 1: five scripts that replace five manual jobs

The scenario is PulseHealth, a health-tech company whose team was doing five checks by hand.

Script The manual job it replaces
Server classifier Checking production servers meet the minimum spec
Log parser Reading logs by eye to find errors
Inventory reporter (JSON) Noticing a service is running fewer copies than it should
Health checker (requests) Checking outside services are up
CSV inventory Adding up server resources from a spreadsheet
Log parser pulling ERROR lines out of an application log

Twelve log lines in, four errors out, and a report file written. That's a job someone was doing by eye.

The idea that clicked hardest was exit codes. A script finishing with 0 means "all good", and anything else means "something's wrong". The inventory reporter exits 1 when a service is degraded. Jenkins and GitHub Actions read that number and stop the pipeline. A ten-line Python check can block a bad deploy.

Inventory reporter flagging a degraded service and exiting with code 1

One service running one of its two copies. The script warns and exits 1, which is enough to fail a pipeline.

Phase 2: tagaudit, a tool with a real job

Untagged cloud resources are how bills grow quietly. Nobody knows who owns a server, so nobody dares delete it.

tagaudit lists every live EC2 instance missing a required tag (Project, Owner, Env by default) and exits 1 if it finds one. It lives in its own repo, because it's a real tool rather than an exercise.

The toolchain:

  • uv to set up the project and lock exact package versions
  • typer and rich for the command line and the table
  • boto3 to talk to AWS, using a paginator so large accounts aren't cut off
  • ruff for style, mypy --strict for types
  • pytest with mocked AWS responses, so tests need no account and cost nothing
Ruff, mypy and pytest all passing with 94% coverage

Lint clean, strict types clean, five tests passing. None of them touched AWS.

The lab had bugs, and fixing them was the best part

The lab's own code had four problems:

  1. It used a list as a default argument, a classic Python trap where the list ends up shared between calls.
  2. It printed "No tag violations" only when there were no instances at all, and counted compliant instances as problems.
  3. It never installed boto3, and mypy --strict fails without boto3's type stubs.
  4. It reported terminated instances, which stay visible in AWS for about an hour after they're gone. Your audit would flag servers that no longer exist.

Each one is fixed, and the tests cover the paths that matter.

Breaking the rule on purpose

Mocks prove the logic. They don't prove the tool works on a real account. So, after checking the budget alarms were still in place, I launched one t3.micro with every tag except Owner.

tagaudit flagging a real EC2 instance missing its Owner tag

A real instance, a missing Owner tag, and exit code 1.

Then I added the tag and ran it again.

tagaudit passing after the Owner tag was added

Green, exit code 0. The instance was terminated straight after and confirmed gone, about five minutes of total runtime.

The final run after termination came back empty. That's fix number 4 doing its job.

CI on every push

GitHub Actions runs the same checks on every push and pull request: install from the lock file, lint, format check, strict types, tests. The first run went green in 18 seconds.

GitHub Actions run passing for tagaudit

The same checks, run by GitHub, on every change.

What I'm taking with me

  • Convert data to the right type the moment it enters your script.
  • Exit codes are how scripts talk to pipelines. Use them on purpose.
  • Keep the logic separate from the command line, so you can test it without the outside world.
  • Mock the cloud in tests, then prove it once against the real thing.
  • If a way of learning isn't working, change the method, not your opinion of yourself.

Repos


About the author

  • I build production-grade cloud infrastructure and agentic AI pipelines on AWS. With a background in biochemistry and biotechnology, I bring scientific precision to the systems I design. Every pipeline I build has deliberate safety guardrails -- minimal permissions, read-only auditors, plan-before-apply gates. Safety is architecture, not discipline. Currently writing a 7-part series documenting the Agentic Infrastructure project: a complete Claude Code pipeline from environment setup to live AWS deployment, SubAgents, MCP integration, and safety hooks. Portfolio: viviancloud.site

No comments yet. Be the first to comment.