0

I am using Python SDK boto3 in order to get all the security groups into the region but I am getting the wrong number. there is my code:

## Client connection
ec2 = boto3.client(
    'ec2',
    aws_access_key_id=aws_access_key,
    aws_secret_access_key=aws_secret_key,
    region_name = ec2_region_name
)


def lambda_handler(event, context):
    count = 0
    for sg in ec2.describe_security_groups():
        count = count + 1
    print(count)

The result is 2 when there are hundreds of security groups.

What am I doing wrong?

Robert
  • 10,403
  • 14
  • 67
  • 117

1 Answers1

1

Please check describe_security_groups documentation return value again.

You need to read the list from the return dictionary key ["SecurityGroups"]

 for sg in ec2.describe_security_groups()["SecurityGroups"]:
        count = count + 1
    print(count)
mootmoot
  • 12,845
  • 5
  • 47
  • 44
  • Ohh, was that? Thank you @mootmoot. Do you know how to get all the instances associated to the security group. With boto2 you can do ```sg.instances()``` and then compare with zero, in that case that security group is unused. But with boto3 i will like to do the same. – Robert Dec 14 '16 at 16:04