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.
More on Python for DevOps
Write a Python script using boto3 that lists all running EC2 instances with their Name tag.BeginnerWrite a script that finds unattached EBS volumes and deletes them (with a dry-run flag).IntermediateCall a REST API with `requests`, handle timeouts and non-2xx responses properly.BeginnerAdd retry with exponential backoff to HTTP calls without writing the loop yourself.IntermediateWrite your own retry decorator with exponential backoff and jitter.AdvancedParse a YAML file and print every container image in a multi-document Kubernetes manifest.Intermediate