This documentation is for Version 1 of the Amazon CLI only. For documentation related to Version 2 of the Amazon CLI, see the Version 2 User Guide.
CloudWatch examples using Amazon CLI
The following code examples show you how to perform actions and implement common scenarios by using the Amazon Command Line Interface with CloudWatch.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use delete-alarms.
- Amazon CLI
 - 
             
                    
To delete an alarm
The following example uses the
delete-alarmscommand to delete the Amazon CloudWatch alarm named "myalarm":aws cloudwatch delete-alarms --alarm-namesmyalarmOutput:
This command returns to the prompt if successful.- 
                    
For API details, see DeleteAlarms
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use delete-anomaly-detector.
- Amazon CLI
 - 
             
                    
To delete a specified anomaly detection model
The following
delete-anomaly-detectorexample deletes an anomaly detector model in the specified account.aws cloudwatch delete-anomaly-detector \ --namespaceAWS/Logs\ --metric-nameIncomingBytes\ --statSampleCountThis command produces no output.
For more information, see Deleting an anomaly detection model
in the Amazon CloudWatch User Guide. - 
                    
For API details, see DeleteAnomalyDetector
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use delete-dashboards.
- Amazon CLI
 - 
             
                    
To delete specified dashboards
The following
delete-dashboardsexample deletes two dashboards namedDashboard-AandDashboard-Bin the specified account.aws cloudwatch delete-dashboards \ --dashboard-namesDashboard-ADashboard-BThis command produces no output.
For more information, see Amazon CloudWatch dashboards
in the Amazon CloudWatch User Guide. - 
                    
For API details, see DeleteDashboards
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use delete-insight-rules.
- Amazon CLI
 - 
             
                    
To delete specified contributor insights rules
The following
delete-insight-rulesexample deletes two contributor insights rules namedRule-AandRule-Bin the specified account.aws cloudwatch delete-insight-rules \ --rule-namesRule-ARule-BOutput:
{ "Failures": [] }For more information, see Use Contributor Insights to analyze high-cardinality data
in the Amazon CloudWatch User Guide. - 
                    
For API details, see DeleteInsightRules
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use delete-metric-stream.
- Amazon CLI
 - 
             
                    
To delete a specified metric stream
The following
delete-metric-streamexample deletes the metric stream namedQuickPartial-gSCKvOin the specified account.aws cloudwatch delete-metric-stream \ --nameQuickPartial-gSCKvOThis command produces no output.
For more information, see Use metric streams
in the Amazon CloudWatch User Guide. - 
                    
For API details, see DeleteMetricStream
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use describe-alarm-history.
- Amazon CLI
 - 
             
                    
To retrieve history for an alarm
The following example uses the
describe-alarm-historycommand to retrieve history for the Amazon CloudWatch alarm named "myalarm":aws cloudwatch describe-alarm-history --alarm-name"myalarm"--history-item-typeStateUpdateOutput:
{ "AlarmHistoryItems": [ { "Timestamp": "2014-04-09T18:59:06.442Z", "HistoryItemType": "StateUpdate", "AlarmName": "myalarm", "HistoryData": "{\"version\":\"1.0\",\"oldState\":{\"stateValue\":\"ALARM\",\"stateReason\":\"testing purposes\"},\"newState\":{\"stateValue\":\"OK\",\"stateReason\":\"Threshold Crossed: 2 datapoints were not greater than the threshold (70.0). The most recent datapoints: [38.958, 40.292].\",\"stateReasonData\":{\"version\":\"1.0\",\"queryDate\":\"2014-04-09T18:59:06.419+0000\",\"startDate\":\"2014-04-09T18:44:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[38.958,40.292],\"threshold\":70.0}}}", "HistorySummary": "Alarm updated from ALARM to OK" }, { "Timestamp": "2014-04-09T18:59:05.805Z", "HistoryItemType": "StateUpdate", "AlarmName": "myalarm", "HistoryData": "{\"version\":\"1.0\",\"oldState\":{\"stateValue\":\"OK\",\"stateReason\":\"Threshold Crossed: 2 datapoints were not greater than the threshold (70.0). The most recent datapoints: [38.839999999999996, 39.714].\",\"stateReasonData\":{\"version\":\"1.0\",\"queryDate\":\"2014-03-11T22:45:41.569+0000\",\"startDate\":\"2014-03-11T22:30:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[38.839999999999996,39.714],\"threshold\":70.0}},\"newState\":{\"stateValue\":\"ALARM\",\"stateReason\":\"testing purposes\"}}", "HistorySummary": "Alarm updated from OK to ALARM" } ] }- 
                    
