How would you write a Python script that enforces "no public S3 buckets" across accounts?
IntermediateScenarioAWS automation with boto3
Answer
Situation read → Detection and remediation; prevention is better.
What I'd do →
- Prevent first: account-level S3 Block Public Access (
s3control.put_public_access_block) and an SCP that denies changing it. - Detect: for each bucket, check
get_public_access_block, bucket policy status (get_bucket_policy_statusreturnsIsPublic), and ACLs; handleNoSuchPublicAccessBlockConfigurationas "not set". - Remediate with an allow-list for intentional public buckets (static sites behind CloudFront should use OAC instead).
- Report findings; auto-remediate only with approval.
Why → Account-level controls stop the problem; scripts verify they stay on.
Interview tip → Mention get_bucket_policy_status; it saves you parsing policies yourself.
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