AgentSkillsCN

aws-cli-cloudwatch

在使用AWS CloudWatch与CloudWatch Logs命令时——指标、警报、仪表板、日志组、日志流、指标过滤器、洞察查询、异常检测。在发布自定义指标或检索指标数据、创建并管理CloudWatch警报(简单与复合)、构建CloudWatch仪表板、创建并管理日志组与日志流、使用CloudWatch Logs Insights查询日志、设置指标过滤器、配置订阅过滤器、设置保留策略,或通过指标与警报监控ECS服务健康状况时使用此技能。

SKILL.md
--- frontmatter
name: aws-cli-cloudwatch
description: Use when working with AWS CloudWatch and CloudWatch Logs commands — metrics, alarms, dashboards, log groups, log streams, metric filters, insights queries, anomaly detection. Use this skill when publishing custom metrics or retrieving metric data, creating and managing CloudWatch alarms (simple and composite), building CloudWatch dashboards, creating and managing log groups and log streams, querying logs with CloudWatch Logs Insights, setting up metric filters, configuring subscription filters, setting retention policies, or monitoring ECS service health via metrics and alarms.

AWS CLI v2 — CloudWatch (Monitoring & Logs)

Overview

Complete reference for aws cloudwatch (metrics, alarms, dashboards) and aws logs (CloudWatch Logs) subcommands in AWS CLI v2. Covers metric publishing and retrieval, alarm configuration, composite alarms, dashboard management, log group/stream operations, metric filters, Logs Insights queries, and subscription filters.

Quick Reference — Common Workflows

Create an alarm on ECS service CPU

bash
aws cloudwatch put-metric-alarm --alarm-name ecs-high-cpu \
  --metric-name CPUUtilization --namespace AWS/ECS \
  --dimensions Name=ClusterName,Value=my-cluster Name=ServiceName,Value=my-service \
  --statistic Average --period 300 --threshold 80 \
  --comparison-operator GreaterThanThreshold --evaluation-periods 2 \
  --alarm-actions arn:aws:sns:us-east-1:123456789012:my-topic

Query logs with Insights

bash
aws logs start-query --log-group-name /ecs/my-service \
  --start-time $(date -d '1 hour ago' +%s) --end-time $(date +%s) \
  --query-string 'fields @timestamp, @message | filter @message like /ERROR/ | sort @timestamp desc | limit 20'
aws logs get-query-results --query-id <query-id>

Create a log group with retention

bash
aws logs create-log-group --log-group-name /ecs/my-service
aws logs put-retention-policy --log-group-name /ecs/my-service --retention-in-days 30

Get metric statistics

bash
aws cloudwatch get-metric-statistics --namespace AWS/ECS \
  --metric-name MemoryUtilization --dimensions Name=ClusterName,Value=my-cluster \
  --start-time 2026-02-11T00:00:00Z --end-time 2026-02-11T12:00:00Z \
  --period 3600 --statistics Average Maximum

Tail logs in real time

bash
aws logs tail /ecs/my-service --follow --since 10m

Covered Command Groups

GroupCommandsCLI Service
Metricsput-metric-data, get-metric-data, get-metric-statistics, list-metricsaws cloudwatch
Alarmsput-metric-alarm, put-composite-alarm, describe-alarms, delete-alarms, set-alarm-state, enable/disable-alarm-actionsaws cloudwatch
Dashboardsput-dashboard, get-dashboard, list-dashboards, delete-dashboardsaws cloudwatch
Anomaly Detectionput-anomaly-detector, describe-anomaly-detectors, delete-anomaly-detectoraws cloudwatch
Metric Streamsput-metric-stream, get-metric-stream, list-metric-streams, delete-metric-stream, start/stop-metric-streamsaws cloudwatch
Insight Rulesput-insight-rule, describe-insight-rules, delete-insight-rules, enable/disable-insight-rules, get-insight-rule-reportaws cloudwatch
Log Groupscreate, delete, describe, put-retention-policy, tag/untagaws logs
Log Streamscreate, delete, describeaws logs
Log Eventsput, get, filter, tailaws logs
Metric Filtersput, describe, deleteaws logs
Subscription Filtersput, describe, deleteaws logs
Logs Insightsstart-query, stop-query, get-query-results, describe-queriesaws logs

Command Reference

See references/index.md for the quick reference table and global options.

GroupFileCommands
Metricsmetrics.mdput-metric-data, get-metric-data, get-metric-statistics, list-metrics, get-metric-widget-image
Alarmsalarms.mdput-metric-alarm, put-composite-alarm, describe-alarms, describe-alarms-for-metric, describe-alarm-history, delete-alarms, set-alarm-state, enable-alarm-actions, disable-alarm-actions, describe-alarm-contributors
Dashboardsdashboards.mdput-dashboard, get-dashboard, list-dashboards, delete-dashboards
Anomaly Detectionanomaly-detection.mdput-anomaly-detector, describe-anomaly-detectors, delete-anomaly-detector
Insight Rulesinsight-rules.mdput-insight-rule, describe-insight-rules, get-insight-rule-report, delete-insight-rules, enable-insight-rules, disable-insight-rules
Metric Streamsmetric-streams.mdput-metric-stream, get-metric-stream, list-metric-streams, delete-metric-stream, start-metric-streams, stop-metric-streams
Tags (cloudwatch)tags-cloudwatch.mdtag-resource, untag-resource, list-tags-for-resource
Log Groupslog-groups.mdcreate-log-group, delete-log-group, describe-log-groups, put-retention-policy, delete-retention-policy, associate-kms-key, disassociate-kms-key
Log Streamslog-streams.mdcreate-log-stream, delete-log-stream, describe-log-streams
Log Eventslog-events.mdput-log-events, get-log-events, filter-log-events, tail, get-log-record, start-live-tail
Metric Filtersmetric-filters.mdput-metric-filter, describe-metric-filters, delete-metric-filter, test-metric-filter
Subscription Filterssubscription-filters.mdput-subscription-filter, describe-subscription-filters, delete-subscription-filter
Destinationsdestinations.mdput-destination, put-destination-policy, describe-destinations, delete-destination
Logs Insightslogs-insights.mdstart-query, get-query-results, stop-query, describe-queries
Export Tasksexport-tasks.mdcreate-export-task, describe-export-tasks, cancel-export-task
Resource Policiesresource-policies.mdput-resource-policy, describe-resource-policies, delete-resource-policy
Log Data Protectionlog-data-protection.mdput-data-protection-policy, get-data-protection-policy, delete-data-protection-policy
Query Definitionsquery-definitions.mdput-query-definition, describe-query-definitions, delete-query-definition
Log Anomaly Detectionlog-anomaly-detection.mdcreate-log-anomaly-detector, get-log-anomaly-detector, list-log-anomaly-detectors, update-log-anomaly-detector, delete-log-anomaly-detector, list-anomalies, update-anomaly
Tags (logs)tags-logs.mdtag-resource, untag-resource, list-tags-for-resource