AWS Health Aware(AWS Healthの通知フレームワーク) をTerraformでデプロイしてみる

2022.09.06

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

はじめに

AWS Health Aware はAWS Healthの通知フレームワークです。 組織(AWS Organizations)やAWSアカウント単体の AWS Health イベントの通知を管理します。 テンプレートを展開するだけで、 Slackチャンネルや Eメール、EventBridge など各種エンドポイントへの 通知設定を簡単に構築できます。

AWS Health Aware(AHA) の展開には CloudFormation もしくは Terraform が使えます。 今回は Terraform を使ってAHAを展開してみました。

AWS Health Aware についての詳細や、 CloudFormationを使った展開についてのブログは以下が参考になります。 そちらを御覧ください。

構築の概要

以下のような環境、およびAHA展開を試してみました。

  • AWS Organizations環境
  • 特定メンバーアカウント上にAHAを展開
  • 東京リージョンとバージニア北部リージョン、グローバルのAWS Healthイベントを取得
  • 送信先はEventBridgeのカスタムイベントバス

構築してみる

AHA WITH AWS Organizations on Member Account using Terraform の手順に沿って進めます。

事前準備: Health Organizational View の有効化

この作業は管理アカウント上で行います。

Health Organizational View を有効化します。 AWS Health ページの [組織の状態] → [設定] からポチポチで有効化できます。

img

事前準備: 通知先のエンドポイントの作成

この作業はメンバーアカウント上で行います。

サポートしているエンドポイントは以下のとおり。

  • Amazon Chime
  • Slack
  • Microsoft Teams
  • Email
  • EventBridge

1つ以上のエンドポイントを準備しておきます。

今回は EventBridge を使います。 カスタムイベントバス( aha-eventbus )を作成しました。

img

デプロイ: リポジトリのクローン

AHAリポジトリをローカルにクローンします。

$ git clone https://github.com/aws-samples/aws-health-aware.git

デプロイ: 管理アカウントにIAMロールを展開

この作業は管理アカウント上で行います。

このIAMロールは「AHAの展開先メンバーアカウント」から「管理アカウント」 へスイッチロールするものです。組織全体のHealth情報を取得するための権限が含まれています。

$ cd aws-health-aware/terraform/Terraform_MGMT_ROLE
$ terraform init
$ terraform plan
$ terraform apply
 Input *OrgMemberAccountId*  Enter the account id of the member account you plan to run AHA in (e.g. 000123456789).
 # ↑ここでメンバーアカウントのAWSアカウントIDを入力します

展開後の AWSHealthAwareRoleForPHDEventsArn のロールARNをメモしておきます。 次で使います。

AWSHealthAwareRoleForPHDEventsArn = "arn:aws:iam::111111111111:role/AWSHealthAwareRoleForPHDEvents-example"

デプロイ: メンバーアカウントにAHAを展開

この作業はメンバーアカウント上で行います。

ディレクトリ移動します。

$ cd aws-health-aware/terraform/Terraform_DEPLOY_AHA

今回の構築に合わせて terraform.tfvars を編集します。 変更点は以下のとおり。

- aha_primary_region="us-east-1"
+ aha_primary_region="ap-northeast-1" 
# → 展開先を東京リージョンに変更

- AWSOrganizationsEnabled="No"
+ AWSOrganizationsEnabled="Yes"
# → Organizationsを利用

- EventBusName=""
+ EventBusName="aha-eventbus"
# → EventBridgeカスタムイベントバスを指定

- Regions="all regions"
+ Regions="us-east-1,ap-northeast-1,global"
# → 東京リージョンとバージニア北部リージョン、グローバルの Health情報を取得

- ManagementAccountRoleArn=""
+ ManagementAccountRoleArn="arn:aws:iam::111111111111:role/AWSHealthAwareRoleForPHDEvents-example"
# → 先程メモした「管理アカウントにあるIAMロールのARN」

デプロイします。

$ terraform init
$ terraform plan
$ terraform apply

以下リソースが展開されました。

