Skip to content

Instantly share code, notes, and snippets.

Last active April 18, 2023 08:01
  • Star 74 You must be signed in to star a gist
  • Fork 30 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A basic boto3 based tool for retrieving information from running EC2 instances.
from collections import defaultdict
import boto3
A tool for retrieving basic information from the running EC2 instances.
# Connect to EC2
ec2 = boto3.resource('ec2')
# Get information for all running instances
running_instances = ec2.instances.filter(Filters=[{
'Name': 'instance-state-name',
'Values': ['running']}])
ec2info = defaultdict()
for instance in running_instances:
for tag in instance.tags:
if 'Name'in tag['Key']:
name = tag['Value']
# Add instance info to a dictionary
ec2info[] = {
'Name': name,
'Type': instance.instance_type,
'State': instance.state['Name'],
'Private IP': instance.private_ip_address,
'Public IP': instance.public_ip_address,
'Launch Time': instance.launch_time
attributes = ['Name', 'Type', 'State', 'Private IP', 'Public IP', 'Launch Time']
for instance_id, instance in ec2info.items():
for key in attributes:
print("{0}: {1}".format(key, instance[key]))
Copy link

sandygvs commented Sep 6, 2017

is there a way to filter out ASG specific to VPC or subnets via python boto? I tried with filter but no luck.

Any help is much appreciated

Copy link

ikekim commented Sep 15, 2018

I like this code because I was unable to find any other code example of using boto3 and printing the specific tag values.

However, looks like this code would not run properly if the running EC2s have multiple tag names that have "Name" in the tag names.

For example, the running instances has a tag name "Name" and value "web1" and has another tag name "Cluster Name" and value "US-West". I have not tested it yet, but I will circle back after I test it.

Copy link

@sandygvs / anyone else looking for an answer to the above (if I understand the question correctly) - if you're referring to filtering this code by VPC, subnets etc or limiting to a region you can add things like:

import boto3
from collections import defaultdict

region = 'ap-southeast-2'
az = 'ap-southeast-2a'
vpc = 'vpc-12345678'
subnet = 'subnet-12345678'
ec2 = boto3.resource('ec2', region)

running_instances = ec2.instances.filter(
        'Name': 'instance-state-name',
        'Values': ['running']
        'Name': 'availability-zone',
        'Values': [az]
        'Name': 'vpc-id',
        'Values': [vpc]
        'Name': 'subnet-id',
        'Values': [subnet]

If you mean ASG as in outputting security groups, you can add 'SG': instance.security_groups at the end of ec2info, and include it as an attribute.

Copy link

ghost commented May 24, 2019

As @ikekim said this will not work properly if instance.tags has more than one tag with a value which has 'Name` as a substring, e.g.

>>> 'Name' in 'NameOfEmployee'
>>> 'name' in 'dog'
>>> 'Name' in 'Names'

I used

if tag['Key'] == 'Name'

...because if you examine the instance.tags list of dicts there is only one 'Key' with a value of 'Name'

Copy link

Hola buen dia tengo una duda, como podria obtener el nombre de la instancia ?

Copy link

Thanks a lot!!

Copy link

@TacMechMonkey, is there anyway we can filter Instance Status for specific VPCID? resource does not have status option.

running_instances = ec2.instances.filter(
'Name': 'instance-status.status',
'Values': ['impaired']
'Name': 'vpc-id',
'Values': [vpc]

Copy link

Copy link

OtmanGX commented May 17, 2021 via email

Copy link

faisalnizam commented Dec 7, 2021

you can use something like this and put in lambda. This will give you all the asg based on a name/tag you provide , list all the instance id's and then you can get things like IP/key etc on the id's

import boto3

client = boto3.client('autoscaling')
paginator = client.get_paginator('describe_auto_scaling_groups')
page_iterator = paginator.paginate(
   PaginationConfig = {
      'PageSize': 100

def lambda_handler(event, context):

   filtered_asgs =
      'AutoScalingGroups[] | [?contains(Tags[?Key==`{}`].Value, `{}`)]'.format('type', 'node')

   for asg in filtered_asgs:
      print (asg['AutoScalingGroupName'])
      response = client.describe_auto_scaling_groups(
      for i in instances:
         reservations = clientips.describe_instances(InstanceIds=[i['InstanceId']]).get("Reservations")
         for reservation in reservations:
            for instance in reservation['Instances']:

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