I am using boto3, I need to list all elastic IPs, find the ones that are not associated with any instance and release them.
What I am doing is:
ec2 = boto3.resource('ec2')
for volume in ec2.volumes.all():
for instance in ec2.instances.all():
I got it working with this code:
def elastic_ips_cleanup(): """ Cleanup elastic IPs that are not being used """ client = boto3.client('ec2') addresses_dict = client.describe_addresses() for eip_dict in addresses_dict['Addresses']: if "InstanceId" not in eip_dict: print (eip_dict['PublicIp'] + " doesn't have any instances associated, releasing") client.release_address(AllocationId=eip_dict['AllocationId'])