cloudsweep-aws 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cloudsweep/__init__.py +126 -0
- cloudsweep/ebs.py +38 -0
- cloudsweep/eip.py +25 -0
- cloudsweep/logs.py +30 -0
- cloudsweep/regions.py +13 -0
- cloudsweep_aws-1.0.0.dist-info/METADATA +93 -0
- cloudsweep_aws-1.0.0.dist-info/RECORD +10 -0
- cloudsweep_aws-1.0.0.dist-info/WHEEL +4 -0
- cloudsweep_aws-1.0.0.dist-info/entry_points.txt +3 -0
- cloudsweep_aws-1.0.0.dist-info/licenses/LICENSE +21 -0
cloudsweep/__init__.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sys
|
|
3
|
+
from importlib.metadata import version
|
|
4
|
+
|
|
5
|
+
import boto3
|
|
6
|
+
import typer
|
|
7
|
+
from botocore.exceptions import ClientError
|
|
8
|
+
from rich.console import Console
|
|
9
|
+
from rich.table import Table
|
|
10
|
+
|
|
11
|
+
from cloudsweep.ebs import find_unattached_volumes
|
|
12
|
+
from cloudsweep.eip import find_unused_elastic_ips
|
|
13
|
+
from cloudsweep.logs import find_log_groups_without_retention
|
|
14
|
+
from cloudsweep.regions import get_enabled_regions
|
|
15
|
+
|
|
16
|
+
app = typer.Typer(
|
|
17
|
+
help="Find forgotten AWS resources that are costing you money.",
|
|
18
|
+
no_args_is_help=True,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def cost_for_sorting(finding: dict) -> float:
|
|
23
|
+
"""Unknown costs count as 0, so they end up at the bottom."""
|
|
24
|
+
if finding["monthly_cost"] is None:
|
|
25
|
+
return 0.0
|
|
26
|
+
return finding["monthly_cost"]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def print_message(text: str) -> None:
|
|
30
|
+
"""Print progress and warnings to stderr, so they never mix with the results."""
|
|
31
|
+
print(text, file=sys.stderr)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def print_table(findings: list[dict]) -> None:
|
|
35
|
+
if not findings:
|
|
36
|
+
print("No forgotten resources found.")
|
|
37
|
+
return
|
|
38
|
+
|
|
39
|
+
table = Table()
|
|
40
|
+
table.add_column("Region")
|
|
41
|
+
table.add_column("Resource")
|
|
42
|
+
table.add_column("ID", no_wrap=True)
|
|
43
|
+
table.add_column("Details")
|
|
44
|
+
table.add_column("Monthly cost", justify="right")
|
|
45
|
+
|
|
46
|
+
total = 0.0
|
|
47
|
+
for f in findings:
|
|
48
|
+
cost = f["monthly_cost"]
|
|
49
|
+
cost_text = "unknown" if cost is None else f"${cost:.2f}"
|
|
50
|
+
table.add_row(f["region"], f["resource"], f["id"], f["details"], cost_text)
|
|
51
|
+
if cost is not None:
|
|
52
|
+
total += cost
|
|
53
|
+
|
|
54
|
+
Console().print(table)
|
|
55
|
+
print(f"Potential savings: ${total:.2f}/month")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def show_version(value: bool) -> None:
|
|
59
|
+
if value:
|
|
60
|
+
print(f"cloudsweep {version('cloudsweep-aws')}")
|
|
61
|
+
raise typer.Exit()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@app.callback()
|
|
65
|
+
def main(
|
|
66
|
+
version_flag: bool = typer.Option(
|
|
67
|
+
False, "--version", callback=show_version, is_eager=True, help="Show the version and exit."
|
|
68
|
+
),
|
|
69
|
+
) -> None:
|
|
70
|
+
pass
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@app.command()
|
|
74
|
+
def scan(
|
|
75
|
+
region: str | None = typer.Option(
|
|
76
|
+
None, "--region", "-r", help="AWS region to scan. Defaults to your AWS CLI region."
|
|
77
|
+
),
|
|
78
|
+
all_regions: bool = typer.Option(
|
|
79
|
+
False, "--all-regions", help="Scan every region enabled for your account."
|
|
80
|
+
),
|
|
81
|
+
output: str = typer.Option(
|
|
82
|
+
"table", "--output", "-o", help="How to show the results: table or json."
|
|
83
|
+
),
|
|
84
|
+
) -> None:
|
|
85
|
+
"""Scan for forgotten resources and estimate what they cost per month."""
|
|
86
|
+
if output not in ["table", "json"]:
|
|
87
|
+
print_message(f"Unknown output format '{output}'. Use table or json.")
|
|
88
|
+
raise typer.Exit(code=2)
|
|
89
|
+
|
|
90
|
+
if region is None:
|
|
91
|
+
region = boto3.Session().region_name
|
|
92
|
+
if region is None:
|
|
93
|
+
print_message("No region given. Use --region, or set one with: aws configure set region <region>")
|
|
94
|
+
raise typer.Exit(code=2)
|
|
95
|
+
|
|
96
|
+
if all_regions:
|
|
97
|
+
regions_to_scan = get_enabled_regions(region)
|
|
98
|
+
else:
|
|
99
|
+
regions_to_scan = [region]
|
|
100
|
+
|
|
101
|
+
findings = []
|
|
102
|
+
skipped = []
|
|
103
|
+
for r in regions_to_scan:
|
|
104
|
+
print_message(f"Scanning {r}...")
|
|
105
|
+
try:
|
|
106
|
+
for f in find_unattached_volumes(r):
|
|
107
|
+
findings.append(f)
|
|
108
|
+
for f in find_unused_elastic_ips(r):
|
|
109
|
+
findings.append(f)
|
|
110
|
+
for f in find_log_groups_without_retention(r):
|
|
111
|
+
findings.append(f)
|
|
112
|
+
except ClientError as error:
|
|
113
|
+
code = error.response["Error"]["Code"]
|
|
114
|
+
print_message(f" Skipped {r}: {code}")
|
|
115
|
+
skipped.append(r)
|
|
116
|
+
|
|
117
|
+
if skipped:
|
|
118
|
+
print_message(f"Warning: could not scan {len(skipped)} region(s): {', '.join(skipped)}")
|
|
119
|
+
|
|
120
|
+
findings = sorted(findings, key=cost_for_sorting, reverse=True)
|
|
121
|
+
|
|
122
|
+
if output == "json":
|
|
123
|
+
print(json.dumps(findings, indent=2))
|
|
124
|
+
else:
|
|
125
|
+
print_message("")
|
|
126
|
+
print_table(findings)
|
cloudsweep/ebs.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import boto3
|
|
2
|
+
|
|
3
|
+
EBS_PRICE_PER_GB = {
|
|
4
|
+
"gp3": 0.08,
|
|
5
|
+
"gp2": 0.10,
|
|
6
|
+
"io2": 0.125,
|
|
7
|
+
"st1": 0.045,
|
|
8
|
+
"sc1": 0.015,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def find_unattached_volumes(region: str) -> list[dict]:
|
|
13
|
+
ec2 = boto3.client("ec2", region_name=region)
|
|
14
|
+
paginator = ec2.get_paginator("describe_volumes")
|
|
15
|
+
findings = []
|
|
16
|
+
|
|
17
|
+
for page in paginator.paginate():
|
|
18
|
+
for volume in page["Volumes"]:
|
|
19
|
+
if volume["State"] != "available":
|
|
20
|
+
continue
|
|
21
|
+
|
|
22
|
+
price = EBS_PRICE_PER_GB.get(volume["VolumeType"])
|
|
23
|
+
if price is None:
|
|
24
|
+
monthly_cost = None
|
|
25
|
+
else:
|
|
26
|
+
monthly_cost = round(volume["Size"] * price, 2)
|
|
27
|
+
|
|
28
|
+
findings.append({
|
|
29
|
+
"resource": "EBS volume",
|
|
30
|
+
"id": volume["VolumeId"],
|
|
31
|
+
"region": region,
|
|
32
|
+
"details": f"{volume['VolumeType']}, {volume['Size']} GB",
|
|
33
|
+
"type": volume["VolumeType"],
|
|
34
|
+
"size_gb": volume["Size"],
|
|
35
|
+
"monthly_cost": monthly_cost,
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
return findings
|
cloudsweep/eip.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import boto3
|
|
2
|
+
|
|
3
|
+
PUBLIC_IP_PRICE_PER_HOUR = 0.005
|
|
4
|
+
HOURS_PER_MONTH = 730
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def find_unused_elastic_ips(region: str) -> list[dict]:
|
|
8
|
+
"""Return Elastic IPs in a region that aren't attached to anything."""
|
|
9
|
+
ec2 = boto3.client("ec2", region_name=region)
|
|
10
|
+
response = ec2.describe_addresses()
|
|
11
|
+
findings = []
|
|
12
|
+
|
|
13
|
+
for address in response["Addresses"]:
|
|
14
|
+
if "AssociationId" in address:
|
|
15
|
+
continue
|
|
16
|
+
|
|
17
|
+
findings.append({
|
|
18
|
+
"resource": "Elastic IP",
|
|
19
|
+
"id": address["AllocationId"],
|
|
20
|
+
"region": region,
|
|
21
|
+
"details": address["PublicIp"],
|
|
22
|
+
"monthly_cost": round(PUBLIC_IP_PRICE_PER_HOUR * HOURS_PER_MONTH, 2),
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
return findings
|
cloudsweep/logs.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import boto3
|
|
2
|
+
|
|
3
|
+
LOG_STORAGE_PRICE_PER_GB = 0.03
|
|
4
|
+
BYTES_PER_GB = 1024 * 1024 * 1024
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def find_log_groups_without_retention(region: str) -> list[dict]:
|
|
8
|
+
"""Return log groups that keep their logs forever and have data stored."""
|
|
9
|
+
logs = boto3.client("logs", region_name=region)
|
|
10
|
+
paginator = logs.get_paginator("describe_log_groups")
|
|
11
|
+
findings = []
|
|
12
|
+
|
|
13
|
+
for page in paginator.paginate():
|
|
14
|
+
for group in page["logGroups"]:
|
|
15
|
+
if "retentionInDays" in group:
|
|
16
|
+
continue
|
|
17
|
+
if group["storedBytes"] == 0:
|
|
18
|
+
continue
|
|
19
|
+
|
|
20
|
+
stored_gb = group["storedBytes"] / BYTES_PER_GB
|
|
21
|
+
|
|
22
|
+
findings.append({
|
|
23
|
+
"resource": "Log group",
|
|
24
|
+
"id": group["logGroupName"],
|
|
25
|
+
"region": region,
|
|
26
|
+
"details": f"{stored_gb:.2f} GB, never expires",
|
|
27
|
+
"monthly_cost": round(stored_gb * LOG_STORAGE_PRICE_PER_GB, 2),
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
return findings
|
cloudsweep/regions.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import boto3
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def get_enabled_regions(region: str) -> list[str]:
|
|
5
|
+
"""Return the names of all regions enabled for this AWS account."""
|
|
6
|
+
ec2 = boto3.client("ec2", region_name=region)
|
|
7
|
+
response = ec2.describe_regions()
|
|
8
|
+
|
|
9
|
+
regions = []
|
|
10
|
+
for r in response["Regions"]:
|
|
11
|
+
regions.append(r["RegionName"])
|
|
12
|
+
|
|
13
|
+
return regions
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cloudsweep-aws
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Find forgotten AWS resources that are costing you money.
|
|
5
|
+
Keywords: aws,cost,cloud,cli,finops
|
|
6
|
+
Author: Abdullah Tahir
|
|
7
|
+
Author-email: Abdullah Tahir <157417306+abdthr@users.noreply.github.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: boto3[crt]>=1.43.118
|
|
11
|
+
Requires-Dist: rich>=15.0.0
|
|
12
|
+
Requires-Dist: typer>=0.27.3
|
|
13
|
+
Requires-Python: >=3.12
|
|
14
|
+
Project-URL: Homepage, https://github.com/abdthr/cloudsweep
|
|
15
|
+
Project-URL: Issues, https://github.com/abdthr/cloudsweep/issues
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# cloudsweep
|
|
19
|
+
[](https://github.com/abdthr/cloudsweep/actions/workflows/tests.yml)
|
|
20
|
+
|
|
21
|
+
Find forgotten AWS resources that are costing you money.
|
|
22
|
+
|
|
23
|
+
You delete an EC2 instance, but its disk stays behind. It's attached to nothing,
|
|
24
|
+
nobody remembers it exists and it shows up on your bill every month. Cloudsweep
|
|
25
|
+
finds that kind of leftover and tells you roughly what it's costing you.
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
$ cloudsweep scan --all-regions
|
|
29
|
+
...
|
|
30
|
+
┏━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
|
|
31
|
+
┃ Region ┃ Resource ┃ ID ┃ Details ┃ Monthly cost ┃
|
|
32
|
+
┡━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━┩
|
|
33
|
+
│ us-east-1 │ EBS volume │ vol-0a7c3f19e2b84d561 │ gp2, 500 GB │ $50.00 │
|
|
34
|
+
│ eu-north-1 │ EBS volume │ vol-0e01dd418b516f0ab │ gp3, 100 GB │ $8.00 │
|
|
35
|
+
│ us-east-1 │ Elastic IP │ eipalloc-07d4c1e9a2b3f5a68 │ 3.91.24.17 │ $3.65 │
|
|
36
|
+
└────────────┴────────────┴────────────────────────────┴─────────────┴──────────────┘
|
|
37
|
+
Potential savings: $61.65/month
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
It can scan a single region or every region your account has enabled.
|
|
41
|
+
|
|
42
|
+
## What it finds
|
|
43
|
+
|
|
44
|
+
| Resource | Why it costs money |
|
|
45
|
+
|---|---|
|
|
46
|
+
| Unattached EBS volumes | Disks are billed per GB every month, even when nothing uses them |
|
|
47
|
+
| Unused Elastic IPs | AWS charges for every public IPv4 address by the hour, attached or not |
|
|
48
|
+
| Log groups that never expire | CloudWatch keeps logs forever by default, and you pay for every GB stored |
|
|
49
|
+
|
|
50
|
+
## How it behaves
|
|
51
|
+
|
|
52
|
+
Cloudsweep is read-only. It lists what it finds and leaves the decision to you,
|
|
53
|
+
because an "unused" volume might be someone's backup. If it can't price something,
|
|
54
|
+
it still shows it and marks the cost as `unknown` instead of hiding it.
|
|
55
|
+
|
|
56
|
+
If a region can't be scanned (for example because a policy blocks it), cloudsweep
|
|
57
|
+
skips it, tells you which ones it missed and carries on with the rest.
|
|
58
|
+
|
|
59
|
+
Costs are estimates based on approximate US prices, so treat them as a rough guide.
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
You'll need [uv](https://docs.astral.sh/uv/) and AWS credentials (for example from `aws login`).
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
git clone https://github.com/abdthr/cloudsweep.git
|
|
67
|
+
cd cloudsweep
|
|
68
|
+
uv run cloudsweep scan # uses your default AWS region
|
|
69
|
+
uv run cloudsweep scan --region us-east-1
|
|
70
|
+
uv run cloudsweep scan --all-regions # every region enabled for your account
|
|
71
|
+
uv run cloudsweep scan --output json # machine-readable, for scripts
|
|
72
|
+
uv run cloudsweep --help
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
It only needs read permissions: `ec2:DescribeVolumes`, `ec2:DescribeAddresses`,
|
|
76
|
+
`ec2:DescribeRegions` and `logs:DescribeLogGroups`.
|
|
77
|
+
|
|
78
|
+
## Tests
|
|
79
|
+
|
|
80
|
+
The tests use [moto](https://github.com/getmoto/moto) to fake AWS, so they don't
|
|
81
|
+
need an account or cost anything.
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
uv run pytest
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## What's next
|
|
88
|
+
|
|
89
|
+
- Publish it so you can `pip install cloudsweep`
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
MIT
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
cloudsweep/__init__.py,sha256=PvoVNOcq6hAAtmKnm8t9EEYQx2FusemVno-6QKdA8iw,3809
|
|
2
|
+
cloudsweep/ebs.py,sha256=hD4BxSb-gcAF4_zOa3JPYbTP0FBn6KXKFx60sF5W4ss,1061
|
|
3
|
+
cloudsweep/eip.py,sha256=yvNg26To1iT_e5EgIFGAehtWgvYkfDCIBVXxekSaNgc,708
|
|
4
|
+
cloudsweep/logs.py,sha256=CIBGp8KP9Rw10FE3FRvym4R6l3i_v-7yeAfxXWbdXHk,961
|
|
5
|
+
cloudsweep/regions.py,sha256=Ok-VE3lsnvcO8c8z8XzUJTGkcVhtb0HiUh5Dp4vNwPQ,337
|
|
6
|
+
cloudsweep_aws-1.0.0.dist-info/licenses/LICENSE,sha256=NOM-FgOtMUGrRuxXKuA32CX_SlDtxkIBIvT3vMnhAjQ,1071
|
|
7
|
+
cloudsweep_aws-1.0.0.dist-info/WHEEL,sha256=-gI5GRFdiNNn2CFz-ptlExISMINC-k8bFxeojAlHbrM,81
|
|
8
|
+
cloudsweep_aws-1.0.0.dist-info/entry_points.txt,sha256=rZKfJyG_HWGgahXFZ8sceurS62v4j4xYv5REDVTy7VM,47
|
|
9
|
+
cloudsweep_aws-1.0.0.dist-info/METADATA,sha256=J0f6foHDb0CZqVvvAI2pL02eusHM54lN06NjOWlyVTI,4062
|
|
10
|
+
cloudsweep_aws-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Abdullah Tahir
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|