-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspot-pricing.py
52 lines (42 loc) · 1.72 KB
/
spot-pricing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import boto3, json
from datetime import datetime, timedelta
from influxdb import InfluxDBClient
ec2 = boto3.resource('ec2')
client = boto3.client(service_name='ec2')
start_time = datetime.utcnow() - timedelta(minutes=1)
instance_type = "m3.medium"
operating_system = 'Linux/UNIX'
availability_zone = "us-east-1a"
prices = client.describe_spot_price_history(InstanceTypes=[
instance_type
],
AvailabilityZone=availability_zone,
StartTime=start_time,
Filters=[
{
'Name': 'product-description',
'Values': [
operating_system,
]
},
],
)
# Connect to the DB
client = InfluxDBClient('localhost', 8086, 'root', 'root', 'spotprices')
measurement = "LINUX_SPOT_PRICES"
tags = {"instance_type": instance_type,
"availability_zone": availability_zone}
for point in prices['SpotPriceHistory']:
timestamp = point['Timestamp'].strftime('%Y-%m-%dT%H:%M:%SZ')
price = point['SpotPrice']
point_data = [
{
"measurement": measurement,
"tags": tags,
"time": timestamp,
"fields": {
"spot_price": price
}
}
]
client.write_points(point_data)