#!/usr/bin/env python
import boto

print 'Security group information:\n'

ec2 = boto.connect_ec2()
sgs = ec2.get_all_security_groups()

for sg in sgs:
    instances = sg.instances()
    print 'id: %s, name: %s, count: %s' % (sg.id, sg.name, len(instances))

    for inst in instances:
        tag_name = 'UNKNOWN'
        if inst.tags is not None and 'Name' in inst.tags:
            tag_name = inst.tags['Name']
        print '\tid: %s, name: %s' % (inst.id, tag_name)

#!/usr/bin/env python
import boto3

print 'Security group information:\n'

ec2 = boto3.resource('ec2')
sgs = ec2.security_groups.all()

for sg in sgs:
    tag_name = sg.group_name
    if sg.tags is not None:
        for tag in sg.tags:
            if tag['Key'] == 'Name' and tag['Value'] != '':
                tag_name = tag['Value']

    print 'id: %s, name: %s, vpc_id: %s' % (sg.id, tag_name, sg.vpc_id)
Update script to use boto3 instead of boto2