Interview Prep/Python for DevOps/Automation scripting & CLI design

Your manager asks for a weekly CSV of every EC2 instance, its owner tag and monthly cost across 8 accounts. How do you build it?

IntermediateScenarioAutomation scripting & CLI design
Answer

Situation read → Multi-account inventory plus cost data. EC2 APIs do not return cost.

What I'd do →

  1. Assume a read-only role in each account (sts.assume_role) from a central tooling account.
  2. List instances per region with paginators, building rows with InstanceId, type, state, Owner tag (or "MISSING").
  3. Get cost from Cost Explorer (ce.get_cost_and_usage grouped by a cost allocation tag or resource-level data) or from the CUR in Athena; do not estimate from price lists if real data exists.
  4. Write with csv.DictWriter, upload to S3, email or post the link.
  5. Schedule weekly in CI with OIDC.

Why → Inventory from APIs plus cost from billing data is accurate and auditable.

Interview tip → Mention activating the Owner cost allocation tag; without that, cost by owner is impossible.

All Python for DevOps questions Roadmap: Python for automation

More on Automation scripting & CLI design