$ terraform state list
data.archive_file.lambda_zip
data.aws_caller_identity.current
data.aws_iam_policy_document.AHA-LambdaPolicy-Document
aws_cloudwatch_event_rule.AHA-LambdaSchedule-PrimaryRegion
aws_cloudwatch_event_target.AHA-LambdaFunction-PrimaryRegion
aws_dynamodb_table.AHA-DynamoDBTable[0]
aws_iam_role.AHA-LambdaExecutionRole
aws_lambda_function.AHA-LambdaFunction-PrimaryRegion
aws_lambda_permission.AHA-LambdaSchedulePermission-PrimaryRegion
aws_secretsmanager_secret.AssumeRoleArn[0]
aws_secretsmanager_secret.EventBusName[0]
aws_secretsmanager_secret_version.AssumeRoleArn[0]
aws_secretsmanager_secret_version.EventBusName[0]
random_string.resource_code

完了

以上でAHAの展開は完了です。

必要に応じてEventBridgeルールを作成、SNSトピック等に飛ばします。

通知を確認

実際の通知をいくつか受け取りました。 出てきた通知は以下のようなJSON形式です。

{
  "account": "111111111111",
  "region": "ap-northeast-1",
  "detail": {
    "mydata": {
      "attachments": [
        {
          "fields": [
            {
              "short": true,
              "value": "HOGE_PRD (222222222222)",
              "title": "Account(s)"
            },
            {
              "short": true,
              "value": "222222222222",
              "title": "Resource(s)"
            },
            {
              "short": true,
              "value": "S3",
              "title": "Service"
            },
            {
              "short": true,
              "value": "ap-northeast-1",
              "title": "Region"
            },
            {
              "short": true,
              "value": "2022-09-02 09:45:00",
              "title": "Start Time (UTC)"
            },
            {
              "short": true,
              "value": "open",
              "title": "Status"
            },
            {
              "short": false,
              "value": "arn:aws:health:ap-northeast-1::event/S3/AWS_S3_OPERATIONAL_NOTIFICATION/AWS_S3_OPERATIONAL_NOTIFICATION_7c0example",
              "title": "Event ARN"
            },
            {
              "short": false,
              "value": "English follows Japanese | 英語のメッセージは日本語の後にございます \n\nAmazon S3 の AWS CloudTrail データイベントを拡張し、バケット所有者がオブジェクトレベルのアクセス許可や ACL に関係なく(略)",
              "title": "Updates"
            }
          ]
        }
      ]
    }
  },
  "detail-type": "create",
  "source": "aha",
  "version": "0",
  "time": "2022-09-02T11:00:02Z",
  "id": "4b360example-1111-2222-3333-example",
  "resources": []
}

(ほかに出てきたイベントを、自分の備忘録も兼ねて [補足: AHAイベントサンプル] に記載してます。)

なお Slack Webhook 等を設定すると以下のように整形した形で通知されるので、 利用できる場合はそちらを使ったほうが楽ですね。

img

画像: Organizations連携で集約したAWS Personal Health Dashboardのイベントを通知するAWS Health Aware (AHA)を試してみた | DevelopersIO

おわりに

AHAを Organizationsレベルで、Terraformでデプロイしてみました。

今回はイベント内容・形式の把握のために、 全ての通知 ( HEALTH_EVENT_TYPE = issue | accountNotification | scheduledChange ) を受け取りました。 ただ、この部分は絞って良いかもしれません。

  • 最低限 AWS側の障害(APIエラー等)を受け取れる issue に絞る
  • 「EC2インスタンスのリタイア予告」などは受け取りたいので scheduledChange は入れる

といった絞り込みが候補になってきそうです。

参考

補足

構築時のTerraformバージョン

$ terraform -version

Terraform v1.1.2
on darwin_arm64
+ provider registry.terraform.io/hashicorp/aws v3.74.3

SNSトピックとの連携例(Terraformコード)

data aws_caller_identity this {}

resource aws_cloudwatch_event_bus aha {
  name = "aha-eventbus"
}

### SNS Topic
resource aws_sns_topic aha {
  name = "aha-notification"
}

# resource aws_sns_topic_subscription aha {}

resource aws_sns_topic_policy aha {
  arn = aws_sns_topic.health.arn
  policy =  <<-EOF
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Principal": {
            "Service": "events.amazonaws.com"
          },
          "Action": "sns:Publish",
          "Resource": "*"
        }
      ]
    }
  EOF
}

### EventBridge Rule
resource aws_cloudwatch_event_rule aha {
  name           = "aha-notification"
  event_bus_name = aws_cloudwatch_event_bus.aha.name

  event_pattern = <<-EOF
    {
      "account": ["${data.aws_caller_identity.this.account_id}"]
    }
  EOF
}

resource aws_cloudwatch_event_target aha {
  event_bus_name = aws_cloudwatch_event_bus.aha.name
  rule           = aws_cloudwatch_event_rule.aha.name
  target_id      = "sns-topic"
  arn            = aws_sns_topic.aha.arn
}

AHAのイベントサンプル

▼ S3の AWS_S3_OPERATIONAL_NOTIFICATION (カテゴリは accountNotification )

{
  "account": "111111111111",
  "region": "ap-northeast-1",
  "detail": {
    "mydata": {
      "attachments": [
        {
          "fields": [
            {
              "short": true,
              "value": "HOGE_PRD (222222222222)",
              "title": "Account(s)"
            },
            {
              "short": true,
              "value": "222222222222",
              "title": "Resource(s)"
            },
            {
              "short": true,
              "value": "S3",
              "title": "Service"
            },
            {
              "short": true,
              "value": "ap-northeast-1",
              "title": "Region"
            },
            {
              "short": true,
              "value": "2022-09-02 09:45:00",
              "title": "Start Time (UTC)"
            },
            {
              "short": true,
              "value": "open",
              "title": "Status"
            },
            {
              "short": false,
              "value": "arn:aws:health:ap-northeast-1::event/S3/AWS_S3_OPERATIONAL_NOTIFICATION/AWS_S3_OPERATIONAL_NOTIFICATION_7c0example",
              "title": "Event ARN"
            },
            {
              "short": false,
              "value": "English follows Japanese | 英語のメッセージは日本語の後にございます \n\nAmazon S3 の AWS CloudTrail データイベントを拡張し、バケット所有者がオブジェクトレベルのアクセス許可や ACL に関係なく(略)",
              "title": "Updates"
            }
          ]
        }
      ]
    }
  },
  "detail-type": "create",
  "source": "aha",
  "version": "0",
  "time": "2022-09-02T11:00:02Z",
  "id": "4b360example-1111-2222-3333-example",
  "resources": []
}

▼ CloudShellの AWS_CLOUDSHELL_PERSISTENCE_EXPIRING (カテゴリは scheduledChange )

{
   "account": "111111111111",
   "region": "ap-northeast-1",
   "detail": {
       "mydata": {
           "attachments": [
               {
                   "fields": [
                       {
                           "short": true,
                           "value": "HOGE_PRD (222222222222)",
                           "title": "Account(s)"
                       },
                       {
                           "short": true,
                           "value": "arn:aws:sts::222222222222:assumed-role/cm-hoge/cm-hoge",
                           "title": "Resource(s)"
                       },
                       {
                           "short": true,
                           "value": "CLOUDSHELL",
                           "title": "Service"
                       },
                       {
                           "short": true,
                           "value": "ap-northeast-1",
                           "title": "Region"
                       },
                       {
                           "short": true,
                           "value": "2022-09-04 00:00:00",
                           "title": "Start Time (UTC)"
                       },
                       {
                           "short": true,
                           "value": "open",
                           "title": "Status"
                       },
                       {
                           "short": false,
                           "value": "arn:aws:health:ap-northeast-1::event/CLOUDSHELL/AWS_CLOUDSHELL_PERSISTENCE_EXPIRING/AWS_CLOUDSHELL_PERSISTENCE_EXPIRING_v20220208_222222222222_ap-northeast-1_14-9-2022",
                           "title": "Event ARN"
                       },
                       {
                           "short": false,
                           "value": "Some users of this account haven't used AWS CloudShell for over 110 days in the ap-northeast-1 Region. On September 14, 2022 we're scheduled to delete the CloudShell home directory and data of inactive users in the ap-northeast-1 Region.(略)",
                           "title": "Updates"
                       }
                   ]
               }
           ]
       }
   },
   "detail-type": "create",
   "source": "aha",
   "version": "0",
   "time": "2022-09-04T12:24:01Z",
   "id": "08bb6example",
   "resources": []
}

