我正在尝试使用get_spot_price_history()
函数在Python中通过Boto获取当前现货价格。
conn = boto.connect_ec2(aws_key, aws_secret)
prices = conn.get_spot_price_history("m3.medium", '2017-04-20T21:14:45.000Z', '2017-04-20T21:20:45.000Z',"us-east-1")
它给出了一个错误
Traceback (most recent call last):
File "run.py", line 22, in <module>
prices = conn.get_spot_price_history("m3.medium", '2017-04-20T21:14:45.000Z', '2017-04-20T21:20:45.000Z',"us-east-1")
File "/usr/lib/python2.6/site-packages/boto/ec2/connection.py", line 1421, in get_spot_price_history
[('item', SpotPriceHistory)], verb='POST')
File "/usr/lib/python2.6/site-packages/boto/connection.py", line 1182, in get_list
raise self.ResponseError(response.status, response.reason, body)
boto.exception.EC2ResponseError: EC2ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidRequest</Code><Message>The request received was invalid.</Message></Error></Errors><RequestID>da79e2ba-7475-4133-98f3-3c6aab8a07c6</RequestID></Response>
我的目的是获取现货实例的当前价格。谁能告诉我我在做错什么或其他简单的方法/功能以获得现货的市场价值。我是亚马逊网络服务的新手。请帮助。
看来返回的结果是相反的顺序,因此最终价格首先退回。
使用boto3(如今建议使用(:
import boto3
client=boto3.client('ec2',region_name='us-east-1')
prices=client.describe_spot_price_history(InstanceTypes=['m3.medium'],MaxResults=1,ProductDescriptions=['Linux/UNIX (Amazon VPC)'],AvailabilityZone='us-east-1a')
print prices['SpotPriceHistory'][0]
返回:
{u'Timestamp': datetime.datetime(2017, 4, 24, 20, 2, 11, tzinfo=tzlocal()),
u'ProductDescription': 'Linux/UNIX (Amazon VPC)',
u'InstanceType': 'm3.medium',
u'SpotPrice': '0.700000',
u'AvailabilityZone': 'us-east-1a'}
请注意,这不能保证这是如果您在US-EAST-1A中启动了匹配的现场实例,这是您将支付的价格。这仅仅是该组合的最新报道价格。
- 按需清单价格:$ 0.067
- 返回的最新现货价格:$ 0.007(我认为结果为美分(
- 现场实例定价页面显示:$ 0.011
us-east-1
是区域名称, get_spot_price_history
接受(可选(可用性区域。对于区域us-east-1
,区域为us-east-1a, us-east-1b, us-east-1c, us-east-1d, us-east-1e
。
并将参数作为关键字参数传递,
prices = conn.get_spot_price_history(instance_type="m3.medium",
start_time="2017-04-20T21:14:45.000Z",
end_time="2017-04-20T21:20:45.000Z",
availability_zone="us-east-1a")
获取所有区域的信息:
import boto3
import datetime
client = boto3.client('ec2', region_name='us-west-2')
regions = [x["RegionName"] for x in client.describe_regions()["Regions"]]
INSTANCE = "p2.xlarge"
print("Instance: %s" % INSTANCE)
results = []
for region in regions:
client = boto3.client('ec2', region_name=region)
prices = client.describe_spot_price_history(
InstanceTypes=[INSTANCE],
ProductDescriptions=['Linux/UNIX', 'Linux/UNIX (Amazon VPC)'],
StartTime=(datetime.datetime.now() -
datetime.timedelta(hours=4)).isoformat(),
MaxResults=1
)
for price in prices["SpotPriceHistory"]:
results.append((price["AvailabilityZone"], price["SpotPrice"]))
for region, price in sorted(results, key=lambda x: x[1]):
print("Region: %s price: %s" % (region, price))