PutMetricFilter - Amazon CloudWatch Logs
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).


Creates or updates a metric filter and associates it with the specified log group. With metric filters, you can configure rules to extract metric data from log events ingested through PutLogEvents.

The maximum number of metric filters that can be associated with a log group is 100.

When you create a metric filter, you can also optionally assign a unit and dimensions to the metric that is created.


Metrics extracted from log events are charged as custom metrics. To prevent unexpected high charges, do not specify high-cardinality fields such as IPAddress or requestID as dimensions. Each different value found for a dimension is treated as a separate metric and accrues charges as a separate custom metric.

CloudWatch Logs might disable a metric filter if it generates 1,000 different name/value pairs for your specified dimensions within one hour.

You can also set up a billing alarm to alert you if your charges are higher than expected. For more information, see Creating a Billing Alarm to Monitor Your Estimated Amazon Charges.

Request Syntax

{ "filterName": "string", "filterPattern": "string", "logGroupName": "string", "metricTransformations": [ { "defaultValue": number, "dimensions": { "string" : "string" }, "metricName": "string", "metricNamespace": "string", "metricValue": "string", "unit": "string" } ] }

Request Parameters

For information about the parameters that are common to all actions, see Common Parameters.

The request accepts the following data in JSON format.


A name for the metric filter.

Type: String

Length Constraints: Minimum length of 1. Maximum length of 512.

Pattern: [^:*]*

Required: Yes


A filter pattern for extracting metric data out of ingested log events.

Type: String

Length Constraints: Minimum length of 0. Maximum length of 1024.

Required: Yes


The name of the log group.

Type: String

Length Constraints: Minimum length of 1. Maximum length of 512.

Pattern: [\.\-_/#A-Za-z0-9]+

Required: Yes


A collection of information that defines how metric data gets emitted.

Type: Array of MetricTransformation objects

Array Members: Fixed number of 1 item.

Required: Yes

Response Elements

If the action is successful, the service sends back an HTTP 200 response with an empty HTTP body.


For information about the errors that are common to all actions, see Common Errors.


A parameter is specified incorrectly.

HTTP Status Code: 400


You have reached the maximum number of resources that can be created.

HTTP Status Code: 400


Multiple concurrent requests to update the same resource were in conflict.

HTTP Status Code: 400


The specified resource does not exist.

HTTP Status Code: 400


The service cannot complete the request.

HTTP Status Code: 500


To create or update a metric filter

The following example creates a metric filter for the specified log group.

Sample Request

POST / HTTP/1.1 Host: logs.<region>.<domain> X-Amz-Date: <DATE> Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=content-type;date;host;user-agent;x-amz-date;x-amz-target;x-amzn-requestid, Signature=<Signature> User-Agent: <UserAgentString> Accept: application/json Content-Type: application/x-amz-json-1.1 Content-Length: <PayloadSizeBytes> Connection: Keep-Alive X-Amz-Target: Logs_20140328.PutMetricFilter { "logGroupName": "my-log-group", "filterName": "my-metric-filter", "filterPattern": "[ip, identity, user_id, timestamp, request, status_code, size]", "metricTransformations": [ { "defaultValue": "0", "metricValue": "$size", "metricNamespace": "MyApp", "metricName": "Volume", "dimensions": {"Request": "$request","UserId": "$user_id"}, "unit": "Count" } ] }

Sample Response

HTTP/1.1 200 OK x-amzn-RequestId: <RequestId> Content-Type: application/x-amz-json-1.1 Content-Length: <PayloadSizeBytes> Date: <Date>

See Also

For more information about using this API in one of the language-specific Amazon SDKs, see the following: