import time
import boto3
class VolumesSequential(object):
"""Finds total volume size for all EC2 instances"""
def __init__(self):
self.ec2 = boto3.resource('ec2')
def instance_volumes(self, instance):
"""
Finds total size of the EBS volumes attached
to an EC2 instance
"""
instance_total = 0
for volume in instance.volumes.all():
instance_total += volume.size
return instance_total
def total_size(self):
"""
Lists all EC2 instances in the default region
and sums result of instance_volumes
"""
print "Running sequentially"
instances = self.ec2.instances.all()
instances_total = 0
for instance in instances:
instances_total += self.instance_volumes(instance)
return instances_total
def lambda_handler(event, context):
volumes = VolumesSequential()
_start = time.time()
total = volumes.total_size()
print "Total volume size: %s GB" % total
print "Sequential execution time: %s seconds" % (time.time() - _start)