▼ ACMの AWS_ACM_RENEWAL_STATE_CHANGE open (カテゴリは scheduledChange )

{
   "account": "111111111111",
   "region": "ap-northeast-1",
   "detail": {
       "mydata": {
           "attachments": [
               {
                   "fields": [
                       {
                           "short": true,
                           "value": "HOGE_PRD (222222222222)",
                           "title": "Account(s)"
                       },
                       {
                           "short": true,
                           "value": "arn:aws:acm:ap-northeast-1:222222222222:certificate/72064aexample",
                           "title": "Resource(s)"
                       },
                       {
                           "short": true,
                           "value": "ACM",
                           "title": "Service"
                       },
                       {
                           "short": true,
                           "value": "ap-northeast-1",
                           "title": "Region"
                       },
                       {
                           "short": true,
                           "value": "2022-09-06 00:00:00",
                           "title": "Start Time (UTC)"
                       },
                       {
                           "short": true,
                           "value": "open",
                           "title": "Status"
                       },
                       {
                           "short": false,
                           "value": "arn:aws:health:ap-northeast-1::event/ACM/AWS_ACM_RENEWAL_STATE_CHANGE/AWS_ACM_RENEWAL_STATE_CHANGE-b41cexample",
                           "title": "Event ARN"
                       },
                       {
                           "short": false,
                           "value": "You have an AWS Certificate Manager (ACM) SSL/TLS certificate in your AWS account that expires on Oct 20, 2022 at 23:59:59 UTC. That certificate includes the primary domain *.www.example.com and a total of 1 domains...(略)"
                       (以降メッセージ省略)

▼ ACMの AWS_ACM_RENEWAL_STATE_CHANGE closed (カテゴリは scheduledChange )

{
   "account": "111111111111",
   "region": "ap-northeast-1",
   "detail": {
       "mydata": {
           "attachments": [
               {
                   "fields": [
                       {
                           "short": true,
                           "value": "HOGE_PRD (222222222222)",
                           "title": "Account(s)"
                       },
                       {
                           "short": true,
                           "value": "arn:aws:acm:ap-northeast-1:222222222222:certificate/720aexample",
                           "title": "Resource(s)"
                       },
                       {
                           "short": true,
                           "value": "ACM",
                           "title": "Service"
                       },
                       {
                           "short": true,
                           "value": "ap-northeast-1",
                           "title": "Region"
                       },
                       {
                           "short": true,
                           "value": "2022-09-06 01:07:00",
                           "title": "Start Time (UTC)"
                       },
                       {
                           "short": true,
                           "value": "2022-09-06 01:07:00",
                           "title": "End Time (UTC)"
                       },
                       {
                           "short": true,
                           "value": "closed",
                           "title": "Status"
                       },
                       {
                           "short": false,
                           "value": "arn:aws:health:ap-northeast-1::event/ACM/AWS_ACM_RENEWAL_STATE_CHANGE/AWS_ACM_RENEWAL_STATE_CHANGE-b41c94example",
                           "title": "Event ARN"
                       },
                       {
                           "short": false,
                           "value": "This is to notify you that AWS Certificate Manager (ACM) has completed the renewal of an SSL/TLS certificate that certificate includes the primary domain ...(略)",
                           "title": "Updates"
                       }
                   ]
               }
           ]
       }
   },
   "detail-type": "resolve",
   "source": "aha",
   "version": "0",
   "time": "2022-09-06T01:08:01Z",
   "id": "901dexample",
   "resources": []
}

2022/09/06 展開時のメモ

  • M1 Macで展開時にエラーが出る (内部で使っている template_file プロバイダーが対応していないため)
  • シングルリージョン展開時にエラーが出る

上記解決策はすでに Pull Request がありました。(感謝!)