For API details, see DescribeAlarmHistory
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use describe-alarms-for-metric.
- Amazon CLI
 - 
             
                    
To display information about alarms associated with a metric
The following example uses the
describe-alarms-for-metriccommand to display information about any alarms associated with the Amazon EC2 CPUUtilization metric and the instance with the ID i-0c986c72.:aws cloudwatch describe-alarms-for-metric --metric-nameCPUUtilization--namespaceAWS/EC2--dimensionsName=InstanceId,Value=i-0c986c72Output:
{ "MetricAlarms": [ { "EvaluationPeriods": 10, "AlarmArn": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:myHighCpuAlarm2", "StateUpdatedTimestamp": "2013-10-30T03:03:51.479Z", "AlarmConfigurationUpdatedTimestamp": "2013-10-30T03:03:50.865Z", "ComparisonOperator": "GreaterThanOrEqualToThreshold", "AlarmActions": [ "arn:aws:sns:us-east-1:111122223333:NotifyMe" ], "Namespace": "AWS/EC2", "AlarmDescription": "CPU usage exceeds 70 percent", "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2013-10-30T03:03:51.479+0000\",\"startDate\":\"2013-10-30T02:08:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[40.698,39.612,42.432,39.796,38.816,42.28,42.854,40.088,40.760000000000005,41.316],\"threshold\":70.0}", "Period": 300, "StateValue": "OK", "Threshold": 70.0, "AlarmName": "myHighCpuAlarm2", "Dimensions": [ { "Name": "InstanceId", "Value": "i-0c986c72" } ], "Statistic": "Average", "StateReason": "Threshold Crossed: 10 datapoints were not greater than or equal to the threshold (70.0). The most recent datapoints: [40.760000000000005, 41.316].", "InsufficientDataActions": [], "OKActions": [], "ActionsEnabled": true, "MetricName": "CPUUtilization" }, { "EvaluationPeriods": 2, "AlarmArn": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:myHighCpuAlarm", "StateUpdatedTimestamp": "2014-04-09T18:59:06.442Z", "AlarmConfigurationUpdatedTimestamp": "2014-04-09T22:26:05.958Z", "ComparisonOperator": "GreaterThanThreshold", "AlarmActions": [ "arn:aws:sns:us-east-1:111122223333:HighCPUAlarm" ], "Namespace": "AWS/EC2", "AlarmDescription": "CPU usage exceeds 70 percent", "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2014-04-09T18:59:06.419+0000\",\"startDate\":\"2014-04-09T18:44:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[38.958,40.292],\"threshold\":70.0}", "Period": 300, "StateValue": "OK", "Threshold": 70.0, "AlarmName": "myHighCpuAlarm", "Dimensions": [ { "Name": "InstanceId", "Value": "i-0c986c72" } ], "Statistic": "Average", "StateReason": "Threshold Crossed: 2 datapoints were not greater than the threshold (70.0). The most recent datapoints: [38.958, 40.292].", "InsufficientDataActions": [], "OKActions": [], "ActionsEnabled": false, "MetricName": "CPUUtilization" } ] }- 
                    
For API details, see DescribeAlarmsForMetric
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use describe-alarms.
- Amazon CLI
 - 
             
                    
To list information about an alarm
The following example uses the
describe-alarmscommand to provide information about the alarm named "myalarm":aws cloudwatch describe-alarms --alarm-names"myalarm"Output:
{ "MetricAlarms": [ { "EvaluationPeriods": 2, "AlarmArn": "arn:aws:cloudwatch:us-east-1:123456789012:alarm:myalarm", "StateUpdatedTimestamp": "2014-04-09T18:59:06.442Z", "AlarmConfigurationUpdatedTimestamp": "2012-12-27T00:49:54.032Z", "ComparisonOperator": "GreaterThanThreshold", "AlarmActions": [ "arn:aws:sns:us-east-1:123456789012:myHighCpuAlarm" ], "Namespace": "AWS/EC2", "AlarmDescription": "CPU usage exceeds 70 percent", "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2014-04-09T18:59:06.419+0000\",\"startDate\":\"2014-04-09T18:44:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[38.958,40.292],\"threshold\":70.0}", "Period": 300, "StateValue": "OK", "Threshold": 70.0, "AlarmName": "myalarm", "Dimensions": [ { "Name": "InstanceId", "Value": "i-0c986c72" } ], "Statistic": "Average", "StateReason": "Threshold Crossed: 2 datapoints were not greater than the threshold (70.0). The most recent datapoints: [38.958, 40.292].", "InsufficientDataActions": [], "OKActions": [], "ActionsEnabled": true, "MetricName": "CPUUtilization" } ] }- 
                    
