Category: automation

EC2 Key Pairs are used to configure an EC2 instance with SSH access and provide a convenient way to manage instances. However, when was the last time you performed an audit to make sure that the only key pairs in your account are given to active employees who have proper authorization to connect to instances?

We’ll also create 3 lists — one for storing all key pairs, one for used key pairs, and one for unused key pairs. Next, we make a call to get all the key pairs and filter for the key pair names: In order to find all the key pairs currently in use, we first list the EC2 instances and then inspect those instances for their key pair.

We delete the unused key pairs by iterating over the list of unused key pairs and calling the ec2.delete_key_pair() function: Find the whole Python script here.

Related Articles