AWS HealthでEC2の予定されたイベントの通知にアクションを設定する #reinvent

2016.12.04

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

AWS HealthでEC2の予定されたイベントの通知にアクションを設定する

AWS の EC2 や RDS では再起動、停止/開始、またはリタイアといったインスタンス単位のイベントが稀に発生します。インスタンスのいずれかが予定されたイベントの影響を受ける場合、予定されたイベントの前に AWS アカウントに関連付けられた E メールアドレスにイベントの詳細(開始日、終了日など)が記載された E メールが AWS から送信されます。

先日発表された AWS Health により、このようなイベントの予定は CloudWatch Events 経由で発火され、アクションを定義出来るようになりました。

実際にやってみましょう。

AWS Health と CloudWatch Events を連携させる方法

例として、EC2 の予定されたイベントが発生するとSNS通知させるように設定します。

以下の流れで作業します。

  1. SNS トピックの作成
  2. CloudWatch Events の設定

SNS トピックの作成

CloudWatch Events からの通知先の SNS トピックを作成します。 作成したトピックに対して、E-Mail 送信などで購読し、トピックにメッセージが送信されたときに受け取れるようにします。

CloudWatch Events は SNS 以下の通知に対応しています。

  • AWS Lambda functions
  • Amazon Kinesis streams
  • Amazon SQS queues
  • Built-in targets (CloudWatch alarm actions)
  • Amazon SNS topics

CloudWatch Events のルール作成画面に移動

Amazon 管理コンソールで CloudWatch → Events と移動し、Create rule ボタンをクリックします。

CloudWatch_Management_Console

Event selector の設定

ルールの作成画面において Event selector を次のように設定します。

一つ目の AWS サービスの対象を選択する項目において Specific Service(s) の EC2 を選択します。

二つ目の イベントタイプを選択する項目において Specific event type category(s)scheduledChange を選択します。

三つ目の 対象インスタンスを選択する項目において、今回は全インスタンスを対象とするため Any event type code を選択します。

CloudWatch Events Selector

Targets の設定

ルールの作成画面において Targets を次のように設定します。

SNS に通知するため、STEP 1 で作成した SNS トピックを選択します。

CloudWatch Events Targets

  • Event selector
  • Targets

の設定が完了したら "Configure details" ボタンを押します。

ルールのメタ情報を設定

  • ルール名(Name)
  • 概要(Description)
  • 有効無効状態(Status)

を設定します。 設定が完了したら Create rule ボタンをクリックします

condigure rule details 作成が完了すると、Rule 一覧画面に遷移し、作成したルールが登録されていることがわかります。

CLoudWatch Events List

ルールの詳細を確認

作成した ec2-scheduled-change ルールをクリックすると、ルールの詳細を確認できます。

CloudWatch_Events_Detial

Event Pattern は以下のようになっています。

{
  "source": [
    "aws.health"
  ],
  "detail-type": [
    "AWS Health Event"
  ],
  "detail": {
    "service": [
      "EC2"
    ],
    "eventTypeCategory": [
      "scheduledChange"
    ]
  }
}

イベントが発生するのを待つ

以上で CloudWatch Events の設定は完了しました。 あとは運用している EC2 インスタンスにイベントが予定されると、SNS 経由で購読したメールアドレスに通知が飛ぶようになります。

具体的には、「AWS Notification Message」と言う件名で以下のような JSON メッセージが届きました。

{
  "Type" : "Notification",
  "MessageId" : "DUMMY-DUMMY",
  "TopicArn" : "arn:aws:sns:us-east-1:123456789012:test",
  "Message" : "DUMMY",
  "Timestamp" : "2017-07-21T01:11:05.236Z",
  "SignatureVersion" : "1",
  "Signature" : "XXXX==",
  "SigningCertURL" : "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-DUMMY.pem",
  "UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:123456789012:test:DUMMY"
}

このメッセージに対して、本文とも言える Message を JSON としてパースし pretty print したのが以下です。

{
  "version": "0",
  "id": "dummy",
  "detail-type": "AWS Health Event",
  "source": "aws.health",
  "account": "123456789012",
  "time": "2017-07-24T16:00:00Z",
  "region": "us-east-1",
  "resources": [
    "db-FOO",
    "db-BAR"
  ],
  "detail": {
    "eventArn": "arn:aws:health:us-east-1::event/AWS_RDS_OPERATIONAL_NOTIFICATION_DUMMY",
    "service": "RDS",
    "eventTypeCode": "AWS_RDS_OPERATIONAL_NOTIFICATION",
    "eventTypeCategory": "accountNotification",
    "startTime": "Mon, 24 Jul 2017 16:00:00 GMT",
    "endTime": "Tue, 25 Jul 2017 16:00:00 GMT",
    "eventDescription": [
      {
        "language": "en_US",
        "latestDescription": "You are receiving this email because you currently have one or more Amazon Aurora database instances using the Performance Insights feature in the preview of Amazon Aurora PostgreSQL-compatibility. We will be upgrading the Performance Insights infrastructure to improve stability, functionality, and performance as part of moving towards making the feature generally available.  The feature will not be available between July 24 9:00 AM and July 25 9:00 AM PDT.  After the upgrade, you should still be able to see performance data collected prior to the upgrade, but it may contain minor anomalies. Performance data collected after the upgrade should be correct.    New functionality that will be available:  - ability to click on the load chart and drag to select a region and upon releasing the mouse, the load chart will zoom into the select region and the Top N table will adjust to the newly selected region.  - when clicking on a row in the Top N table, the load chart will filter to show only that selected item and the load chart will say scope to: followed by the item identifier (such as the SQL text for a SQL statement   See new functionality on demonstration video: https://www.youtube.com/watch?v=slADfUH-TWk    Your impacted RDS instances are listed on 'Affected resources' tab.   There is no action necessary on your part. "
      }
    ],
    "affectedEntities": [
      {
        "entityValue": "db-FOO",
        "tags": {}
      },
      {
        "entityValue": "db-BAR",
        "tags": {}
      }
    ]
  }
}

より発展的なアクション設定

今回は設定を簡単にするために、イベントの通知先として SNS を利用しました。 イベント発生時に Lambda を呼び出すようにすると

  • 対象 EC2 インスタンスの再起動
  • イベント内容をもとにチャットやメールを送信

などを柔軟に行えるようになります。

まとめ

EC2/RDS などの予定されたイベントに対して、E メール通知をもとにプログラマブルにアクションを取っていた方は多いと思いますが、今回の機能追加により AWS と親和性高くプログラマブルにアクションを定義出来るようになりました。

オペレーション業務がより効率化されるのではないでしょうか。

参考