Skip to content

Instantly share code, notes, and snippets.

@mda590
Last active April 24, 2024 16:37
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 37 You must be signed in to fork a gist
  • Save mda590/679aba60ca03699d5b12a32314debdc0 to your computer and use it in GitHub Desktop.
Save mda590/679aba60ca03699d5b12a32314debdc0 to your computer and use it in GitHub Desktop.
Example using boto3 to list running EC2 instances
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
@murarisumit
Copy link

thanks, came to know about boto resources(boto3.resource) from here. Was looking something like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment