

# Amazon CloudWatch tutorials
<a name="CloudWatch-tutorials"></a>

 The following scenarios illustrate how you can use Amazon CloudWatch. In the first scenario, you use the CloudWatch console to create a billing alarm that tracks your AWS usage and lets you know when you have exceeded a certain spending threshold. In the second scenario, you use the AWS Command Line Interface (AWS CLI) to publish a single metric for a hypothetical application named *GetStarted*. 

**Scenarios**
+ [Monitor your estimated charges](gs_monitor_estimated_charges_with_cloudwatch.md)
+ [Publish metrics](PublishMetrics.md)

# Scenario: Monitor your estimated charges using CloudWatch
<a name="gs_monitor_estimated_charges_with_cloudwatch"></a>

In this scenario, you create an Amazon CloudWatch alarm to monitor your estimated charges. When you enable the monitoring of estimated charges for your AWS account, the estimated charges are calculated and sent several times daily to CloudWatch as metric data.

Billing metric data is stored in the US East (N. Virginia) Region and reflects worldwide charges. This data includes the estimated charges for every service in AWS that you use, as well as the estimated overall total of your AWS charges.

You can choose to receive alerts by email when charges have exceeded a certain threshold. These alerts are triggered by CloudWatch and messages are sent using Amazon Simple Notification Service (Amazon SNS).

**Note**  
For information about analyzing CloudWatch charges that you have already been billed for, see [Analyzing, optimizing, and reducing CloudWatch costs](cloudwatch_billing.md).

