-
-
Save shencan/0de504263b50c19cbfcc355aeefc6481 to your computer and use it in GitHub Desktop.
Example using boto3 to list running EC2 instances
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
ec2 = boto3.resource('ec2') | |
def lambda_handler(event, context): | |
# create filter for instances in running state | |
filters = [ | |
{ | |
'Name': 'instance-state-name', | |
'Values': ['running'] | |
} | |
] | |
# filter the instances based on filters() above | |
instances = ec2.instances.filter(Filters=filters) | |
# instantiate empty array | |
RunningInstances = [] | |
for instance in instances: | |
# for each instance, append to array and print instance id | |
RunningInstances.append(instance.id) | |
print instance.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment