

# CloudWatch Logs Insights を使用したログのクエリ
<a name="sal-cw-querying-insights"></a>

CloudWatch Logs Insights では、サーバーアクセスログデータをインタラクティブに検索および分析できます。クエリは、ログが配信されるロググループに対して実行され、結果を数秒で返します。

**注記**  
Amazon S3 汎用バケットに配信されるログのクエリについては、「[Amazon Athena を使用したリクエストのアクセスログのクエリ](using-s3-access-logs-to-identify-requests.md#querying-s3-access-logs-for-requests)」を参照してください。SQL を使用した S3 Tables のミラーリングのクエリについては、「[S3 Tables でのアクセスログのクエリ](sal-cw-querying-s3tables.md)」を参照してください。

## 開始方法
<a name="sal-cw-insights-getting-started"></a>

1. CloudWatch コンソールの [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/) を開いてください。

1. ナビゲーションペインで、**[ログ]**、**[Log Insights]** の順に選択します。

1. ロググループセレクタからサーバーアクセスロググループを選択します。

1. クエリの時間範囲を選択します。

1. クエリを入力して **[クエリの実行]** を選択します。

## トラフィック分析
<a name="sal-cw-insights-traffic"></a>

次のクエリは、リクエストボリュームとトラフィックパターンの理解に役立ちます。

**Example 時間の経過に伴うリクエストボリューム**  

```
stats count(*) as requests by bin(5m) as interval
| sort interval asc
```

**Example オペレーションタイプ別のリクエストミックス**  

```
stats count(*) as cnt by operation
| sort cnt desc
```

**Example バケット別のトラフィック**  

```
stats count(*) as requests, sum(bytes_sent_size) as bytes_out by bucket_arn
| sort requests desc
```

## エラーのトラブルシューティング
<a name="sal-cw-insights-errors"></a>

次のクエリは、エラーの特定と診断に役立ちます。

**Example エラー率の内訳**  

```
filter http_status >= 400
| stats count(*) as error_count by http_status, error_code, operation
| sort error_count desc
```

**Example 403 Access Denied リクエスト**  

```
filter http_status = 403
| stats count(*) as denied_count by key_name, remote_ip, requester
| sort denied_count desc
```

**Example 404 Not Found リクエスト**  

```
filter http_status = 404
| stats count(*) as miss_count by key_name, error_code, remote_ip
| sort miss_count desc
```

**Example 503 SlowDown (スロットリング) イベント**  

```
filter http_status = 503
| stats count(*) as throttle_count by key_name, remote_ip
| sort throttle_count desc
```

## アクセスパターン
<a name="sal-cw-insights-access"></a>

次のクエリは、データにアクセスするユーザーとその方法の理解に役立ちます。

**Example ソース IP 別のトラフィック**  

```
stats count(*) as total_requests,
  sum(http_status >= 400) as errors,
  sum(bytes_sent_size) as bytes_transferred
  by remote_ip
| sort total_requests desc
```

**Example リクエスタ別のオペレーション**  

```
stats count(*) as requests by requester, operation
| sort requests desc
```

**Example 最もアクセスされたキー**  

```
filter operation like /REST\.(GET|PUT)\.OBJECT/
| stats count(*) as access_count, sum(bytes_sent_size) as bytes_out by key_name
| sort access_count desc
| limit 10
```

## レイテンシー分析
<a name="sal-cw-insights-latency"></a>

次のクエリは、遅いリクエストとレイテンシーの傾向の特定に役立ちます。

**Example オペレーションタイプ別のレイテンシー**  

```
filter operation like /REST\.(GET|PUT)\.OBJECT/
| stats avg(total_duration) as avg_ms,
  pct(total_duration, 50) as p50_ms,
  pct(total_duration, 95) as p95_ms,
  max(total_duration) as max_ms,
  count(*) as requests
  by operation
```

**Example p95 レイテンシーが最も遅いキー**  

```
filter operation like /REST\.GET\.OBJECT/
| stats avg(total_duration) as avg_ms,
  pct(total_duration, 95) as p95_ms,
  count(*) as requests
  by key_name
| sort p95_ms desc
| limit 10
```

**Example 時間の経過に伴うレイテンシー**  

```
filter operation like /REST\.(GET|PUT)\.OBJECT/
| stats avg(total_duration) as avg_ms, pct(total_duration, 95) as p95_ms by bin(5m) as interval
| sort interval asc
```

## 原価配分
<a name="sal-cw-insights-cost"></a>

次のクエリは、データ転送を理解し、コスト要因を特定するのに役立ちます。

**Example オペレーション別のデータ転送**  

```
filter operation like /REST\.(GET|PUT|DELETE)\.OBJECT/
| stats sum(bytes_sent_size) as total_bytes_out,
  sum(object_size) as total_object_bytes,
  count(*) as request_count
  by operation
| sort total_bytes_out desc
```

**Example 提供されている最大サイズのオブジェクト**  

```
filter operation like /REST\.GET\.OBJECT/ and bytes_sent_size > 0
| stats max(object_size) as max_size, avg(object_size) as avg_size, count(*) as reads by key_name
| sort max_size desc
| limit 10
```

## セキュリティ分析
<a name="sal-cw-insights-security"></a>

次のクエリは、疑わしいアクセスパターンの検出に役立ちます。

**Example エラー率の高い IP**  

```
stats count(*) as total,
  sum(http_status >= 400) as errors
  by remote_ip
| filter errors > 10
| sort errors desc
```

**Example キー別のアクセス試行の失敗**  

```
filter http_status >= 400
| stats count(*) as attempts by remote_ip, key_name, operation, http_status
| sort attempts desc
| limit 20
```

**Example TLS バージョンのディストリビューション**  

```
stats count(*) as cnt by tls_version
| sort cnt desc
```