Help us understand the problem. What is going on with this article?

[JAWS-UG CLI] CloudWatch:#4 アラームの削除

More than 5 years have passed since last update.

https://jawsug-cli.doorkeeper.jp/events/20523 でのハンズオン資料です。

AWS CLIを利用してCloudWatchのアラームを削除してみます。

前提条件

CloudWatchへの権限

CloudWatchに対して以下の権限があること。
(今回のハンズオンでは使わない権限もあります。)

IAMポリシードキュメント(抜粋)
      {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Action": [
              "cloudwatch:DeleteAlarms",
              "cloudwatch:DescribeAlarmsForMetric",
              "cloudwatch:ListMetrics",
            ],
            "Effect": "Allow",
            "Resource": "*"
          }
        ]
      }

AWS CLIのバージョン

以下のバージョンで動作確認済

  • AWS CLI 1.7.18
  • AWS CLI 1.7.8
コマンド
aws --version
結果(例)
aws-cli/1.7.18 Python/2.7.5 Darwin/13.4.0

0. 準備

0.1. リージョンの決定

作成するユーザのデフォルトリージョンを決めます。
(カレントユーザが利用するカレントリージョンも切り変わります。)

コマンド(東京リージョンの場合)
export AWS_DEFAULT_REGION='ap-northeast-1'

0.2. 変数の確認

プロファイルとリージョンが想定のものになっていることを確認します。

変数の確認
aws configure list
結果(例)
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile      iam_full-prjZ-mbp13              env    AWS_DEFAULT_PROFILE
      access_key     ****************LOAQ shared-credentials-file
      secret_key     ****************I1O1 shared-credentials-file
          region           ap-northeast-1              env    AWS_DEFAULT_REGION

1. 事前作業

1.1. 変数の確認

変数の確認
cat << ETX

CWATCH_ALARM_NAME:  ${CWATCH_ALARM_NAME}

ETX

1.2. アラーム名の指定

CWATCH_ALARM_NAMEが空か、想定と異なる場合は指定しなおします。

コマンド(例)
CWATCH_ALARM_NAME='visible-test'

2. 事前確認

アラーム設定の確認

アラームの設定内容を確認します。

コマンド
aws cloudwatch describe-alarms --alarm-names ${CWATCH_ALARM_NAME}
結果(例)
      {
          "MetricAlarms": [
              {
                  "EvaluationPeriods": 1, 
                  "AlarmArn": "arn:aws:cloudwatch:ap-northeast-1:XXXXXXXXXXXX:alarm:visible-test", 
                  "StateUpdatedTimestamp": "2015-01-05T02:24:05.584Z", 
                  "AlarmConfigurationUpdatedTimestamp": "2015-01-05T02:24:04.968Z", 
                  "ComparisonOperator": "GreaterThanOrEqualToThreshold", 
                  "AlarmActions": [
                      "arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic"
                  ], 
                  "Namespace": "AWS/SQS", 
                  "AlarmDescription": "Test Alert for SQS Visible", 
                  "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2015-01-05T02:24:05.571+0000\",\"startDate\":\"2015-01-05T02:20:00.000+0000\",\"unit\":\"Count\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[0.0],\"threshold\":1.0}", 
                  "Period": 60, 
                  "StateValue": "OK", 
                  "Threshold": 1.0, 
                  "AlarmName": "visible-test", 
                  "Dimensions": [
                      {
                          "Name": "QueueName", 
                          "Value": "example-queue"
                      }
                  ], 
                  "Unit": "Count", 
                  "Statistic": "Average", 
                  "StateReason": "Threshold Crossed: 1 datapoint (0.0) was not greater than or equal to the threshold (1.0).", 
                  "InsufficientDataActions": [], 
                  "OKActions": [], 
                  "ActionsEnabled": true, 
                  "MetricName": "ApproximateNumberOfMessagesVisible"
              }
          ]
      }

3. 削除

アラームを削除します。

コマンド
aws cloudwatch delete-alarms --alarm-names ${CWATCH_ALARM_NAME}
結果
(戻り値なし)

4. 事後確認

4.1. アラーム設定の確認

アラームの設定が存在しないことを確認します。

コマンド
aws cloudwatch describe-alarms --alarm-names ${CWATCH_ALARM_NAME}
結果
{
    "MetricAlarms": []
}

4.2. アラーム変更履歴の確認

アラーム変更履歴に、以下の1つのエントリがあることを確認します。

  • "HistoryItemType"が"ConfigurationUpdate"で、HistorySummaryが"HistorySummary": "Alarm \"visible-test\" deleted"のエントリ
コマンド
CWATCH_MAX_ITEMS='1'
コマンド
aws cloudwatch describe-alarm-history \
        --alarm-name ${CWATCH_ALARM_NAME} \
        --max-items ${CWATCH_MAX_ITEMS}
結果(例)
      {
          "AlarmHistoryItems": [
              {
                  "Timestamp": "2015-01-05T06:16:01.319Z", 
                  "HistoryItemType": "ConfigurationUpdate", 
                  "AlarmName": "visible-test", 
                  "HistoryData": "{\"version\":\"1.0\",\"type\":\"Delete\",\"deletedAlarm\":{\"threshold\":1.0,\"namespace\":\"AWS/SQS\",\"unit\":\"Count\",\"stateValue\":\"OK\",\"metricName\":\"ApproximateNumberOfMessagesVisible\",\"period\":60,\"dimensions\":[{\"name\":\"QueueName\",\"value\":\"example-queue\"}],\"okactions\":[],\"alarmActions\":[\"arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic\"],\"evaluationPeriods\":1,\"comparisonOperator\":\"GreaterThanOrEqualToThreshold\",\"alarmName\":\"visible-test\",\"actionsEnabled\":true,\"statistic\":\"Average\",\"insufficientDataActions\":[],\"stateUpdatedTimestamp\":\"2015-01-05T05:11:52.179+0000\",\"alarmArn\":\"arn:aws:cloudwatch:ap-northeast-1:XXXXXXXXXXXX:alarm:visible-test\",\"alarmConfigurationUpdatedTimestamp\":\"2015-01-05T06:11:30.824+0000\",\"alarmDescription\":\"Test Alert for SQS Visible\"}}", 
                  "HistorySummary": "Alarm \"visible-test\" deleted"
              }
          ], 
          "NextToken": "None___1"
      }

完了

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした