如何通过CloudFormation为CloudWatch仪表板指标设置持久时间范围
在CloudFormation中为CloudWatch仪表板小部件设置持久时间范围
要让CloudWatch仪表板的小部件独立于全局时间选择器、保持自定义时间范围,只需在目标小部件的properties配置中添加两项关键设置:
- 开启持久时间范围:添加
persistTime: true,对应控制台的「Persist time range」复选框功能 - 指定自定义时间范围:通过
period或timeRange定义小部件专属的时间范围(比如固定显示最近5分钟的数据)
修改后的完整CloudFormation代码
Resources: MyDashboard: Type: AWS::CloudWatch::Dashboard Properties: DashboardName: MyDashboard DashboardBody: Fn::ToJsonString: widgets: - type: metric x: 0 y: 0 width: 4 height: 6 properties: view: gauge # 开启持久时间范围,不受仪表板全局时间影响 persistTime: true # 设置小部件固定显示最近5分钟的数据(300秒或"5m"均可) period: 300 yAxis: left: min: 0 max: 100 metrics: - - AWS/IoT - MyThingsCount - AggregationType - count title: # of Things stat: Average region: !Ref AWS::Region - type: metric x: 4 y: 0 width: 6 height: 6 properties: metrics: - - AWS/IoT - MyThingsCount - AggregationType - count title: Things over time stat: Average region: !Ref AWS::Region
关键说明
persistTime: true是核心开关,开启后小部件会忽略仪表板的全局时间设置,仅使用自身定义的时间范围period用于指定指标数据的采样周期,同时会控制小部件的显示时间范围(如5m对应最近5分钟的数据)- 若需更灵活的时间范围(比如特定时间段),可替换为
timeRange参数,示例:timeRange: {"start": "-1h", "end": "now"}
内容的提问来源于stack exchange,提问作者Gary Howe
相关产品推荐
相关产品推荐

