You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让AWS CLI describe-spot-price-history仅返回指定时间范围结果?

问题:AWS EC2 Spot价格历史查询返回超出设定时间范围的记录

我的AWS CLI环境信息:

aws-cli/2.7.2 Python/3.9.11 Linux/5.4.72-microsoft-standard-WSL2 exe/x86_64.debian.11 prompt/off

执行的查询命令:

aws ec2 describe-spot-price-history --availability-zone eu-central-1b --instance-types p3.8xlarge p3.16xlarge --start-time 2023-12-26T09:00:00 --end-time 2023-12-26T10:00:00

按照帮助文档示例设置了开始和结束时间,但返回结果里包含了超出该范围的记录,比如2023-12-26T05:01:40+00:00甚至2023-12-25的记录,请问哪里操作有误?如何让输出仅包含设定时间范围内的记录?


解决方案

1. 明确指定UTC时区

AWS EC2的describe-spot-price-history接口默认使用UTC时间处理--start-time和--end-time参数。如果输入时间未指定时区,CLI会将其解析为本地时区时间再转换为UTC,导致实际过滤范围与预期不符。

解决方法:在时间字符串末尾加上Z表示UTC时间,修正后的命令:

aws ec2 describe-spot-price-history --availability-zone eu-central-1b --instance-types p3.8xlarge p3.16xlarge --start-time 2023-12-26T09:00:00Z --end-time 2023-12-26T10:00:00Z

2. 升级AWS CLI到最新版本

你使用的CLI版本(2.7.2)发布于2022年,存在已知的时间范围过滤bug。升级到最新稳定版可修复这类问题:

# Debian/Ubuntu系统升级命令
sudo apt update && sudo apt upgrade awscli
# 或使用pip升级
pip install --upgrade awscli

3. 本地强制过滤结果

如果上述方法无效,可通过--query参数结合JMESPath语法,在本地对返回结果二次过滤,确保仅保留指定时间范围内的记录:

aws ec2 describe-spot-price-history --availability-zone eu-central-1b --instance-types p3.8xlarge p3.16xlarge --start-time 2023-12-26T09:00:00Z --end-time 2023-12-26T10:00:00Z --query 'SpotPriceHistory[?Timestamp >= `2023-12-26T09:00:00Z` && Timestamp <= `2023-12-26T10:00:00Z`]'

内容的提问来源于stack exchange,提问作者Alexander Ites

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 07:15:16