For API details, see DescribeAlarms
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use describe-anomaly-detectors.
- Amazon CLI
 - 
             
                    
To retrieve a list of anomaly detection models
The following
describe-anomaly-detectorsexample displays information about anomaly detector models that are associated with theAWS/Logsnamespace in the specified account.aws cloudwatch describe-anomaly-detectors \ --namespaceAWS/LogsOutput:
{ "AnomalyDetectors": [ { "Namespace": "AWS/Logs", "MetricName": "IncomingBytes", "Dimensions": [], "Stat": "SampleCount", "Configuration": { "ExcludedTimeRanges": [] }, "StateValue": "TRAINED", "SingleMetricAnomalyDetector": { "AccountId": "123456789012", "Namespace": "AWS/Logs", "MetricName": "IncomingBytes", "Dimensions": [], "Stat": "SampleCount" } }, { "Namespace": "AWS/Logs", "MetricName": "IncomingBytes", "Dimensions": [ { "Name": "LogGroupName", "Value": "demo" } ], "Stat": "Average", "Configuration": { "ExcludedTimeRanges": [] }, "StateValue": "PENDING_TRAINING", "SingleMetricAnomalyDetector": { "AccountId": "123456789012", "Namespace": "AWS/Logs", "MetricName": "IncomingBytes", "Dimensions": [ { "Name": "LogGroupName", "Value": "demo" } ], "Stat": "Average" } } ] }For more information, see Using CloudWatch anomaly detection
in the Amazon CloudWatch User Guide. - 
                    
For API details, see DescribeAnomalyDetectors
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use describe-insight-rules.
- Amazon CLI
 - 
             
                    
To retrieve a list of Contributor Insights rules
The following
describe-insight-rulesexample shows all the Contributor Insight rules in the specified account.aws cloudwatch describe-insight-rulesOutput:
{ "InsightRules": [ { "Name": "Rule-A", "State": "ENABLED", "Schema": "CloudWatchLogRule/1", "Definition": "{\n\t\"AggregateOn\": \"Count\",\n\t\"Contribution\": {\n\t\t\"Filters\": [],\n\t\t\"Keys\": [\n\t\t\t\"$.requestId\"\n\t\t]\n\t},\n\t\"LogFormat\": \"JSON\",\n\t\"Schema\": {\n\t\t\"Name\": \"CloudWatchLogRule\",\n\t\t\"Version\": 1\n\t},\n\t\"LogGroupARNs\": [\n\t\t\"arn:aws:logs:us-east-1:123456789012:log-group:demo\"\n\t]\n}", "ManagedRule": false }, { "Name": "Rule-B", "State": "ENABLED", "Schema": "CloudWatchLogRule/1", "Definition": "{\n\t\"AggregateOn\": \"Count\",\n\t\"Contribution\": {\n\t\t\"Filters\": [],\n\t\t\"Keys\": [\n\t\t\t\"$.requestId\"\n\t\t]\n\t},\n\t\"LogFormat\": \"JSON\",\n\t\"Schema\": {\n\t\t\"Name\": \"CloudWatchLogRule\",\n\t\t\"Version\": 1\n\t},\n\t\"LogGroupARNs\": [\n\t\t\"arn:aws:logs:us-east-1:123456789012:log-group:demo-1\"\n\t]\n}", "ManagedRule": false } ] }For more information, see Use Contributor Insights to analyze high-cardinality data
in the Amazon CloudWatch User Guide. - 
                    
For API details, see DescribeInsightRules
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use disable-alarm-actions.
- Amazon CLI
 - 
             
                    
To disable actions for an alarm
The following example uses the
disable-alarm-actionscommand to disable all actions for the alarm named myalarm.:aws cloudwatch disable-alarm-actions --alarm-namesmyalarmThis command returns to the prompt if successful.
- 
                    
For API details, see DisableAlarmActions
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use disable-insight-rules.
- Amazon CLI
 - 
             
                    
To disable specified contributor insight rules
The following
disable-insight-rulesexample disables two contributor insights rules namedRule-AandRule-Bin the specified account.aws cloudwatch disable-insight-rules \ --rule-namesRule-ARule-BOutput:
{ "Failures": [] }For more information, see Use Contributor Insights to analyze high-cardinality data
in the Amazon CloudWatch User Guide. - 
                    
For API details, see DisableInsightRules
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use enable-alarm-actions.
- Amazon CLI
 - 
             
                    
