AWS SDK For Python (Boto3) で AWS Personal Health Dashboardの情報を取得する

2018.04.19

AWS Personal Health Dashboard (AWS Health) はAWSアカウントに影響のあるイベント情報が表示されます。Amazon CloudWatch Eventsを使用してもイベントのステータス変化を検出して対応することもできますが、Python (Boto3)で必要な情報を取得してみましたのでサンプルコードをご紹介します。

  • ※AWS Health APIはビジネスサポートプラン、エンタープライズサポートプランのアカウントで利用できます。
  • ※リージョンはus-east-1で使用します。

サンプルコード

import boto3
import json
from bson import json_util

# Health constant
REGION = 'us-east-1'


# Function for describe_event (Specify desired information and acquire event.)
def get_describe_event(services, event_status_codes):
    health = boto3.client('health', region_name=REGION)
    response = health.describe_events(
        filter={
            'services': services,
            'eventStatusCodes': event_status_codes
        }
    )

    return response


# Function for describe_event_details (Detailed information about the specified event.)
def get_describe_event_details(event_arns):
    health = boto3.client('health', region_name=REGION)
    response = health.describe_event_details(eventArns=event_arns)

    return response


# Function for describe_affected_entities (Returns the list of entities affected by the specified event.)
def get_describe_affected_entities(event_arns):
    health = boto3.client('health', region_name=REGION)
    response = health.describe_affected_entities(
        filter={
            'eventArns': event_arns
        }
    )

    return response


def main():

    # Filter by specific information for describe_event function. 
    # (ex. services is vpn, event status coeds except close status.)
    services = ['VPN']
    event_status_codes = ['open', 'upcoming']

    # Get describe event (Specify desired information as list type)
    result = get_describe_event(services, event_status_codes)
    print("starting describe event")
    print(json.dumps(result, default=json_util.default, sort_keys=True, indent=4))
    print("end of describe event")

    # Get describe event details
    event_arns = []
    for event in result['events']:
        event_arns.append(event['arn'])

    result = get_describe_event_details(event_arns)
    print("starting describe event details")
    print(json.dumps(result, default=json_util.default, sort_keys=True, indent=4))
    print("end of describe event details")

    # Get describe affected entities
    result = get_describe_affected_entities(event_arns)
    print("starting describe affected entities")
    print(json.dumps(result, default=json_util.default, sort_keys=True, indent=4))
    print("end of describe affected entities")


if __name__ == '__main__':
    main()

DescribeEvents

DescribeEvents を使用してイベントを取得します。 サンプルコードでは、ServiceVPNeventStatusCodesopen, upcoming にして closed 以外のイベントを取得しています。

もう少し絞りたい場合はfilterで絞り込めるので13行目からの部分で追加してください。

  • 実行結果
{
"ResponseMetadata": {
"HTTPHeaders": {
"content-length": "736",
"content-type": "application/x-amz-json-1.1",
"date": "Wed, 18 Apr 2018 06:58:38 GMT",
"x-amzn-requestid": "eba3fbf0-42d5-11e8-b7d6-************"
},
"HTTPStatusCode": 200,
"RequestId": "eba3fbf0-42d5-11e8-b7d6-************",
"RetryAttempts": 0
},
"events": [
{
"arn": "arn:aws:health:region::event/AWS_VPN_MAINTENANCE_SCHEDULED_************",
"endTime": {
"$date": date
},
"eventTypeCategory": "scheduledChange",
"eventTypeCode": "AWS_VPN_MAINTENANCE_SCHEDULED",
"lastUpdatedTime": {
"$date": date
},
"region": "region",
"service": "VPN",
"startTime": {
"$date": date
},
"statusCode": "upcoming"
},
{
"arn": "arn:aws:health:region::event/AWS_VPN_MAINTENANCE_SCHEDULED_************",
"endTime": {
"$date": date
},
"eventTypeCategory": "scheduledChange",
"eventTypeCode": "AWS_VPN_MAINTENANCE_SCHEDULED",
"lastUpdatedTime": {
"$date": date
},
"region": "region",
"service": "VPN",
"startTime": {
"$date": date
},
"statusCode": "upcoming"
}
]
}

VPNで2つのイベントがあることがわかりました。

DescribeEventDetails

