Last active
October 11, 2024 16:44
-
-
Save dastergon/b4994c605f76d528d0c4 to your computer and use it in GitHub Desktop.
A basic boto3 based tool for retrieving information from 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
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[instance.id] = { | |
'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])) | |
print("------") |
@TacMechMonkey, is there anyway we can filter Instance Status for specific VPCID? resource does not have status option.
running_instances = ec2.instances.filter(
Filters=[{
'Name': 'instance-status.status',
'Values': ['impaired']
},
{
'Name': 'vpc-id',
'Values': [vpc]
}]
)
@nakkanar you're on the right track, you'll have to use the ec2 client:
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_instance_status
Yeah you can specify the region in the constructor, but just in case what
do you mean by services ? or what you wanted to say was instances.
…On Mon, May 17, 2021 at 3:11 PM code_.blooded ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Is there any way to list all the running services in a particular region
using boto3?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/b4994c605f76d528d0c4#gistcomment-3745658>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJRMQGMU4OQT2BQH2QSFOTTTOEPYPANCNFSM4HPTH6SQ>
.
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')
clientips=boto3.client('ec2')
paginator = client.get_paginator('describe_auto_scaling_groups')
page_iterator = paginator.paginate(
PaginationConfig = {
'PageSize': 100
}
)
def lambda_handler(event, context):
filtered_asgs = page_iterator.search(
'AutoScalingGroups[] | [?contains(Tags[?Key==`{}`].Value, `{}`)]'.format('type', 'node')
)
for asg in filtered_asgs:
print (asg['AutoScalingGroupName'])
response = client.describe_auto_scaling_groups(
AutoScalingGroupNames=[
asg['AutoScalingGroupName'],
],
)
instances=response["AutoScalingGroups"][0]["Instances"]
instanceids=[]
for i in instances:
instanceids.append(i["InstanceId"])
instaneips=[]
reservations = clientips.describe_instances(InstanceIds=[i['InstanceId']]).get("Reservations")
for reservation in reservations:
for instance in reservation['Instances']:
print(instance.get("PublicIpAddress"))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot!!