To enable all actions for an alarm
The following example uses the
enable-alarm-actionscommand to enable all actions for the alarm named myalarm.:aws cloudwatch enable-alarm-actions --alarm-namesmyalarmThis command returns to the prompt if successful.
- 
                    
For API details, see EnableAlarmActions
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use enable-insight-rules.
- Amazon CLI
 - 
             
                    
To enable specified contributor insight rules
The following
enable-insight-rulesexample enables two contributor insights rules namedRule-AandRule-Bin the specified account.aws cloudwatch enable-insight-rules \ --rule-namesRule-ARule-BOutput:
{ "Failures": [] }For more information, see Use Contributor Insights to analyze high-cardinality data
in the Amazon CloudWatch User Guide. - 
                    
For API details, see EnableInsightRules
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use get-dashboard.
- Amazon CLI
 - 
             
                    
To retrieve information about a Dashboard
The following
get-dashboardexample displays information about the dashboard namedDashboard-Ain the specified account.aws cloudwatch get-dashboard \ --dashboard-nameDashboard-AOutput:
{ "DashboardArn": "arn:aws:cloudwatch::123456789012:dashboard/Dashboard-A", "DashboardBody": "{\"widgets\":[{\"type\":\"metric\",\"x\":0,\"y\":0,\"width\":6,\"height\":6,\"properties\":{\"view\":\"timeSeries\",\"stacked\":false,\"metrics\":[[\"AWS/EC2\",\"NetworkIn\",\"InstanceId\",\"i-0131f062232ade043\"],[\".\",\"NetworkOut\",\".\",\".\"]],\"region\":\"us-east-1\"}}]}", "DashboardName": "Dashboard-A" }For more information, see Amazon CloudWatch dashboards
in the Amazon CloudWatch User Guide. - 
                    
For API details, see GetDashboard
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use get-insight-rule-report.
- Amazon CLI
 - 
             
                    
To retrieve the time series data collected by a Contributor Insights rule
The following
get-insight-rule-reportexample returns the time series data collected by a Contributor Insights rule.aws cloudwatch get-insight-rule-report \ --rule-nameRule-A\ --start-time2024-10-13T20:15:00Z\ --end-time2024-10-13T20:30:00Z\ --period300Output:
{ "KeyLabels": [ "PartitionKey" ], "AggregationStatistic": "Sum", "AggregateValue": 0.5, "ApproximateUniqueCount": 1, "Contributors": [ { "Keys": [ "RequestID" ], "ApproximateAggregateValue": 0.5, "Datapoints": [ { "Timestamp": "2024-10-13T21:00:00+00:00", "ApproximateValue": 0.5 } ] } ], "RuleAttributes": [] }For more information, see Use Contributor Insights to analyze high-cardinality data
in the Amazon CloudWatch User Guide. - 
                    
For API details, see GetInsightRuleReport
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use get-metric-data.
- Amazon CLI
 - 
             
                    
Example 1: To get the Average Total IOPS for the specified EC2 using math expression
The following
get-metric-dataexample retrieves CloudWatch metric values for the EC2 instance with InstanceIDi-abcdefusing metric math exprssion that combinesEBSReadOpsandEBSWriteOpsmetrics.aws cloudwatch get-metric-data \ --metric-data-queriesfile://file.json\ --start-time2024-09-29T22:10:00Z\ --end-time2024-09-29T22:15:00ZContents of
file.json:[ { "Id": "m3", "Expression": "(m1+m2)/300", "Label": "Avg Total IOPS" }, { "Id": "m1", "MetricStat": { "Metric": { "Namespace": "AWS/EC2", "MetricName": "EBSReadOps", "Dimensions": [ { "Name": "InstanceId", "Value": "i-abcdef" } ] }, "Period": 300, "Stat": "Sum", "Unit": "Count" }, "ReturnData": false }, { "Id": "m2", "MetricStat": { "Metric": { "Namespace": "AWS/EC2", "MetricName": "EBSWriteOps", "Dimensions": [ { "Name": "InstanceId", "Value": "i-abcdef" } ] }, "Period": 300, "Stat": "Sum", "Unit": "Count" }, "ReturnData": false } ]Output:
{ "MetricDataResults": [ { "Id": "m3", "Label": "Avg Total IOPS", "Timestamps": [ "2024-09-29T22:10:00+00:00" ], "Values": [ 96.85 ], "StatusCode": "Complete" } ], "Messages": [] }Example 2: To monitor the estimated Amazon charges using CloudWatch billing metrics
The following
get-metric-dataexample retrievesEstimatedChargesCloudWatch metric from Amazon/Billing namespace.aws cloudwatch get-metric-data \ --metric-data-queries '[{"Id":"m1","MetricStat":{"Metric":{"Namespace":"AWS/Billing","MetricName":"EstimatedCharges","Dimensions":[{"Name":"Currency","Value":"USD"}]},"Period":21600,"Stat":"Maximum"}}]' \ --start-time2024-09-26T12:00:00Z\ --end-time2024-09-26T18:00:00Z\ --regionus-east-1Output:
{ "MetricDataResults": [ { "Id": "m1", "Label": "EstimatedCharges", "Timestamps": [ "2024-09-26T12:00:00+00:00" ], "Values": [ 542.38 ], "StatusCode": "Complete" } ], "Messages": [] }For more information, see Using math expressions with CloudWatch metrics
in the Amazon CloudWatch User Guide. - 
                    
