Skip to content

Instantly share code, notes, and snippets.

@alexandrerocco
Created November 29, 2021 23:27
Show Gist options
  • Save alexandrerocco/06664fc437b28757ec6fd6fa4da48d40 to your computer and use it in GitHub Desktop.
Save alexandrerocco/06664fc437b28757ec6fd6fa4da48d40 to your computer and use it in GitHub Desktop.
Lists unused AWS key pair and delete if needed
# List key pairs on the account
aws ec2 describe-key-pairs --query 'KeyPairs[*].KeyName' --output table
# Find instances that uses a key pair you found out above
aws ec2 describe-instances --filters Name=instance-state-name,Values=running Name=key-name,Values="KP-NAME" --query 'Reservations[*].Instances[*].InstanceId'
# If you get an empty response, you can opt to delete it with:
aws ec2 delete-key-pair --key-name KP-NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment