Looking for a script to remove a security group from all EC2 instances.

I made a mistake and have 1 security group across 365 instances, which I’m not willing to manually remove. Is there a Python script out there, maybe using BOTO3 that will cycle through all instances, find the security group, and remove it? I’ve been looking through this documentation, but haven’t been able to figure out how to apply it to a script yet (I’m a python noob)

.

.

Edit Thanks to /u/thisisthetechie for the [solution found here](comments : aws
/8d94uk/looking_for_a_script_to_remove_a_security_group/dxn3ulx/)

AWS Tools for Windows PowerShell worked perfectly after initial set up and was way better than trying to figure out the Python route. Cheers.


Related discussions:

do you have a list of all the instances affected? if so why not loop through them with the “delete-security-group” command and then read each line from the file and provide the instance ID?

As an aside, Terraform would make this trivial. Probably worth looking at given the size of your estate.

Or CloudFormation.

For your problem see the other comment. You could use the AWS CLI instead of Python, with some simple bash probably.

Well the environment was not built out using Terraform, however I have used it before. What would be the best way to try using that?

I don’t have that list, but I can try to somehow export it to a CSV. I really wish that was a default option in the AWS Console.

You can import resources into terraform, so could import all your existing ones but this would need some scripting (looping), certainly if you have one security group per instance (I may have misunderstood this).

I can’t be much more help as I’m on my phone on the beach enjoying the last few days of a very much needed holiday :smiley:

Edit: probably worth just scripting this in AWS CLI in bash or whatever you know how to use. Going forward though, definitely look at IaC (infrastructure as code).

Do you need to use Python? The AWS Cli tools on Linux and AWS .NET SDK for Powershell are more than adequate options for making your life a lot easier.

No, I will take any suggestions!

Are you a Linux, Mac or Windows type ‘o’ tech?

Windows with some mac and Linux

Okay, so this is freakin messy and falls over (rightly so) if the instances only security group is your Nasty… I recommend trying it in a sandbox.

$NastyGroup = “sg-e4d17383”
ForEach ( $Instance in $( Get-EC2Instance | Select-Object -ExpandProperty Instances ) ) {
If ( $NastyGroup -in $( $Instance | Select-Object -ExpandProperty SecurityGroups ).GroupId ) {
$Groups = { $( $Instance | Select-Object -ExpandProperty SecurityGroups ).GroupId }.Invoke()
$Groups.Remove( $NastyGroup )
Edit-EC2InstanceAttribute -InstanceId $Instance.InstanceId -Group $Groups
}
}

It doesn’t look that messy to me. Looks like exactly what I need and just didn’t know how to write that myself!

Currently I’m just having an issue with Edit-EC2InstnaceAttribute. It says that I am not authorized to perform that operation even though I am a full admin in IAM.

Once I investigate that I will get back to you.

Update Worked like a charm. What are you talking about freaking messy? You are a hero and a scholar.

Not to seem like an ass, but if you couldn’t write that yourself based on the API docs, you really need to spend some time brushing up on your automation and scripting or you’re going to find yourself out of a job very quickly.