For API details, see GetMetricData
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use get-metric-statistics.
- Amazon CLI
 - 
             
                    
To get the CPU utilization per EC2 instance
The following example uses the
get-metric-statisticscommand to get the CPU utilization for an EC2 instance with the ID i-abcdef.aws cloudwatch get-metric-statistics --metric-nameCPUUtilization--start-time2014-04-08T23:18:00Z--end-time2014-04-09T23:18:00Z--period3600--namespaceAWS/EC2--statisticsMaximum--dimensionsName=InstanceId,Value=i-abcdefOutput:
{ "Datapoints": [ { "Timestamp": "2014-04-09T11:18:00Z", "Maximum": 44.79, "Unit": "Percent" }, { "Timestamp": "2014-04-09T20:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T19:18:00Z", "Maximum": 50.85, "Unit": "Percent" }, { "Timestamp": "2014-04-09T09:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T03:18:00Z", "Maximum": 76.84, "Unit": "Percent" }, { "Timestamp": "2014-04-09T21:18:00Z", "Maximum": 48.96, "Unit": "Percent" }, { "Timestamp": "2014-04-09T14:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T08:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T16:18:00Z", "Maximum": 45.55, "Unit": "Percent" }, { "Timestamp": "2014-04-09T06:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T13:18:00Z", "Maximum": 45.08, "Unit": "Percent" }, { "Timestamp": "2014-04-09T05:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T18:18:00Z", "Maximum": 46.88, "Unit": "Percent" }, { "Timestamp": "2014-04-09T17:18:00Z", "Maximum": 52.08, "Unit": "Percent" }, { "Timestamp": "2014-04-09T07:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T02:18:00Z", "Maximum": 51.23, "Unit": "Percent" }, { "Timestamp": "2014-04-09T12:18:00Z", "Maximum": 47.67, "Unit": "Percent" }, { "Timestamp": "2014-04-08T23:18:00Z", "Maximum": 46.88, "Unit": "Percent" }, { "Timestamp": "2014-04-09T10:18:00Z", "Maximum": 51.91, "Unit": "Percent" }, { "Timestamp": "2014-04-09T04:18:00Z", "Maximum": 47.13, "Unit": "Percent" }, { "Timestamp": "2014-04-09T15:18:00Z", "Maximum": 48.96, "Unit": "Percent" }, { "Timestamp": "2014-04-09T00:18:00Z", "Maximum": 48.16, "Unit": "Percent" }, { "Timestamp": "2014-04-09T01:18:00Z", "Maximum": 49.18, "Unit": "Percent" } ], "Label": "CPUUtilization" }Specifying multiple dimensions
The following example illustrates how to specify multiple dimensions. Each dimension is specified as a Name/Value pair, with a comma between the name and the value. Multiple dimensions are separated by a space. If a single metric includes multiple dimensions, you must specify a value for every defined dimension.
For more examples using the
get-metric-statisticscommand, see Get Statistics for a Metric in the Amazon CloudWatch Developer Guide.aws cloudwatch get-metric-statistics --metric-nameBuffers--namespaceMyNameSpace--dimensionsName=InstanceID,Value=i-abcdefName=InstanceType,Value=m1.small--start-time2016-10-15T04:00:00Z--end-time2016-10-19T07:00:00Z--statisticsAverage--period60- 
                    
For API details, see GetMetricStatistics
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use get-metric-stream.
- Amazon CLI
 - 
             
                    