DescribeEvents でイベントが取得できたので DescribeEventDetails で詳細を確認します。 DescribeEventDetails では eventArnsが必要なので以下のようにlistで取得します。

# Get describe event details
event_arns = []
for event in result['events']:
event_arns.append(event['arn'])
  • 実行結果
{
"ResponseMetadata": {
"HTTPHeaders": {
"content-length": "2971",
"content-type": "application/x-amz-json-1.1",
"date": "Wed, 18 Apr 2018 07:00:36 GMT",
"x-amzn-requestid": "3267386c-42d6-11e8-b7d6-************"
},
"HTTPStatusCode": 200,
"RequestId": "3267386c-42d6-11e8-b7d6-************",
"RetryAttempts": 0
},
"failedSet": [],
"successfulSet": [
{
"event": {
"arn": "arn:aws:health:region::event/AWS_VPN_MAINTENANCE_SCHEDULED_************",
"endTime": {
"$date": date
},
"eventTypeCategory": "scheduledChange",
"eventTypeCode": "AWS_VPN_MAINTENANCE_SCHEDULED",
"lastUpdatedTime": {
"$date": date
},
"region": "region",
"service": "VPN",
"startTime": {
"$date": date
},
"statusCode": "upcoming"
},
"eventDescription": {
"latestDescription": "You're receiving this notification because you have at least one VPN Connection scheduled for planned maintenance in the region region. [...]"
}
},
{
"event": {
"arn": "arn:aws:health:region::event/AWS_VPN_MAINTENANCE_SCHEDULED_************",
"endTime": {
"$date": date
},
"eventTypeCategory": "scheduledChange",
"eventTypeCode": "AWS_VPN_MAINTENANCE_SCHEDULED",
"lastUpdatedTime": {
"$date": date
},
"region": "region",
"service": "VPN",
"startTime": {
"$date": date
},
"statusCode": "upcoming"
},
"eventDescription": {
"latestDescription": "You're receiving this notification because you have at least one VPN Connection scheduled for planned maintenance in the region region. [...]"
}
}
]
}

DescribeAffectedEntities

詳細情報を取得することができましたが影響を受けるリソースがわからないので DescribeAffectedEntities で確認します。 DescribeAffectedEntitieseventArnsが必要です。

  • 実行結果
{
"ResponseMetadata": {
"HTTPHeaders": {
"content-length": "1046",
"content-type": "application/x-amz-json-1.1",
"date": "Wed, 18 Apr 2018 07:00:36 GMT",
"x-amzn-requestid": "328065c2-42d6-11e8-b7d6-************"
},
"HTTPStatusCode": 200,
"RequestId": "328065c2-42d6-11e8-b7d6-************",
"RetryAttempts": 0
},
"entities": [
{
"awsAccountId": "************",
"entityArn": "arn:aws:health:region:************:entity/AWLT6N8y************",
"entityValue": "vpn-********",
"eventArn": "arn:aws:health:region::event/AWS_VPN_MAINTENANCE_SCHEDULED_************",
"lastUpdatedTime": {
"$date": date
},
"statusCode": "IMPAIRED"
},
{
"awsAccountId": "************",
"entityArn": "arn:aws:health:region:************:entity/AWKV5Zj************",
"entityValue": "vpn-********",
"eventArn": "arn:aws:health:region::event/AWS_VPN_MAINTENANCE_SCHEDULED_************",
"lastUpdatedTime": {
"$date": date
},
"statusCode": "IMPAIRED"
},
{
"awsAccountId": "************",
"entityArn": "arn:aws:health:region:************:entity/AWKV5Zj************",
"entityValue": "vpn-********",
"eventArn": "arn:aws:health:region::event/AWS_VPN_MAINTENANCE_SCHEDULED_************",
"lastUpdatedTime": {
"$date": date
},
"statusCode": "IMPAIRED"
}
]
}

entityValueで影響のあるリソースが確認できました。

まとめ

今回は、VPNのメンテナンス情報を取得するために使ってみました。 VPNのメンテナンス情報はAWSから通知される情報ですが、タイムゾーンがPDT、PST、EDTとよく変更があるのでAWS Health APIで取得してからJSTに変換しようと思いAWS Health APIを使用しました。

特定のイベント情報を取得したい場合はご紹介したサンプルコードの内容で必要な情報取得が可能かと思いますので参考になれば幸いです。

参考URL

Health | Boto 3 Documentation