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 →
- Assume a read-only role in each account (
sts.assume_role) from a central tooling account. - List instances per region with paginators, building rows with
InstanceId, type, state,Ownertag (or"MISSING"). - Get cost from Cost Explorer (
ce.get_cost_and_usagegrouped 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. - Write with
csv.DictWriter, upload to S3, email or post the link. - 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.
More on Automation scripting & CLI design
Your team has a 300-line Bash script that parses JSON with `jq`, loops over AWS accounts and retries with `sleep`. It breaks every month. Do you rewrite it in Python?BeginnerA colleague's script deletes files older than 30 days from `/var/log/app`. You are asked to review it before it goes into cron. What do you look for?IntermediateYou need a script that ops engineers run from laptops and that CI runs nightly. How do you design its interface?IntermediateA script reads a 15 GB access log to count 5xx errors per endpoint, and the box runs out of memory. How do you fix it?BeginnerYou must run the same Python automation on 40 servers that have Python 3.6, 3.9 and 3.12. What do you do?IntermediateA cron job that runs a Python script fails silently. Nobody noticed for two weeks. How do you make sure that never happens again?Intermediate