To retrieve information about a metric stream
The following
get-metric-streamexample displays information about the metric stream namedQuickFull-GuaFbsin the specified account.aws cloudwatch get-metric-stream \ --nameQuickFull-GuaFbsOutput:
{ "Arn": "arn:aws:cloudwatch:us-east-1:123456789012:metric-stream/QuickFull-GuaFbs", "Name": "QuickFull-GuaFbs", "FirehoseArn": "arn:aws:firehose:us-east-1:123456789012:deliverystream/MetricStreams-QuickFull-GuaFbs-WnySbECG", "RoleArn": "arn:aws:iam::123456789012:role/service-role/MetricStreams-FirehosePutRecords-JN10W9B3", "State": "running", "CreationDate": "2024-10-11T18:48:59.187000+00:00", "LastUpdateDate": "2024-10-11T18:48:59.187000+00:00", "OutputFormat": "json", "IncludeLinkedAccountsMetrics": false }For more information, see Use metric streams
in the Amazon CloudWatch User Guide. - 
                    
For API details, see GetMetricStream
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use get-metric-widget-image.
- Amazon CLI
 - 
             
                    
To retrieve a snapshot graph of CPUUtilization
The following
get-metric-widget-imageexample retrieves snapshot graph for the metricCPUUtilizationof the EC2 instance with the IDi-abcdeand saves the retrieved image as a file named "image.png" on your local machine.aws cloudwatch get-metric-widget-image \ --metric-widget '{"metrics":[["AWS/EC2","CPUUtilization","InstanceId","i-abcde"]]}' \ --output-formatpng\ --outputtext|base64--decode>image.pngThis command produces no output.
- 
                    
For API details, see GetMetricWidgetImage
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use list-dashboards.
- Amazon CLI
 - 
             
                    
To retrieve a list of Dashboards
The following
list-dashboardsexample lists all the Dashboards in the specified account.aws cloudwatch list-dashboardsOutput:
{ "DashboardEntries": [ { "DashboardName": "Dashboard-A", "DashboardArn": "arn:aws:cloudwatch::123456789012:dashboard/Dashboard-A", "LastModified": "2024-10-11T18:40:11+00:00", "Size": 271 }, { "DashboardName": "Dashboard-B", "DashboardArn": "arn:aws:cloudwatch::123456789012:dashboard/Dashboard-B", "LastModified": "2024-10-11T18:44:41+00:00", "Size": 522 } ] }For more information, see Amazon CloudWatch dashboards
in the Amazon CloudWatch User Guide. - 
                    
For API details, see ListDashboards
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use list-metric-streams.
- Amazon CLI
 - 
             
                    
To retrieve a list of metric streams
The following
list-metric-streamsexample lists all the metric streams in the specified account.aws cloudwatch list-metric-streamsOutput:
{ "Entries": [ { "Arn": "arn:aws:cloudwatch:us-east-1:123456789012:metric-stream/QuickFull-GuaFbs", "CreationDate": "2024-10-11T18:48:59.187000+00:00", "LastUpdateDate": "2024-10-11T18:48:59.187000+00:00", "Name": "QuickFull-GuaFbs", "FirehoseArn": "arn:aws:firehose:us-east-1:123456789012:deliverystream/MetricStreams-QuickFull-GuaFbs-WnySbECG", "State": "running", "OutputFormat": "json" } ] }For more information, see Use metric streams
in the Amazon CloudWatch User Guide. - 
                    
For API details, see ListMetricStreams
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use list-metrics.
- Amazon CLI
 - 
             
                    
To list the metrics for Amazon SNS
The following
list-metricsexample displays the metrics for Amazon SNS.aws cloudwatch list-metrics \ --namespace"AWS/SNS"Output:
{ "Metrics": [ { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "NotifyMe" } ], "MetricName": "PublishSize" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "CFO" } ], "MetricName": "PublishSize" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "NotifyMe" } ], "MetricName": "NumberOfNotificationsFailed" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "NotifyMe" } ], "MetricName": "NumberOfNotificationsDelivered" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "NotifyMe" } ], "MetricName": "NumberOfMessagesPublished" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "CFO" } ], "MetricName": "NumberOfMessagesPublished" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "CFO" } ], "MetricName": "NumberOfNotificationsDelivered" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "CFO" } ], "MetricName": "NumberOfNotificationsFailed" } ] }- 
                    
For API details, see ListMetrics
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use list-tags-for-resource.
- Amazon CLI
 - 
             
                    
To list the tags associated with an existing alarm*
The following
list-tags-for-resourceexample lists all the tags associated with an alarm nameddemoin the specified account.aws cloudwatch list-tags-for-resource \ --resource-arnarn:aws:cloudwatch:us-east-1:123456789012:alarm:demoOutput:
{ "Tags": [ { "Key": "stack", "Value": "Production" }, { "Key": "team", "Value": "Devops" } ] }For more information, see Alarms and tagging
in the Amazon CloudWatch User Guide. - 
                    
