Utilizing Boto3 to Retrieve List of EC2 Instances

In this post I share a handy script that allows you to get list of all ec2 instances by instance id, instance type, launch date and then finally format that data into a dataframe using pandas.

#Import packages
import boto3
import pandas as pd

#Initiate empty list
empty_list = []

#Initiate ec2 client session
ec2_client = boto3.client('ec2')

#Use describe_instances method to get response, parse response, append to list
response = ec2client.describe_instances()
for reservation in response["Reservations"]:
for instance in reservation["Instances"]:

instance_id = instance['InstanceId']
instance_type = instance['InstanceType']
launch_date = str(instance['LaunchTime'])

empty_list.append([instance_id, instance_type, launch_date])

#Create dataframe from list
instance_dataframe = pd.DataFrame(empty_list, columns = ['instance_id', 'instance_type', 'launch_date'])

Thanks for checking out this post.