How to delete all security groups on Amazon ec2?

amazon-ec2

Solution

THis would need some basic scripting and AWS SDK. you can do this with pretty much all the SDK provided by AWS.

I would prefer `AWS-CLI` as i already have it installed and configured. This is what I would do:

- list all the SGs with describe-security-groups

- Install jq (the Json parser for BASH)

- Pull the SG IDs (check this for `jq` syntax)

- Once you have the SG IDs, run delete-security-group by usig a `for` loop.

This is fairly simple and straight forward way of doing wat you want to do. THis can be done by any of the AWS SDKs.

These are just a couple of commands which can be constructed into a Bash script, provided:

- You have aws-cli installed and configured

- you have `jq` installed on your system.

If you already have some other AWS SDK installed, then you are better off with that as java/python/ruby...etc all have their own inbuilt way of parsing JSON/HASH/DataStructure.

Hope this helps.

Problem

I've created new EC2 spot requests over the last weeks. A new security group was created for every request. When the spot requests were deleted the security groups were not deleted. I've hit the 100 groups limit and want to delete them. The EC2 interface apparently allows only one deletion at a time, which means I would have to make 300 clicks to delete these groups. Or is there a better way to delete multiple security groups with few clicks or lines of code?

Original source