For API details, see ListTagsForResource
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use put-anomaly-detector.
- Amazon CLI
 - 
             
                    
To create an anomaly detection model
The following
put-anomaly-detectorexample creates an anomaly detection model for a CloudWatch metric.aws cloudwatch put-anomaly-detector \ --namespaceAWS/Logs\ --metric-nameIncomingBytes\ --statSampleCountThis command produces no output.
For more information, see Using CloudWatch anomaly detection
in the Amazon CloudWatch User Guide. - 
                    
For API details, see PutAnomalyDetector
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use put-composite-alarm.
- Amazon CLI
 - 
             
                    
To create a composite cloudwatch alarm
The following
put-composite-alarmexample creates a composite alarm namedProdAlarmin the specified account.aws cloudwatch put-composite-alarm \ --alarm-nameProdAlarm\ --alarm-rule"ALARM(CPUUtilizationTooHigh) AND ALARM(MemUsageTooHigh)"\ --alarm-actionsarn:aws:sns:us-east-1:123456789012:demo\ --actions-enabledThis command produces no output.
For more information, see Create a composite alarm
in the Amazon CloudWatch User Guide. - 
                    
For API details, see PutCompositeAlarm
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use put-dashboard.
- Amazon CLI
 - 
             
                    
To create a dashboard
The following
put-dashboardexample creates a dashboard namedDashboard-Ain the specified account.aws cloudwatch put-dashboard \ --dashboard-nameDashboard-A\ --dashboard-body '{"widgets":[{"height":6,"width":6,"y":0,"x":0,"type":"metric","properties":{"view":"timeSeries","stacked":false,"metrics":[["Namespace","CPUUtilization","Environment","Prod","Type","App"]],"region":"us-east-1"}}]}'Output:
{ "DashboardValidationMessages": [] }For more information, see Creating a CloudWatch dashboard
in the Amazon CloudWatch User Guide. - 
                    
For API details, see PutDashboard
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use put-insight-rule.
- Amazon CLI
 - 
             
                    
To create a contributor insights rule
The following
put-insight-ruleexample creates a Contributor Insights rule namedVPCFlowLogsContributorInsightsin the specified account.aws cloudwatch put-insight-rule \ --rule-nameVPCFlowLogsContributorInsights\ --rule-definitionfile://insight-rule.json\ --rule-stateENABLEDContents of
insight-rule.json:{ "Schema": { "Name": "CloudWatchLogRule", "Version": 1 }, "AggregateOn": "Count", "Contribution": { "Filters": [], "Keys": [ "tcp-flag" ] }, "LogFormat": "CLF", "LogGroupNames": [ "/vpc/flowlogs/*" ], "Fields": { "23": "tcp-flag" } }This command produces no output.
For more information, see Create a Contributor Insights rule in CloudWatch
in the Amazon CloudWatch User Guide. - 
                    
For API details, see PutInsightRule
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use put-metric-alarm.
- Amazon CLI
 - 
             
                    
To send an Amazon Simple Notification Service email message when CPU utilization exceeds 70 percent
The following example uses the
put-metric-alarmcommand to send an Amazon Simple Notification Service email message when CPU utilization exceeds 70 percent:aws cloudwatch put-metric-alarm --alarm-namecpu-mon--alarm-description"Alarm when CPU exceeds 70 percent"--metric-nameCPUUtilization--namespaceAWS/EC2--statisticAverage--period300--threshold70--comparison-operatorGreaterThanThreshold--dimensions"Name=InstanceId,Value=i-12345678"--evaluation-periods2--alarm-actionsarn:aws:sns:us-east-1:111122223333:MyTopic--unitPercentThis command returns to the prompt if successful. If an alarm with the same name already exists, it will be overwritten by the new alarm.
To specify multiple dimensions
The following example illustrates how to specify multiple dimensions. Each dimension is specified as a Name/Value pair, with a comma between the name and the value. Multiple dimensions are separated by a space:
aws cloudwatch put-metric-alarm --alarm-name"Default_Test_Alarm3"--alarm-description"The default example alarm"--namespace"CW EXAMPLE METRICS"--metric-nameDefault_Test--statisticAverage--period60--evaluation-periods3--threshold50--comparison-operatorGreaterThanOrEqualToThreshold--dimensionsName=key1,Value=value1Name=key2,Value=value2- 
                    
For API details, see PutMetricAlarm
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use put-metric-data.
- Amazon CLI
 - 
             
                    
To publish a custom metric to Amazon CloudWatch
The following example uses the
put-metric-datacommand to publish a custom metric to Amazon CloudWatch:aws cloudwatch put-metric-data --namespace"Usage Metrics"--metric-datafile://metric.jsonThe values for the metric itself are stored in the JSON file,
metric.json.Here are the contents of that file:
[ { "MetricName": "New Posts", "Timestamp": "Wednesday, June 12, 2013 8:28:20 PM", "Value": 0.50, "Unit": "Count" } ]For more information, see Publishing Custom Metrics in the Amazon CloudWatch Developer Guide.
To specify multiple dimensions
The following example illustrates how to specify multiple dimensions. Each dimension is specified as a Name=Value pair. Multiple dimensions are separated by a comma.:
aws cloudwatch put-metric-data --metric-nameBuffers--namespaceMyNameSpace--unitBytes--value231434333--dimensionsInstanceID=1-23456789,InstanceType=m1.small- 
                    
For API details, see PutMetricData
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use put-metric-stream.
- Amazon CLI
 - 
             
                    
To create a metric stream
The following
put-metric-streamexample creates a metric stream namedQuickFull-GuaFbin the specified account.aws cloudwatch put-metric-stream \ --nameQuickFull-GuaFbs\ --firehose-arnarn:aws:firehose:us-east-1:123456789012:deliverystream/MetricStreams-QuickFull-GuaFbs-WnySbECG\ --role-arnarn:aws:iam::123456789012:role/service-role/MetricStreams-FirehosePutRecords-JN10W9B3\ --output-formatjson\ --no-include-linked-accounts-metricsOutput:
{ "Arn": "arn:aws:cloudwatch:us-east-1:123456789012:metric-stream/QuickFull-GuaFbs" }For more information, see Set up a metric stream
in the Amazon CloudWatch User Guide. - 
                    
For API details, see PutMetricStream
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use set-alarm-state.
- Amazon CLI
 - 
             
                    
To temporarily change the state of an alarm
The following example uses the
set-alarm-statecommand to temporarily change the state of an Amazon CloudWatch alarm named "myalarm" and set it to the ALARM state for testing purposes:aws cloudwatch set-alarm-state --alarm-name"myalarm"--state-valueALARM--state-reason"testing purposes"This command returns to the prompt if successful.
- 
                    
For API details, see SetAlarmState
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use start-metric-streams.
- Amazon CLI
 - 
             
                    
To start a specified metric stream
The following
start-metric-streamsexample starts the metric stream namedQuickFull-GuaFbsin the specified account.aws cloudwatch start-metric-streams \ --namesQuickFull-GuaFbsThis command produces no output.
For more information, see Use metric streams
in the Amazon CloudWatch User Guide. - 
                    
For API details, see StartMetricStreams
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use stop-metric-streams.
- Amazon CLI
 - 
             
                    
To stop a specified metric stream
The following
stop-metric-streamsexample stops the metric stream namedQuickFull-GuaFbsin the specified account.aws cloudwatch stop-metric-streams \ --namesQuickFull-GuaFbsThis command produces no output.
For more information, see Use metric streams
in the Amazon CloudWatch User Guide. - 
                    
For API details, see StopMetricStreams
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use tag-resource.
- Amazon CLI
 - 
             
                    
To add one or more tags to the specified resource
The following
tag-resourceexample adds 2 tags to the cloudwatch alarm nameddemoin the specified account.aws cloudwatch tag-resource \ --resource-arnarn:aws:cloudwatch:us-east-1:123456789012:alarm:demo\ --tagsKey=stack,Value=ProductionKey=team,Value=DevopsThis command produces no output.
For more information, see Tagging your Amazon CloudWatch resources
in the Amazon CloudWatch User Guide. - 
                    
For API details, see TagResource
in Amazon CLI Command Reference.  
 - 
                    
 
The following code example shows how to use untag-resource.
- Amazon CLI
 - 
             
                    
To remove one or more tags from the specified resource
The following
untag-resourceexample removes 2 tags from the cloudwatch alarm nameddemoin the specified account.aws cloudwatch untag-resource \ --resource-arnarn:aws:cloudwatch:us-east-1:123456789012:alarm:demo\ --tag-keysstackteamThis command produces no output.
For more information, see Tagging your Amazon CloudWatch resources
in the Amazon CloudWatch User Guide. - 
                    
For API details, see UntagResource
in Amazon CLI Command Reference.  
 -