forked from osalkk/aws_ec2_unused_resources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunused.py
executable file
·234 lines (188 loc) · 6.93 KB
/
unused.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/python3
import boto3
region_list = ['eu-west-1', 'eu-central-1', 'us-east-1', 'us-west-1', 'us-west-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1', 'sa-east-1']
owner_id = 'your_account_id'
filename = 'report.html'
def send_report():
with open (filename, "r") as myfile:
data=myfile.read().replace('\n', '')
client = boto3.client('ses', region_name='eu-west-1') #Choose which region you want to use SES
response = client.send_email(
Source='[email protected]',
Destination={
'ToAddresses': ['[email protected]'
]
},
Message={
'Subject': {
'Data': 'Unused AWS EC2 Resources'
},
'Body': {
'Html': {
'Data': data
}
}
}
)
def append(text):
with open(filename, "a") as f:
f.write(text+'\n')
f.close()
def save_cost():
html = """\
<html>
<head>
<style>
h1 {
text-shadow: 0px 1px 1px #4d4d4d;
color: #222;
font: 40px 'LeagueGothicRegular';
}
table, th, td {
border: 1px solid black;
background-color: #f1f1c1;
table-layout: fixed;
width:100%;
}
caption, th, td {
padding: .2em .8em;
border: 1px solid #fff;
}
caption {
background: #dbb768;
font-weight: bold;
font-size: 1.1em;
}
th {
font-weight: bold;
background: #f3ce7d;
}
td {
background: #ffea97;
}</style>
</head>
<body>
"""
with open(filename, "w") as f:
f.write(html+'\n')
f.close()
for region in region_list:
append('<h1 style="color:red"><center>'+region+'</center></h1>')
#EIP
client = boto3.client('ec2', region_name=region)
response = client.describe_addresses()
eips=[]
for address in response['Addresses']:
if 'InstanceId' not in address:
eips.append(address['PublicIp'])
if len(eips) > 0:
append('<br><table><caption>Disassociated EIPS</caption><tr><th>Resource</th><th>Ip Address</th>')
for eip in eips:
append('<tr><td>EIP</td><td>'+eip+'</td></tr>')
append('</table>')
#Volumes
response=client.describe_volumes()
volumes = []
for volume in response['Volumes']:
if len(volume['Attachments']) == 0:
volume_dict = {}
volume_dict['VolumeId'] = volume['VolumeId']
volume_dict['VolumeType'] = volume['VolumeType']
volume_dict['VolumeSize'] = volume['Size']
volumes.append(volume_dict)
if len(volumes) > 0:
append('<br><table><caption>Unattached Volumes</caption><th>Resource</th><th>Volume ID</th><th>Volume Type</th><th>Volume Size</th>')
for vol in volumes:
append('<tr><td>Volume </td><td>'+vol['VolumeId']+'</td><td>'+vol['VolumeType']+'</td><td>'+str(vol['VolumeSize'])+'GB</td></tr>')
append('</table>')
#Snapshots
response = client.describe_snapshots(OwnerIds=[owner_id])
snapshots=[]
for snapshot in response['Snapshots']:
if 'ami' not in snapshot['Description']:
snapshots.append(snapshot['SnapshotId'])
if len(snapshots) > 0:
append('<br><table><caption>Unused Snaphosts</caption><th>Resource</th><th>Snapshot ID</th>')
for snap in snapshots:
append('<tr><td>Snapshot</td><td>'+snap+'</td></tr>')
append('</table>')
#Securit Groups
response = client.describe_security_groups()
all_sec_groups = []
for SecGrp in response['SecurityGroups']:
all_sec_groups.append(SecGrp['GroupName'])
sec_groups_in_use = []
response = client.describe_instances(
Filters=[
{
'Name': 'instance-state-name',
'Values': ['running', 'stopped']
}
])
for r in response['Reservations']:
for inst in r['Instances']:
if inst['SecurityGroups'][0]['GroupName'] not in sec_groups_in_use:
sec_groups_in_use.append(inst['SecurityGroups'][0]['GroupName'])
unused_sec_groups = []
for groups in all_sec_groups:
if groups not in sec_groups_in_use:
unused_sec_groups.append(groups)
if len(unused_sec_groups) > 0:
append('<br><table><caption>Unused Security Groups</caption><th>Resource</th><th>Security Group Name</th>')
for sg in unused_sec_groups:
append('<tr><td>Security Group</td><td>'+sg+'</td></tr>')
append('</table>')
#ELB
client = boto3.client('elb', region_name=region)
response = client.describe_load_balancers()
elbs=[]
for ELB in response['LoadBalancerDescriptions']:
if len(ELB['Instances']) == 0:
elbs.append(ELB['LoadBalancerName'])
if len(elbs) > 0:
append('<br><table><caption>Unused ELBs</caption><th>Resource</th><th>ELB Name</th>')
for elb in elbs:
append('<tr><td>ELB</td><td>'+elb+'</td></tr>')
append('</table>')
#Autoscaling
client = boto3.client('autoscaling', region_name=region)
response = client.describe_launch_configurations()
LC_list=[]
for LC in response['LaunchConfigurations']:
LC_name = LC['LaunchConfigurationName']
LC_list.append(LC_name)
response1 = client.describe_auto_scaling_groups()
for ASG in response1['AutoScalingGroups']:
if ASG['LaunchConfigurationName'] in LC_list:
LC_list.remove(ASG['LaunchConfigurationName'])
LCs=[]
for LC in LC_list:
LCs.append(LC)
if len(LCs) > 0:
append('<br><table><caption>Unused Launch Configurations</caption><th>Resource</th><th>LC Name</th>')
for lc in LCs:
append('<tr><td>LC</td><td>'+lc+'</td></tr>')
append('</table>')
response = client.describe_auto_scaling_groups()
ASGs=[]
for ASG in response['AutoScalingGroups']:
if ASG['DesiredCapacity'] == 0:
ASGs.append(ASG['AutoScalingGroupName'])
if len(ASGs) > 0:
append('<br><table><caption>Unused Auto Scaling Groups</caption><th>Resource</th><th>ASG Name</th>')
for asg in ASGs:
append('<tr><td>ASG</td><td>'+asg+'</td></tr>')
append('</table>')
html = """\
</body>
</html>
"""
with open(filename, "a") as f:
f.write(html+'\n')
f.close()
if __name__ == "__main__":
try:
save_cost()
send_report()
except Exception as err:
print(err)