LoginSignup
1

More than 5 years have passed since last update.

awscliでAWS WAFのCloudWatchのメトリクスを取得する

Posted at

概要

AWS WAFのメトリクスを、CloudWatch以外の監視システムにデータ連携する場合に、
awscliを使用してデータ取得を行ったときにちょっとはまりましたのでメモしておきます。

regionはus-east-1固定

AWS WAFがCloudFrontに装着するようなサービスなので、regionはCloudFrontのメトリクス取得時と同じus-east-1一択です。
ところが上記を考慮してかつ--start-time、--end-time をメトリクスデータが取得できる期間に設定しても、awscliの実行結果のDatapointsはカラッポです。

awscli実行
 aws --region us-east-1 cloudwatch get-metric-statistics \
 --namespace AWS/WAF \
 --metric-name AllowedRequests \
 --start-time `date -u -d '2 hour ago' +%Y-%m-%dT%TZ` \
 --end-time `date -u +%Y-%m-%dT%TZ` \
 --period 60 \
 --statistics Sum \
 --dimensions Name=Rule,Value=Default_Action Name=WebACL,Value=WebACL名
awscli実行結果
{
    "Datapoints": [],
    "Label": "AllowedRequests"
}

namespaceについて

https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/DeveloperGuide/aws-namespaces.html
で確認すると、namespaceはAWS/WAF となっています。
しかし以下awscliコマンド

aws --region us-east-1 cloudwatch list-metrics

で確認すると、namespaceはAWS/WAFじゃなーい!WAFだあー!!!

Datapointsの中身が取得できるawscliの発行

awscli実行
 aws --region us-east-1 cloudwatch get-metric-statistics \
 --namespace WAF \
 --metric-name AllowedRequests \
 --start-time `date -u -d '2 hour ago' +%Y-%m-%dT%TZ` \
 --end-time `date -u +%Y-%m-%dT%TZ` \
 --period 60 \
 --statistics Sum \
 --dimensions Name=Rule,Value=Default_Action Name=WebACL,Value=WebACL名

を実行すると、以下が取得できます。

awscli実行結果
{
    "Datapoints": [
        {
            "Timestamp": "2016-04-24T19:31:00Z",
            "Sum": 1.0,
            "Unit": "None"
        },
省略
        {
            "Timestamp": "2016-04-24T19:40:00Z",
            "Sum": 1.0,
            "Unit": "None"
        }
    ],
    "Label": "AllowedRequests"
}

ようやくDatapointsの中身が出力されました。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1