6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS CLIでWAFのCloudWatchメトリクスを取得するときにはまったはなし

Last updated at Posted at 2020-08-31

はじめに

■ やりたかったこと。
Cloudwatchにアラームを設定して、アラーム状態になったときにSNSを使って通知がいくようにしたい => WAFのCloudWatchメトリクスの値をとらないと。

ということでaws cliでwafのメトリクスを取ってこようとしたのですが、なかなかうまくいかなくて半日費やしたので解決方法をまとめました。

失敗したやり方

▼ コマンド

root@xxxxxxx:/home# aws cloudwatch get-metric-statistics \
 --namespace AWS/WAFV2 \
 --metric-name CountedRequests \
 --start-time 2020-08-29T23:40:00 \
 --end-time 2020-08-31T10:00:00 \
 --period 300 \
 --statistics Sum \
 --dimensions Name=Rule,Value=Allow-Env-IP Name=WebACL,Value=env-create-acl

▼ 出力結果

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

色々試してみたけれどDatapointsが何をしても空になってしまいます。

解決方法

上手くいかなかったので、ぽちぽちアラームを作っていって何が必要なのか確認しました。
そして、アラーム状態にして飛んできたメールがこちら。

Alarm Details:
- Name:                       testtest
- Description:                
- State Change:               INSUFFICIENT_DATA -> ALARM
- Reason for State Change:    Threshold Crossed: 1 out of the last 1 datapoints [1.0 (31/08/20 04:39:00)] was greater than or equal to the threshold (1.0) (minimum 1 datapoint for OK -> ALARM transition).
- Timestamp:                  Monday 31 August, 2020 04:40:47 UTC
- AWS Account:                000000000000
- Alarm Arn:                  arn:aws:cloudwatch:ap-northeast-1:000000000000:alarm:testtest

Threshold:
- The alarm is in the ALARM state when the metric is GreaterThanOrEqualToThreshold 1.0 for 60 seconds. 

Monitored Metric:
- MetricNamespace:                     AWS/WAFV2
- MetricName:                          CountedRequests
- Dimensions:                          [WebACL = env-create-acl] [Region = ap-northeast-1] [Rule = Allow-Env-IP]
- Period:                              60 seconds
- Statistic:                           Sum
- Unit:                                not specified
- TreatMissingData:                    missing


State Change Actions:
- OK: 
- ALARM: [arn:aws:sns:ap-northeast-1:000000000000:snstopic]
- INSUFFICIENT_DATA: 

んんん、これが怪しい気がしてきたぞ。
Dimensions:[WebACL = env-create-acl] [Region = ap-northeast-1] [Rule = Allow-Env-IP]

そして、--dimensionsRegionを追加してみたコマンドがこちら。

▼ コマンド

root@xxxxxxx:/home# aws cloudwatch get-metric-statistics \
 --namespace AWS/WAFV2 \
 --metric-name CountedRequests \
 --start-time 2020-08-29T23:40:00 \
 --end-time 2020-08-31T10:00:00 \
 --period 300 \
 --statistics Sum \
 --dimensions Name=Rule,Value=Allow-Env-IP Name=WebACL,Value=env-create-acl Name=Region,Value=ap-northeast-1

▼ 出力結果

{
    "Label": "CountedRequests",
    "Datapoints": [
        {
            "Timestamp": "2020-08-31T04:40:00Z",
            "Sum": 8.0,
            "Unit": "None"
        },
        {
            "Timestamp": "2020-08-31T05:20:00Z",
            "Sum": 40.0,
            "Unit": "None"
        }
    ]
}

できた!

おわりに

おまけ:後から気付いたのですがメールを送信しなくても、実はもうアラーム作成画面に答えがあったみたいです。確認大事!

図.png

6
0
0

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
6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?