Dragon Sky FinOps: 62% lower infrastructure cost Design and implementation
Linked revenue metrics with infrastructure cost and found unused resources automatically every day with Python, cutting infrastructure cost by up to 62%.
Finding unused resources (related public repository pf-05)
What this code does: Walks through each AWS account and region looking for unused resources. A public IP counts as unused when it is attached to no instance and has no association, and its name, allocation ID, and IP are recorded together. For security groups, names matching the exclusion rules are skipped, and the rest are checked for any network interface that uses them. The results pile up in one common shape (UnusedResource), so cleanup candidates can be read from a single list.
for account, region, client in provider.regional("ec2"):
base = {"cloud": "AWS", "project": account.name, "project_id": account.account_id, "region": region}
for address in client.describe_addresses()["Addresses"]:
if not address.get("InstanceId") and not address.get("AssociationId"):
label = (
f"{_name(address)} ({address.get('AllocationId')} / {address.get('PublicIp')})".strip()
)
records.append(UnusedResource(resource_type="IP", resource=label, **base))
for group in client.describe_security_groups()["SecurityGroups"]:
if _excluded(group["GroupName"], rules):
continue
used = client.describe_network_interfaces(
Filters=[{"Name": "group-id", "Values": [group["GroupId"]]}]
)["NetworkInterfaces"]
if not used: