costwatch 0.1.0__tar.gz
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.
- costwatch-0.1.0/LICENSE +202 -0
- costwatch-0.1.0/PKG-INFO +150 -0
- costwatch-0.1.0/README.md +128 -0
- costwatch-0.1.0/pyproject.toml +66 -0
- costwatch-0.1.0/pyproject.toml.orig +51 -0
- costwatch-0.1.0/src/costwatch/__init__.py +5 -0
- costwatch-0.1.0/src/costwatch/aws.py +38 -0
- costwatch-0.1.0/src/costwatch/checks/__init__.py +34 -0
- costwatch-0.1.0/src/costwatch/checks/ebs.py +108 -0
- costwatch-0.1.0/src/costwatch/checks/ec2.py +214 -0
- costwatch-0.1.0/src/costwatch/checks/elb.py +72 -0
- costwatch-0.1.0/src/costwatch/checks/logs.py +31 -0
- costwatch-0.1.0/src/costwatch/checks/network.py +64 -0
- costwatch-0.1.0/src/costwatch/checks/rds.py +167 -0
- costwatch-0.1.0/src/costwatch/cli.py +95 -0
- costwatch-0.1.0/src/costwatch/iam.py +41 -0
- costwatch-0.1.0/src/costwatch/metrics.py +48 -0
- costwatch-0.1.0/src/costwatch/models.py +31 -0
- costwatch-0.1.0/src/costwatch/owners.py +124 -0
- costwatch-0.1.0/src/costwatch/pricing.py +132 -0
- costwatch-0.1.0/src/costwatch/py.typed +0 -0
- costwatch-0.1.0/src/costwatch/report.py +87 -0
- costwatch-0.1.0/src/costwatch/scanner.py +64 -0
costwatch-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
costwatch-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: costwatch
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Find wasted AWS spend: idle and orphaned resources with estimated monthly cost
|
|
5
|
+
Keywords: aws,finops,cost,cloud,waste,devops
|
|
6
|
+
Author: jonnym
|
|
7
|
+
Author-email: jonnym <26524200+jonmelia@users.noreply.github.com>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Environment :: Console
|
|
12
|
+
Classifier: Intended Audience :: System Administrators
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: System :: Systems Administration
|
|
16
|
+
Requires-Dist: boto3>=1.43.110
|
|
17
|
+
Requires-Dist: rich>=15.0.0
|
|
18
|
+
Requires-Python: >=3.12
|
|
19
|
+
Project-URL: Repository, https://github.com/jonmelia/costwatch
|
|
20
|
+
Project-URL: Issues, https://github.com/jonmelia/costwatch/issues
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# costwatch
|
|
24
|
+
|
|
25
|
+
[](https://github.com/jonmelia/costwatch/actions/workflows/ci.yml)
|
|
26
|
+
[](LICENSE)
|
|
27
|
+
|
|
28
|
+
**Find wasted AWS spend, and who left it running.**
|
|
29
|
+
|
|
30
|
+
costwatch scans an AWS account for idle and orphaned resources, estimates what each one costs
|
|
31
|
+
per month, and (with `--owners`) works out who owns it from tags or CloudTrail.
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
$ costwatch scan --owners # example output, abridged
|
|
35
|
+
Account 123456789012
|
|
36
|
+
$/month Check Region Resource Details Owner
|
|
37
|
+
50.00 unattached-ebs-volume eu-west-1 old-db-data 500 GiB gp2 volume not attached to any… alice@example.com (AWSReservedSSO_Admin)
|
|
38
|
+
16.43 idle-load-balancer eu-west-1 staging-alb application load balancer has no regis… deploy-bot
|
|
39
|
+
9.50 old-rds-snapshot eu-west-1 pre-upgrade 100 GiB manual snapshot of postgres in… dba-team (tag:Team)
|
|
40
|
+
3.65 unused-elastic-ip us-east-1 eipalloc-… Elastic IP 3.250.1.10 is not associated… unknown
|
|
41
|
+
|
|
42
|
+
Waste by owner
|
|
43
|
+
alice@example.com (AWSReservedSSO_Admin) 1 50.00
|
|
44
|
+
deploy-bot 1 16.43
|
|
45
|
+
...
|
|
46
|
+
Total: ~$79.58/month (~$955/year) across 17 region(s)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
- **Read-only.** It only calls `Describe*`/`Lookup*` APIs. Run `costwatch policy` to see
|
|
50
|
+
exactly what it needs.
|
|
51
|
+
- **Local.** It runs on your machine with your credentials. Nothing is sent anywhere.
|
|
52
|
+
|
|
53
|
+
## Install
|
|
54
|
+
|
|
55
|
+
Requires Python 3.12+.
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
uv tool install git+https://github.com/jonmelia/costwatch
|
|
59
|
+
# or
|
|
60
|
+
pipx install git+https://github.com/jonmelia/costwatch
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Usage
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
costwatch scan # all enabled regions, default credentials / $AWS_PROFILE
|
|
67
|
+
costwatch scan --profile prod --region eu-west-1 --region us-east-1
|
|
68
|
+
costwatch scan --owners # add who owns each resource
|
|
69
|
+
costwatch scan --min-cost 5 # hide findings under $5/month
|
|
70
|
+
costwatch scan --snapshot-age-days 180 --stopped-days 60
|
|
71
|
+
costwatch scan --idle-days 30 # look at 30 days of CloudWatch metrics
|
|
72
|
+
costwatch scan --json > findings.json # machine-readable
|
|
73
|
+
costwatch policy # IAM policy for a read-only role
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
If a check isn't permitted or a region is disabled, it's reported at the end and the rest of
|
|
77
|
+
the scan carries on.
|
|
78
|
+
|
|
79
|
+
## Checks
|
|
80
|
+
|
|
81
|
+
| Check | Flags | Estimated cost |
|
|
82
|
+
|---|---|---|
|
|
83
|
+
| `unattached-ebs-volume` | EBS volumes not attached to an instance | Storage + provisioned IOPS/throughput |
|
|
84
|
+
| `old-ebs-snapshot` | EBS snapshots older than 90 days that no AMI uses | Upper bound (snapshots are incremental) |
|
|
85
|
+
| `gp2-volume` | Attached gp2 volumes | Saving from switching to gp3 at the same performance |
|
|
86
|
+
| `unused-elastic-ip` | Elastic IPs not associated with anything | $0.005/hour |
|
|
87
|
+
| `long-stopped-instance` | Instances stopped for 30+ days | Their attached EBS volumes |
|
|
88
|
+
| `idle-ec2-instance` | Running instances whose CPU never went above 5% in 14 days | Linux on-demand instance price |
|
|
89
|
+
| `unused-ami` | Your AMIs older than 90 days that no instance or launch template uses | Their snapshots (upper bound) |
|
|
90
|
+
| `idle-load-balancer` | ALB/NLB/GWLB/Classic with no registered targets | Hourly load balancer charge |
|
|
91
|
+
| `idle-nat-gateway` | NAT gateways that sent under 1 MiB in 14 days | $0.045/hour (plus any data) |
|
|
92
|
+
| `old-rds-snapshot` | Manual RDS/Aurora snapshots older than 90 days | Upper bound: allocated size × backup rate |
|
|
93
|
+
| `idle-rds-instance` | RDS instances with no connections in 14 days | Instance (× 2 for Multi-AZ) + storage |
|
|
94
|
+
| `retained-rds-backup` | Automated backups kept after their instance was deleted | Upper bound: allocated size × backup rate |
|
|
95
|
+
| `log-group-no-retention` | CloudWatch log groups over 1 GiB with no retention set | Current storage at $0.03/GB-month |
|
|
96
|
+
|
|
97
|
+
The idle checks use CloudWatch metrics and are deliberately conservative: an instance only
|
|
98
|
+
counts as idle if its CPU *never* went above 5%, and resources with no metric data are skipped.
|
|
99
|
+
|
|
100
|
+
Prices are us-east-1 on-demand approximations ([`pricing.py`](src/costwatch/pricing.py)).
|
|
101
|
+
They're for ranking waste, not for matching your bill to the cent; other regions are usually
|
|
102
|
+
within about 20%. Instance types missing from the price table are still reported, with a note
|
|
103
|
+
and a cost of $0.
|
|
104
|
+
|
|
105
|
+
## Owners (`--owners`)
|
|
106
|
+
|
|
107
|
+
For each finding, costwatch looks for an owner in this order:
|
|
108
|
+
|
|
109
|
+
1. **Tags:** `Owner`, `CreatedBy`/`created-by`/`created_by`, `Contact`, `Team` (any case).
|
|
110
|
+
2. **CloudTrail:** the identity that created the resource. SSO users show as
|
|
111
|
+
`user@example.com (PermissionSetRole)`, IAM users by name.
|
|
112
|
+
|
|
113
|
+
CloudTrail event history only goes back 90 days, so older resources show as `unknown` unless
|
|
114
|
+
they're tagged. Lookups are rate-limited by AWS (2 per second per region), so this is slower
|
|
115
|
+
on accounts with lots of findings.
|
|
116
|
+
|
|
117
|
+
## Development
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
git clone https://github.com/jonmelia/costwatch && cd costwatch
|
|
121
|
+
uv sync
|
|
122
|
+
uv run pytest # moto-based, no real AWS calls
|
|
123
|
+
uv run ruff check . && uv run ruff format .
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
`tests/e2e/` runs the real `costwatch` command against a [moto](https://github.com/getmoto/moto)
|
|
127
|
+
server. To try it by hand against a fake account full of waste:
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
uv run moto_server -p 5000
|
|
131
|
+
uv run python -m tests.e2e.seed --endpoint http://localhost:5000
|
|
132
|
+
export AWS_ACCESS_KEY_ID=testing AWS_SECRET_ACCESS_KEY=testing
|
|
133
|
+
AWS_ENDPOINT_URL=http://localhost:5000 \
|
|
134
|
+
uv run costwatch scan --region eu-west-1 --snapshot-age-days 0 --stopped-days 0 --min-cost 1
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Adding a check
|
|
138
|
+
|
|
139
|
+
1. Write a function `(session, region, config) -> list[Finding]` in
|
|
140
|
+
[`src/costwatch/checks/`](src/costwatch/checks/) and add it to `ALL_CHECKS`.
|
|
141
|
+
2. Add any new permissions to [`iam.py`](src/costwatch/iam.py).
|
|
142
|
+
3. If it should support `--owners`, add its create event(s) to `CREATE_EVENTS` in
|
|
143
|
+
[`owners.py`](src/costwatch/owners.py).
|
|
144
|
+
4. Test it with moto.
|
|
145
|
+
|
|
146
|
+
Contributions welcome: open an issue first for anything large.
|
|
147
|
+
|
|
148
|
+
## License
|
|
149
|
+
|
|
150
|
+
[Apache 2.0](LICENSE)
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# costwatch
|
|
2
|
+
|
|
3
|
+
[](https://github.com/jonmelia/costwatch/actions/workflows/ci.yml)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
**Find wasted AWS spend, and who left it running.**
|
|
7
|
+
|
|
8
|
+
costwatch scans an AWS account for idle and orphaned resources, estimates what each one costs
|
|
9
|
+
per month, and (with `--owners`) works out who owns it from tags or CloudTrail.
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
$ costwatch scan --owners # example output, abridged
|
|
13
|
+
Account 123456789012
|
|
14
|
+
$/month Check Region Resource Details Owner
|
|
15
|
+
50.00 unattached-ebs-volume eu-west-1 old-db-data 500 GiB gp2 volume not attached to any… alice@example.com (AWSReservedSSO_Admin)
|
|
16
|
+
16.43 idle-load-balancer eu-west-1 staging-alb application load balancer has no regis… deploy-bot
|
|
17
|
+
9.50 old-rds-snapshot eu-west-1 pre-upgrade 100 GiB manual snapshot of postgres in… dba-team (tag:Team)
|
|
18
|
+
3.65 unused-elastic-ip us-east-1 eipalloc-… Elastic IP 3.250.1.10 is not associated… unknown
|
|
19
|
+
|
|
20
|
+
Waste by owner
|
|
21
|
+
alice@example.com (AWSReservedSSO_Admin) 1 50.00
|
|
22
|
+
deploy-bot 1 16.43
|
|
23
|
+
...
|
|
24
|
+
Total: ~$79.58/month (~$955/year) across 17 region(s)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
- **Read-only.** It only calls `Describe*`/`Lookup*` APIs. Run `costwatch policy` to see
|
|
28
|
+
exactly what it needs.
|
|
29
|
+
- **Local.** It runs on your machine with your credentials. Nothing is sent anywhere.
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
Requires Python 3.12+.
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
uv tool install git+https://github.com/jonmelia/costwatch
|
|
37
|
+
# or
|
|
38
|
+
pipx install git+https://github.com/jonmelia/costwatch
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
costwatch scan # all enabled regions, default credentials / $AWS_PROFILE
|
|
45
|
+
costwatch scan --profile prod --region eu-west-1 --region us-east-1
|
|
46
|
+
costwatch scan --owners # add who owns each resource
|
|
47
|
+
costwatch scan --min-cost 5 # hide findings under $5/month
|
|
48
|
+
costwatch scan --snapshot-age-days 180 --stopped-days 60
|
|
49
|
+
costwatch scan --idle-days 30 # look at 30 days of CloudWatch metrics
|
|
50
|
+
costwatch scan --json > findings.json # machine-readable
|
|
51
|
+
costwatch policy # IAM policy for a read-only role
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
If a check isn't permitted or a region is disabled, it's reported at the end and the rest of
|
|
55
|
+
the scan carries on.
|
|
56
|
+
|
|
57
|
+
## Checks
|
|
58
|
+
|
|
59
|
+
| Check | Flags | Estimated cost |
|
|
60
|
+
|---|---|---|
|
|
61
|
+
| `unattached-ebs-volume` | EBS volumes not attached to an instance | Storage + provisioned IOPS/throughput |
|
|
62
|
+
| `old-ebs-snapshot` | EBS snapshots older than 90 days that no AMI uses | Upper bound (snapshots are incremental) |
|
|
63
|
+
| `gp2-volume` | Attached gp2 volumes | Saving from switching to gp3 at the same performance |
|
|
64
|
+
| `unused-elastic-ip` | Elastic IPs not associated with anything | $0.005/hour |
|
|
65
|
+
| `long-stopped-instance` | Instances stopped for 30+ days | Their attached EBS volumes |
|
|
66
|
+
| `idle-ec2-instance` | Running instances whose CPU never went above 5% in 14 days | Linux on-demand instance price |
|
|
67
|
+
| `unused-ami` | Your AMIs older than 90 days that no instance or launch template uses | Their snapshots (upper bound) |
|
|
68
|
+
| `idle-load-balancer` | ALB/NLB/GWLB/Classic with no registered targets | Hourly load balancer charge |
|
|
69
|
+
| `idle-nat-gateway` | NAT gateways that sent under 1 MiB in 14 days | $0.045/hour (plus any data) |
|
|
70
|
+
| `old-rds-snapshot` | Manual RDS/Aurora snapshots older than 90 days | Upper bound: allocated size × backup rate |
|
|
71
|
+
| `idle-rds-instance` | RDS instances with no connections in 14 days | Instance (× 2 for Multi-AZ) + storage |
|
|
72
|
+
| `retained-rds-backup` | Automated backups kept after their instance was deleted | Upper bound: allocated size × backup rate |
|
|
73
|
+
| `log-group-no-retention` | CloudWatch log groups over 1 GiB with no retention set | Current storage at $0.03/GB-month |
|
|
74
|
+
|
|
75
|
+
The idle checks use CloudWatch metrics and are deliberately conservative: an instance only
|
|
76
|
+
counts as idle if its CPU *never* went above 5%, and resources with no metric data are skipped.
|
|
77
|
+
|
|
78
|
+
Prices are us-east-1 on-demand approximations ([`pricing.py`](src/costwatch/pricing.py)).
|
|
79
|
+
They're for ranking waste, not for matching your bill to the cent; other regions are usually
|
|
80
|
+
within about 20%. Instance types missing from the price table are still reported, with a note
|
|
81
|
+
and a cost of $0.
|
|
82
|
+
|
|
83
|
+
## Owners (`--owners`)
|
|
84
|
+
|
|
85
|
+
For each finding, costwatch looks for an owner in this order:
|
|
86
|
+
|
|
87
|
+
1. **Tags:** `Owner`, `CreatedBy`/`created-by`/`created_by`, `Contact`, `Team` (any case).
|
|
88
|
+
2. **CloudTrail:** the identity that created the resource. SSO users show as
|
|
89
|
+
`user@example.com (PermissionSetRole)`, IAM users by name.
|
|
90
|
+
|
|
91
|
+
CloudTrail event history only goes back 90 days, so older resources show as `unknown` unless
|
|
92
|
+
they're tagged. Lookups are rate-limited by AWS (2 per second per region), so this is slower
|
|
93
|
+
on accounts with lots of findings.
|
|
94
|
+
|
|
95
|
+
## Development
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
git clone https://github.com/jonmelia/costwatch && cd costwatch
|
|
99
|
+
uv sync
|
|
100
|
+
uv run pytest # moto-based, no real AWS calls
|
|
101
|
+
uv run ruff check . && uv run ruff format .
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
`tests/e2e/` runs the real `costwatch` command against a [moto](https://github.com/getmoto/moto)
|
|
105
|
+
server. To try it by hand against a fake account full of waste:
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
uv run moto_server -p 5000
|
|
109
|
+
uv run python -m tests.e2e.seed --endpoint http://localhost:5000
|
|
110
|
+
export AWS_ACCESS_KEY_ID=testing AWS_SECRET_ACCESS_KEY=testing
|
|
111
|
+
AWS_ENDPOINT_URL=http://localhost:5000 \
|
|
112
|
+
uv run costwatch scan --region eu-west-1 --snapshot-age-days 0 --stopped-days 0 --min-cost 1
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Adding a check
|
|
116
|
+
|
|
117
|
+
1. Write a function `(session, region, config) -> list[Finding]` in
|
|
118
|
+
[`src/costwatch/checks/`](src/costwatch/checks/) and add it to `ALL_CHECKS`.
|
|
119
|
+
2. Add any new permissions to [`iam.py`](src/costwatch/iam.py).
|
|
120
|
+
3. If it should support `--owners`, add its create event(s) to `CREATE_EVENTS` in
|
|
121
|
+
[`owners.py`](src/costwatch/owners.py).
|
|
122
|
+
4. Test it with moto.
|
|
123
|
+
|
|
124
|
+
Contributions welcome: open an issue first for anything large.
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
[Apache 2.0](LICENSE)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "costwatch"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Find wasted AWS spend: idle and orphaned resources with estimated monthly cost"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
keywords = [
|
|
10
|
+
"aws",
|
|
11
|
+
"finops",
|
|
12
|
+
"cost",
|
|
13
|
+
"cloud",
|
|
14
|
+
"waste",
|
|
15
|
+
"devops",
|
|
16
|
+
]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Development Status :: 3 - Alpha",
|
|
19
|
+
"Environment :: Console",
|
|
20
|
+
"Intended Audience :: System Administrators",
|
|
21
|
+
"Intended Audience :: Developers",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Topic :: System :: Systems Administration",
|
|
24
|
+
]
|
|
25
|
+
dependencies = [
|
|
26
|
+
"boto3>=1.43.110",
|
|
27
|
+
"rich>=15.0.0",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[[project.authors]]
|
|
31
|
+
name = "jonnym"
|
|
32
|
+
email = "26524200+jonmelia@users.noreply.github.com"
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Repository = "https://github.com/jonmelia/costwatch"
|
|
36
|
+
Issues = "https://github.com/jonmelia/costwatch/issues"
|
|
37
|
+
|
|
38
|
+
[project.scripts]
|
|
39
|
+
costwatch = "costwatch.cli:main"
|
|
40
|
+
|
|
41
|
+
[build-system]
|
|
42
|
+
requires = ["uv_build>=0.12.24,<0.13.0"]
|
|
43
|
+
build-backend = "uv_build"
|
|
44
|
+
|
|
45
|
+
[dependency-groups]
|
|
46
|
+
dev = [
|
|
47
|
+
"moto[ec2,elb,elbv2,rds,server,cloudwatch,logs,autoscaling]>=5.2.3",
|
|
48
|
+
"pytest>=9.1.1",
|
|
49
|
+
"ruff>=0.16.10",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
[tool.ruff]
|
|
53
|
+
line-length = 100
|
|
54
|
+
|
|
55
|
+
[tool.ruff.lint]
|
|
56
|
+
select = [
|
|
57
|
+
"E",
|
|
58
|
+
"F",
|
|
59
|
+
"I",
|
|
60
|
+
"UP",
|
|
61
|
+
"B",
|
|
62
|
+
"SIM",
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
[tool.pytest.ini_options]
|
|
66
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "costwatch"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Find wasted AWS spend: idle and orphaned resources with estimated monthly cost"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "jonnym", email = "26524200+jonmelia@users.noreply.github.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.12"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
keywords = ["aws", "finops", "cost", "cloud", "waste", "devops"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Environment :: Console",
|
|
16
|
+
"Intended Audience :: System Administrators",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Topic :: System :: Systems Administration",
|
|
20
|
+
]
|
|
21
|
+
dependencies = [
|
|
22
|
+
"boto3>=1.43.110",
|
|
23
|
+
"rich>=15.0.0",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[build-system]
|
|
27
|
+
requires = ["uv_build>=0.12.24,<0.13.0"]
|
|
28
|
+
build-backend = "uv_build"
|
|
29
|
+
|
|
30
|
+
[dependency-groups]
|
|
31
|
+
dev = [
|
|
32
|
+
"moto[ec2,elb,elbv2,rds,server,cloudwatch,logs,autoscaling]>=5.2.3",
|
|
33
|
+
"pytest>=9.1.1",
|
|
34
|
+
"ruff>=0.16.10",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Repository = "https://github.com/jonmelia/costwatch"
|
|
39
|
+
Issues = "https://github.com/jonmelia/costwatch/issues"
|
|
40
|
+
|
|
41
|
+
[project.scripts]
|
|
42
|
+
costwatch = "costwatch.cli:main"
|
|
43
|
+
|
|
44
|
+
[tool.ruff]
|
|
45
|
+
line-length = 100
|
|
46
|
+
|
|
47
|
+
[tool.ruff.lint]
|
|
48
|
+
select = ["E", "F", "I", "UP", "B", "SIM"]
|
|
49
|
+
|
|
50
|
+
[tool.pytest.ini_options]
|
|
51
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import threading
|
|
2
|
+
|
|
3
|
+
import boto3
|
|
4
|
+
from botocore.config import Config
|
|
5
|
+
from botocore.exceptions import BotoCoreError, ClientError
|
|
6
|
+
|
|
7
|
+
# boto3 sessions are not thread-safe when creating clients; the clients themselves are.
|
|
8
|
+
_client_lock = threading.Lock()
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def client(session: boto3.Session, service: str, region: str, config: Config | None = None):
|
|
12
|
+
with _client_lock:
|
|
13
|
+
return session.client(service, region_name=region, config=config)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def tag_dict(tags: list[dict] | None) -> dict[str, str]:
|
|
17
|
+
return {t["Key"]: t.get("Value", "") for t in tags or [] if "Key" in t}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def name_tag(tags: list[dict] | None) -> str | None:
|
|
21
|
+
return tag_dict(tags).get("Name")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def chunks(items: list, size: int):
|
|
25
|
+
for i in range(0, len(items), size):
|
|
26
|
+
yield items[i : i + size]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def short_error(e: ClientError | BotoCoreError, limit: int = 160) -> str:
|
|
30
|
+
"""One-line error: 'AccessDenied: User ... is not authorized ...'."""
|
|
31
|
+
if isinstance(e, ClientError):
|
|
32
|
+
err = e.response.get("Error", {})
|
|
33
|
+
code = err.get("Code") or str(e.response.get("ResponseMetadata", {}).get("HTTPStatusCode"))
|
|
34
|
+
text = f"{code}: {err.get('Message') or ''}".rstrip(": ")
|
|
35
|
+
else:
|
|
36
|
+
text = str(e)
|
|
37
|
+
text = " ".join(text.split())
|
|
38
|
+
return text if len(text) <= limit else text[: limit - 1] + "…"
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from costwatch.checks.ebs import gp2_volumes, old_snapshots, unattached_volumes
|
|
2
|
+
from costwatch.checks.ec2 import (
|
|
3
|
+
idle_instances,
|
|
4
|
+
long_stopped_instances,
|
|
5
|
+
unused_amis,
|
|
6
|
+
unused_elastic_ips,
|
|
7
|
+
)
|
|
8
|
+
from costwatch.checks.elb import idle_classic_load_balancers, idle_load_balancers
|
|
9
|
+
from costwatch.checks.logs import log_groups_without_retention
|
|
10
|
+
from costwatch.checks.network import idle_nat_gateways
|
|
11
|
+
from costwatch.checks.rds import (
|
|
12
|
+
idle_rds_instances,
|
|
13
|
+
old_rds_cluster_snapshots,
|
|
14
|
+
old_rds_snapshots,
|
|
15
|
+
retained_rds_backups,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
ALL_CHECKS = [
|
|
19
|
+
unattached_volumes,
|
|
20
|
+
old_snapshots,
|
|
21
|
+
gp2_volumes,
|
|
22
|
+
unused_elastic_ips,
|
|
23
|
+
long_stopped_instances,
|
|
24
|
+
idle_instances,
|
|
25
|
+
unused_amis,
|
|
26
|
+
idle_load_balancers,
|
|
27
|
+
idle_classic_load_balancers,
|
|
28
|
+
idle_nat_gateways,
|
|
29
|
+
old_rds_snapshots,
|
|
30
|
+
old_rds_cluster_snapshots,
|
|
31
|
+
idle_rds_instances,
|
|
32
|
+
retained_rds_backups,
|
|
33
|
+
log_groups_without_retention,
|
|
34
|
+
]
|