Skip to content

Instantly share code, notes, and snippets.

@kenazk
Created May 25, 2020 04:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kenazk/60cd3993803f4e26c917aa7a12533795 to your computer and use it in GitHub Desktop.
Save kenazk/60cd3993803f4e26c917aa7a12533795 to your computer and use it in GitHub Desktop.
Relay: Delete Unused Key Pairs
#!/usr/bin/env python
import boto3
sess = boto3.Session(
aws_access_key_id='MYAWSACCESSKEYID',
aws_secret_access_key='MYSECRETACCESSKEY',
region_name='us-east-1'
)
# Creating lists for all, used, and unused key pairs
all_key_pairs = []
all_used_key_pairs = []
all_unused_key_pairs = []
# List all key pairs in the account
ec2 = sess.client('ec2')
all_key_pairs = list(map(lambda i: i['KeyName'], ec2.describe_key_pairs()['KeyPairs']))
# Each EC2 reservation returns a group of instances.
instance_groups = list(map(lambda i: i['Instances'], ec2.describe_instances()['Reservations']))
# Create a list of all used key pairs in the account based on the running instances
for group in instance_groups:
for i in group:
if i['KeyName'] not in all_used_key_pairs:
all_used_key_pairs.append(i['KeyName'])
# Now compare the two lists
for key in all_key_pairs:
if key not in all_used_key_pairs:
all_unused_key_pairs.append(key)
print('Keeping used key pairs: {}'.format(all_used_key_pairs))
# Delete all unused key pairs
print('Deleting unused key pairs:')
for key in all_unused_key_pairs:
print(key)
ec2.delete_key_pair(KeyName=key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment