Write a script that finds IAM access keys older than 90 days.

IntermediateHands-onPython for DevOps
Answer
from datetime import datetime, timezone
import boto3

iam = boto3.client("iam")
now = datetime.now(timezone.utc)
for page in iam.get_paginator("list_users").paginate():
    for u in page["Users"]:
        for k in iam.list_access_keys(UserName=u["UserName"])["AccessKeyMetadata"]:
            age = (now - k["CreateDate"]).days
            if age > 90 and k["Status"] == "Active":
                print(f"{u['UserName']} {k['AccessKeyId']} {age} days")

Key trick → boto3 returns timezone-aware datetimes, so now must be aware too, or the subtraction raises TypeError. At scale, generate_credential_report is one call instead of N.

All Real-World Interview questions Roadmap: Interview readiness

More on Python for DevOps