Finance asks why EC2 spend doubled last month. You have Python and read access. What do you do?
IntermediateScenarioAWS automation with boto3
Answer
Situation read → Cost investigation; the data is in Cost Explorer, not EC2.
What I'd do →
- Query Cost Explorer:
ce.get_cost_and_usage(TimePeriod=..., Granularity="DAILY", Metrics=["UnblendedCost"], GroupBy=[{"Type": "DIMENSION", "Key": "USAGE_TYPE"}])to see when and which usage type jumped. - Group by
LINKED_ACCOUNT,REGION,INSTANCE_TYPEand by theOwnertag to narrow it. - Cross-check with CloudTrail
RunInstancesevents around the day of the jump. - Report with a daily chart and the specific resources.
Why → Start from billing data and drill down; do not guess from current inventory.
Interview tip → Note that Cost Explorer API calls cost $0.01 each, so cache results.
More on AWS automation with boto3
You are asked to write a script that stops all dev EC2 instances at 8 PM and starts them at 8 AM. How do you build it?BeginnerA script that lists all S3 objects in a bucket reports 1,000 objects, but the console says there are 48,000. What happened, and how do you fix the script?BeginnerYou need to find and delete EBS snapshots older than 90 days, but some are used by AMIs and backup plans. How do you write it safely?IntermediateYour audit script hits `ThrottlingException` / `RequestLimitExceeded` when scanning large accounts. What do you change?AdvancedYou must audit 40 AWS accounts in an Organization from one script. How do you handle credentials?AdvancedA Lambda written in Python times out at 15 minutes while processing a big S3 prefix. What are your options?Advanced