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