Skip to content

Instantly share code, notes, and snippets.

@nirbhabbarat
Created February 12, 2018 17:37
Show Gist options
  • Save nirbhabbarat/bb78f9434c0a14f0aa9e616cbd2208a5 to your computer and use it in GitHub Desktop.
Save nirbhabbarat/bb78f9434c0a14f0aa9e616cbd2208a5 to your computer and use it in GitHub Desktop.
As EIP are billed if unused in AWS, simple script to delete all unused EIPs
import os
import boto3
key=os.environ['TF_VAR_aws_access_key']
secret=os.environ['TF_VAR_aws_secret_key']
client = boto3.client('ec2', region_name=os.environ['TF_VAR_region'], aws_access_key_id=key, aws_secret_access_key=secret)
def elastic_ips_cleanup(client_connection):
""" Cleanup elastic IPs that are not being used """
addresses_dict = client_connection.describe_addresses()
for eip_dict in addresses_dict['Addresses']:
print eip_dict
if "NetworkInterfaceId" not in eip_dict:
print (eip_dict['PublicIp'] +
" doesn't have any instances associated, releasing")
client_connection.release_address(AllocationId=eip_dict['AllocationId'])
elastic_ips_cleanup(client)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment