AWS WAF metrics

表題の件についてのメモ。

■ 環境

  • AWS WAF
  • CloudWatch
  • awscli 1.11.80

■ list-metrics

まずはどんなメトリクスがあるのかを調べる。

$ aws cloudwatch list-metrics --namespace WAF

`–namespace WAF`を入れないと、全メトリクスが取得できてしまうので絞る。自身の環境の場合は下記のようなレスポンスが得られた。

$ aws cloudwatch list-metrics --namespace WAF
{
    "Metrics": [
        {
            "Namespace": "WAF",
            "Dimensions": [
                {
                    "Name": "WebACL",
                    "Value": "withsintest"
                },
                {
                    "Name": "Region",
                    "Value": "ap-northeast-1"
                },
                {
                    "Name": "Rule",
                    "Value": "testrule1"
                }
            ],
            "MetricName": "BlockedRequests"
        },
        {
            "Namespace": "WAF",
            "Dimensions": [
                {
                    "Name": "WebACL",
                    "Value": "withsintest"
                },
                {
                    "Name": "Region",
                    "Value": "ap-northeast-1"
                },
                {
                    "Name": "Rule",
                    "Value": "ALL"
                }
            ],
            "MetricName": "BlockedRequests"
        }
    ]
}
$

■ get-metric-statistics

続いてこれらのメトリクスデータを取得する。

$ aws cloudwatch get-metric-statistics

オプションが多々必要なようである。

  • –namespace WAF
  • –metric-name BlockedRequests
  • –start-time 2017-04-24T15:00:00Z
  • –end-time 2017-04-25T15:00:00Z
  • –period 3600

これら必須のオプションだけで取得できるかと思いきや、下記エラーが出た。

An error occurred (InvalidParameterCombination) when calling the GetMetricStatistics operation: At least one of the parameters Statistics and ExtendedStatistics must be specified.
  • –statistics Sum

これを追加したところ、エラーは出なくなったがデータは何も取得できず。

{
    "Datapoints": [],
    "Label": "BlockedRequests"
}

上記のように空である。続いて書きオプションも追加。

  • –dimensions

これも指定する必要がありそうだ。”MetricName“が同じだから”Dimensions“で判断するしかないということか。と自身の中では納得し、結果下記のように実行。

$ aws cloudwatch get-metric-statistics \
--namespace WAF \
--metric-name BlockedRequests \
--start-time 2017-04-24T15:00:00Z \
--end-time 2017-04-205T15:00:00Z \
--period 3600 \
--statistics Sum \
--dimensions Name=WebACL,Value=withsintest Name=Region,Value=ap-northeast-1 Name=Rule,Value=ALL
{
    "Datapoints": [
        {
            "Timestamp": "2017-04-24T19:00:00Z",
            "Sum": 3.0,
            "Unit": "None"
        },
        {
            "Timestamp": "2017-04-25T02:00:00Z",
            "Sum": 4.0,
            "Unit": "None"
        },
        {
            "Timestamp": "2017-04-25T02:00:00Z",
            "Sum": 2.0,
            "Unit": "None"
        }
    ],
    "Label": "BlockedRequests"
}
$

`–period`で指定した1時間ごとのデータが取得できた。”0“件の場合には省略されるようだ。

以上。

■ 関連

waf or waf-regional