Write a boto3 script that finds S3 buckets without default encryption or with public access not blocked.

AdvancedHands-onPython for DevOps
Answer
import boto3
from botocore.exceptions import ClientError

s3 = boto3.client("s3")
for b in s3.list_buckets()["Buckets"]:
    name = b["Name"]
    issues = []
    try:
        pab = s3.get_public_access_block(Bucket=name)["PublicAccessBlockConfiguration"]
        if not all(pab.values()):
            issues.append("public access block partially off")
    except ClientError as e:
        if e.response["Error"]["Code"] == "NoSuchPublicAccessBlockConfiguration":
            issues.append("no public access block")
        else:
            raise
    try:
        s3.get_bucket_encryption(Bucket=name)
    except ClientError as e:
        if e.response["Error"]["Code"] == "ServerSideEncryptionConfigurationNotFoundError":
            issues.append("no default encryption")
    print(f"{name}: {', '.join(issues) or 'OK'}")

Key trick → boto3 reports "not configured" as a ClientError with a specific code, so branch on e.response["Error"]["Code"] instead of catching everything. (Since January 2023 new buckets get SSE-S3 by default, so the encryption check mostly flags old buckets.)

All Real-World Interview questions Roadmap: Interview readiness

More on Python for DevOps