[Solved] Count Running and Stopped Ec2 Instances with AWS Lambda
Here’s some code that retrieves a list of instances and count the number of stopped and running instances: import boto3 def lambda_handler(event, context): ec2_resource = boto3.resource(‘ec2’) instances = [instance.state[“Name”] for instance in ec2_resource.instances.all()] print(‘Running: ‘, instances.count(‘running’)) print(‘Stopped: ‘, instances.count(‘stopped’)) The call to ec2_resource.instances.all() retrieves a list of all instances, and there is a state attribute … Read more