Nick
Nick

Reputation: 23

Boto3 Finding unused Security Groups

I am trying to understand more on Boto3 scripting.

I want to search for unused security groups within several VPC's which are all in the same region

I am trying to get the python script here to work: boto3 searching unused security groups

So my list-unused-sq.py is shown below

import boto3

ec2 = boto3.resource('ec2')

sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())

all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs

print 'Total SGs:', len(all_sgs)
print 'SGS attached to instances:', len(all_inst_sgs)
print 'Orphaned SGs:', len(unused_sgs)
print 'Unattached SG names:', unused_sgs

When i run the script i get the following error

./list-unused-sq.py: line 1: import: command not found
./list-unused-sq.py: line 3: syntax error near unexpected token `('
./list-unused-sq.py: line 3: `ec2 = boto3.resource('ec2') #You have to change this line based on how you pass AWS credentials and AWS config'

Is someone able to point out where i have gone wrong and what i need to do to correct it?

Thanks Nick

Upvotes: 1

Views: 1800

Answers (1)

SpKel
SpKel

Reputation: 552

Look at your first error line:

./list-unused-sq.py: line 1: import: command not found    

Seems like your problem is not related with boto3 but in your script not recognizing your local python. More info about your problem and how to solve it

Upvotes: 1

Related Questions