You 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?
IntermediateScenarioAWS automation with boto3
Answer
Situation read → Old snapshot cleanup with dependencies; deleting the wrong one breaks AMIs or compliance retention.
What I'd do →
- List with
describe_snapshots(OwnerIds=["self"])paginator; filterStartTime < now - 90dusing timezone-aware datetimes. - Exclude snapshots referenced by registered AMIs (
describe_images(Owners=["self"]), collectBlockDeviceMappings[].Ebs.SnapshotId). - Exclude snapshots managed by AWS Backup or DLM (tags like
aws:backup:source-resourceordlm:managed) and anything taggedRetain=true. - Dry-run report first; delete with
delete_snapshot, handlingInvalidSnapshot.InUseas skip, not failure. - Long term, let DLM or AWS Backup own retention.
Why → Delete only what you can prove is unowned.
Interview tip → Mentioning the AMI cross-check shows you have actually done this.
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?BeginnerYour 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?AdvancedYou are asked to tag every untagged resource in an account with `Owner=unknown`. What questions do you ask and how do you implement it?Intermediate