We are working on a project to optimize EC2 costs for few deep learning EC2 instances.

Getting the price data for the EC2 spots is a key data point of the analysis. The below script does the job pretty well.

import sys
import boto3
import json
import pandas as pd
import math

client = boto3.client('ec2')
response = client.describe_spot_price_history()

df = pd.DataFrame(response['SpotPriceHistory'])
df = df[df.apply(lambda x: not(math.isnan(float(x['SpotPrice']))), axis=1)]
df['Monthly'] = df['SpotPrice'].astype(float) * 31 * 24
df.head()