Amazon CloudWatch Logs
User Guide

Example: Counting HTTP 404 Responses

Using CloudWatch Logs, you can monitor how many times your Apache servers return a HTTP 404 response, which is the response code for page not found. You might want to monitor this to understand how often your site visitors do not find the resource they are looking for. Assume that your log records are structured to include the following information for each log event (site visit):

  • Requestor IP Address

  • RFC 1413 Identity

  • Username

  • Timestamp

  • Request method with requested resource and protocol

  • HTTP response code to request

  • Bytes transferred in request

An example of this might look like the following: - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 404 2326

You could specify a rule which attempts to match events of that structure for HTTP 404 errors, as shown in the following example:

To create a metric filter using the CloudWatch console

  1. Open the CloudWatch console at

  2. If necessary, change the region. From the navigation bar, select the region that meets your needs. For more information, see Regions and Endpoints in the Amazon Web Services General Reference.

  3. In the navigation pane, click Logs.

  4. In the contents pane, select a log group, and then click Create Metric Filter.

  5. On the Define Logs Metric Filter screen, in the Filter Pattern field, enter [IP, UserInfo, User, Timestamp, RequestInfo, StatusCode=404, Bytes].

  6. To test your filter pattern, in the Select Log Data to Test list, select the log group you want to test the metric filter against, and then click Test Pattern.

  7. Under Results, CloudWatch Logs displays a message showing how many occurrences of the filter pattern were found in the log file.


    To see detailed results, click Show test results.

  8. Click Assign Metric, and then on the Create Metric Filter and Assign a Metric screen, in the Filter Name field, enter HTTP404Errors.

  9. Under Metric Details, in the Metric Namespace field, enter YourNameSpace.

  10. In the Metric Name field, enter ApacheNotFoundErrorCount, and then click Create Filter.

To create a metric filter using the AWS CLI

  • At a command prompt, remove the backslashes (\) and type this all on one line:

    % aws logs put-metric-filter \
      --log-group-name MyApp/access.log \
      --filter-name HTTP404Errors \
      --filter-pattern '[ip, id, user, timestamp, request, status_code=404, size]' \
      --metric-transformations \

In this example, literal characters such as the left and right square brackets, double quotes and character string 404 were used. The pattern needs to match with the entire log event message for the log event to be considered for monitoring.

You can verify the creation of the metric filter by using the describe-monitoring-policies command. You should see output that looks like this:

% aws logs describe-metric-filters --log-group-name MyApp/access.log

    "metricFilters": [
            "filterName": "HTTP404Errors", 
            "metricTransformations": [
                    "metricValue": "1", 
                    "metricNamespace": "YourNamespace", 
                    "metricName": "ApacheNotFoundErrorCount"
            "creationTime": 1399277571078, 
            "filterPattern": "[ip, id, user, timestamp, request, status_code=404, size]"

Now you can post a few events manually:

% aws logs put-log-events \
--log-group-name MyApp/access.log --log-stream-name hostname \
--log-events \
timestamp=1394793518000,message=" - bob [10/Oct/2000:13:55:36 -0700] \"GET /apache_pb.gif HTTP/1.0\" 404 2326" \
timestamp=1394793528000,message=" - bob [10/Oct/2000:13:55:36 -0700] \"GET /apache_pb2.gif HTTP/1.0\" 200 2326"

Soon after putting these sample log events, you can retrieve the metric named in the CloudWatch console as ApacheNotFoundErrorCount.