**Topics**
+ [

## Step 1: Enable billing alerts
](#gs_turning_on_billing_metrics)
+ [

## Step 2: Create a billing alarm
](#gs_creating_billing_alarm)
+ [

## Step 3: Check the alarm status
](#gs_checking_billing_alarm)
+ [

## Step 4: Edit a billing alarm
](#gs_editing_billing_alarm)
+ [

## Step 5: Delete a billing alarm
](#gs_deleting_billing_alarm)

## Step 1: Enable billing alerts
<a name="gs_turning_on_billing_metrics"></a>

Before you can create an alarm for your estimated charges, you must enable billing alerts, so that you can monitor your estimated AWS charges and create an alarm using billing metric data. After you enable billing alerts, you cannot disable data collection, but you can delete any billing alarms that you created.

After you enable billing alerts for the first time, it takes about 15 minutes before you can view billing data and set billing alarms.

**Requirements**
+ You must be signed in using root user credentials or as a user who has been given permission to view billing information.
+ For consolidated billing accounts, billing data for each linked account can be found by logging in as the paying account. You can view billing data for total estimated charges and estimated charges by service for each linked account, in addition to the consolidated account.
+ In a consolidated billing account, member linked account metrics are captured only if the payer account enables the **Receive Billing Alerts** preference. If you change which account is your management/payer account, you must enable the billing alerts in the new management/payer account.
+ The account must not be part of the Amazon Partner Network (APN) because billing metrics are not published to CloudWatch for APN accounts. For more information, see [AWS Partner Network](https://aws.amazon.com/partners/).

**To enable the monitoring of estimated charges**

1. Open the AWS Billing and Cost Management console at [https://console.aws.amazon.com/costmanagement/](https://console.aws.amazon.com/costmanagement/).

1. In the navigation pane, choose **Billing Preferences**.

1. By **Alert preferences** choose **Edit**.

1. Choose **Receive CloudWatch Billing Alerts**.

1. Choose **Save preferences**.

## Step 2: Create a billing alarm
<a name="gs_creating_billing_alarm"></a>

**Important**  
 Before you create a billing alarm, you must set your Region to US East (N. Virginia). Billing metric data is stored in this Region and represents worldwide charges. You also must enable billing alerts for your account or in the management/payer account (if you are using consolidated billing). For more information, see [Step 1: Enable billing alerts](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/gs_monitor_estimated_charges_with_cloudwatch.html#gs_turning_on_billing_metrics). 

 In this procedure, you create an alarm that sends a notification when your estimated charges for AWS exceed a defined threshold. 

**To create a billing alarm using the CloudWatch console**

1. Open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

1.  In the navigation pane, choose **Alarms**, and then choose **All alarms**. 

1.  Choose **Create alarm**. 

1.  Choose **Select metric**. In the **Browse** tab, choose **Billing**, and then choose **Total Estimated Charge**. 
**Note**  
 If you don't see the **Billing**/**Total Estimated Charge** metric, enable billing alerts, and change your Region to US East (N. Virginia). For more information, see [Enabling billing alerts](monitor_estimated_charges_with_cloudwatch.md#turning_on_billing_metrics). 

1.  Select the checkbox for the **EstimatedCharges** metric, and then choose **Select metric**. 

1. For **Statistic**, choose **Maximum**.

1. For **Period**, choose **6 hours**.

1.  For **Threshold type**, choose **Static**. 

1.  For **Whenever EstimatedCharges is . . .**, choose **Greater**. 

1.  For **than . . .**, define the value that you want to cause your alarm to trigger. For example, **200** USD. 

   The **EstimatedCharges** metric values are only in US dollars (USD), and the currency conversion is provided by Amazon Services LLC. For more information, see [ What is AWS Billing?](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/billing-what-is.html).

1. Choose **Additional Configuration** and do the following:
   + For **Datapoints to alarm**, specify **1 out of 1**.
   + For **Missing data treatment**, choose **Treat missing data as missing**.

1.  Choose **Next**. 

1.  Under **Notification**, ensure that **In alarm** is selected. Then specify an Amazon SNS topic to be notified when your alarm is in the `ALARM` state. The Amazon SNS topic can include your email address so that you recieve email when the billing amount crosses the threshold that you specified.

   You can select an existing Amazon SNS topic, create a new Amazon SNS topic, or use a topic ARN to notify other account. If you want your alarm to send multiple notifications for the same alarm state or for different alarm states, choose **Add notification**. 

1.  Choose **Next**. 

1.  Under **Name and description**, enter a name for your alarm. 

   1.  (Optional) Enter a description of your alarm. 

1. Choose **Next**.

1.  Under **Preview and create**, make sure that your configuration is correct, and then choose **Create alarm**. 

## Step 3: Check the alarm status
<a name="gs_checking_billing_alarm"></a>

Now, check the status of the billing alarm that you just created.

**To check the alarm status**

1. Open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

1. If necessary, change the Region to US East (N. Virginia). Billing metric data is stored in this Region and reflects worldwide charges.

1. In the navigation pane, choose **Alarms**, **All alarms**.

1. Find the row in the table for your new alarm. Until the subscription is confirmed, it is shown as "Pending confirmation". After the subscription is confirmed, refresh the console to show the updated status.

## Step 4: Edit a billing alarm
<a name="gs_editing_billing_alarm"></a>

For example, you may want to increase the amount of money you spend with AWS each month from \$1200 to \$1400. You can edit your existing billing alarm and increase the monetary amount that must be exceeded before the alarm is triggered.

**To edit a billing alarm**

1. Open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

1. If necessary, change the Region to US East (N. Virginia). Billing metric data is stored in this Region and reflects worldwide charges.

1. In the navigation pane, choose **Alarms**, **All alarms**.

1. Select the checkbox next to the alarm and choose **Actions**, **Edit**.

1. For **than...**, specify the new amount that must be exceeded to trigger the alarm and send an email notification.

1. Choose **Save Changes**.

## Step 5: Delete a billing alarm
<a name="gs_deleting_billing_alarm"></a>

If you no longer need your billing alarm, you can delete it.

**To delete a billing alarm**

1. Open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

1. If necessary, change the Region to US East (N. Virginia). Billing metric data is stored in this Region and reflects worldwide charges.

1. In the navigation pane, choose **Alarms**, **All alarms**.

1. Select the checkbox next to the alarm and choose **Actions**, **Delete**.

1. When prompted for confirmation, choose **Yes, Delete**.

# Scenario: Publish metrics to CloudWatch
<a name="PublishMetrics"></a>

In this scenario, you use the AWS Command Line Interface (AWS CLI) to publish a single metric for a hypothetical application named *GetStarted*. If you haven't already installed and configured the AWS CLI, see [Getting Set Up with the AWS Command Line Interface](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-set-up.html) in the *AWS Command Line Interface User Guide*.

**Topics**
+ [

## Step 1: Define the data configuration
](#define-data-domain)
+ [

## Step 2: Add metrics to CloudWatch
](#add-metrics-to-scenario)
+ [

## Step 3: Get statistics from CloudWatch
](#GetStatistics)
+ [

## Step 4: View graphs with the console
](#ViewGraphs)

## Step 1: Define the data configuration
<a name="define-data-domain"></a>

In this scenario, you publish data points that track the request latency for the application. Choose names for your metric and namespace that make sense to you. For this example, name the metric *RequestLatency* and place all of the data points into the *GetStarted* namespace. 

You publish several data points that collectively represent three hours of latency data. The raw data comprises 15 request latency readings distributed over three hours. Each reading is in milliseconds: 
+ Hour one: 87, 51, 125, 235
+ Hour two: 121, 113, 189, 65, 89
+ Hour three: 100, 47, 133, 98, 100, 328

You can publish data to CloudWatch as single data points or as an aggregated set of data points called a *statistic set*. You can aggregate metrics to a granularity as low as one minute. You can publish the aggregated data points to CloudWatch as a set of statistics with four predefined keys: `Sum`, `Minimum`, `Maximum`, and `SampleCount`.

You publish the data points from hour one as single data points. For the data from hours two and three, you aggregate the data points and publish a statistic set for each hour. The key values are shown in the following table.


| Hour | Raw data | Sum | Minimum | Maximum | SampleCount | 
| --- | --- | --- | --- | --- | --- | 
| `1` | `87` |  |  |  |  | 
| `1` | `51` |  |  |  |  | 
| `1` | `125` |  |  |  |  | 
| `1` | `235` |  |  |  |  | 
| `2` | `121, 113, 189, 65, 89` | `577` | `65` | `189` | `5` | 
| `3` | `100, 47, 133, 98, 100, 328` | `806` | `47` | `328` | `6` | 

## Step 2: Add metrics to CloudWatch
<a name="add-metrics-to-scenario"></a>

After you have defined your data configuration, you are ready to add data.

**To publish data points to CloudWatch**

1. At a command prompt, run the following [put-metric-data](https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/put-metric-data.html) commands to add data for the first hour. Replace the example timestamp with a timestamp that is two hours in the past, in Universal Coordinated Time (UTC).

   ```
   aws cloudwatch put-metric-data --metric-name RequestLatency --namespace GetStarted \
   --timestamp 2016-10-14T20:30:00Z --value 87 --unit Milliseconds
   aws cloudwatch put-metric-data --metric-name RequestLatency --namespace GetStarted \
   --timestamp 2016-10-14T20:30:00Z --value 51 --unit Milliseconds
   aws cloudwatch put-metric-data --metric-name RequestLatency --namespace GetStarted \
   --timestamp 2016-10-14T20:30:00Z --value 125 --unit Milliseconds
   aws cloudwatch put-metric-data --metric-name RequestLatency --namespace GetStarted \
   --timestamp 2016-10-14T20:30:00Z --value 235 --unit Milliseconds
   ```

1. Add data for the second hour, using a timestamp that is one hour later than the first hour.

   ```
   aws cloudwatch put-metric-data --metric-name RequestLatency --namespace GetStarted \
   --timestamp 2016-10-14T21:30:00Z --statistic-values Sum=577,Minimum=65,Maximum=189,SampleCount=5 --unit Milliseconds
   ```

1. Add data for the third hour, omitting the timestamp to default to the current time. 

   ```
   aws cloudwatch put-metric-data --metric-name RequestLatency --namespace GetStarted \
   --statistic-values Sum=806,Minimum=47,Maximum=328,SampleCount=6 --unit Milliseconds
   ```

## Step 3: Get statistics from CloudWatch
<a name="GetStatistics"></a>

Now that you have published metrics to CloudWatch, you can retrieve statistics based on those metrics using the [get-metric-statistics](https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/get-metric-statistics.html) command as follows. Be sure to specify `--start-time` and `--end-time` far enough in the past to cover the earliest timestamp that you published.

```
aws cloudwatch get-metric-statistics --namespace GetStarted --metric-name RequestLatency --statistics Average \
--start-time 2016-10-14T00:00:00Z --end-time 2016-10-15T00:00:00Z --period 60
```

The following is example output:

```
{
	"Datapoints": [],
	"Label": "Request:Latency"
}
```

## Step 4: View graphs with the console
<a name="ViewGraphs"></a>

After you have published metrics to CloudWatch, you can use the CloudWatch console to view statistical graphs.

**To view graphs of your statistics on the console**

1. Open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

1. In the **Navigation** pane, choose **Metrics**.

1. On the **All metrics** tab, in the search box, type **RequestLatency** and press Enter.

1. Select the check box for the **RequestLatency** metric. A graph of the metric data is displayed in the upper pane.

For more information, see [Graphing metrics](graph_metrics.md).