-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdelete-unused-ips.py
41 lines (36 loc) · 1.37 KB
/
delete-unused-ips.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
import boto3
# Add your AWS access key ID and secret access key here
aws_access_key_id = "AKIAZKCNCMWKKWPI7FW"
aws_secret_access_key = "Aek8INSHLfbVjnhg+J5vJai6i7pBxHTWLkHh0v4"
ec2 = boto3.resource(
"ec2",
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key
)
unused_ips = {}
for region in ec2.meta.client.describe_regions()["Regions"]:
region_name = region["RegionName"]
try:
ec2conn = boto3.client(
"ec2",
region_name=region_name,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key
)
addresses = ec2conn.describe_addresses(
Filters=[{"Name": "domain", "Values": ["vpc"]}]
)["Addresses"]
for address in addresses:
if (
"AssociationId" not in address
and address["AllocationId"] not in unused_ips
):
unused_ips[address["AllocationId"]] = region_name
ec2conn.release_address(AllocationId=address["AllocationId"])
print(
f"Deleted unused Elastic IP {address['PublicIp']} in region {region_name}"
)
except Exception as e:
print(f"No access to region {region_name}: {e}")
print(f"Found and deleted {len(unused_ips)} unused Elastic IPs across all regions:")
print(unused_ips)