Write a script that finds unattached EBS volumes and deletes them (with a dry-run flag).
IntermediateHands-onPython for DevOps
Answer
import argparse
import boto3
parser = argparse.ArgumentParser()
parser.add_argument("--apply", action="store_true", help="actually delete")
args = parser.parse_args()
ec2 = boto3.client("ec2")
pages = ec2.get_paginator("describe_volumes").paginate(
Filters=[{"Name": "status", "Values": ["available"]}]
)
for page in pages:
for v in page["Volumes"]:
print(f"{v['VolumeId']} {v['Size']}GiB created {v['CreateTime']:%Y-%m-%d}")
if args.apply:
ec2.delete_volume(VolumeId=v["VolumeId"])
print(" deleted")
Key trick → status=available means not attached. Default to dry-run and require --apply; interviewers check whether you make destructive scripts safe by default.
More on Python for DevOps
Write a Python script using boto3 that lists all running EC2 instances with their Name tag.BeginnerCall 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.IntermediateRun a shell command from Python, capture output, and fail properly on error.Intermediate