Elastic Beanstalk環境の通知設定を管理する方法

2016.08.29

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

西澤です。Elastic Beanstalk環境を構築した際に、通知先を設定をしておくことで、異常を容易に検知することが可能です。EB環境を構築、管理する上で、そのメリットをより多く享受できるようにする為には、アプリケーション全体をImmutableに作っておくことではないかと思います。今回、EBのNotifications設定について試行錯誤して、環境や設定を削除したときの挙動に違いがあることがわかったので、整理しておきたいと思います。

Notification Endpointでの管理

Notification Endpoint設定

AWS Management Consoleから操作を行う場合、実体はNotification Endpointを操作することになります。もちろん、こちらの設定を、.ebextensionsやAWS CLI、SDK等で行うこともできます。

eb-notification-1

EBのConfiguration -> Notificationsから通知先のメールアドレス(Notification Endpoint)を変更することができます。Endpointを指定するとそれを管理するSNS Topicも自動的に作成されていました。

eb-notification-2

ちょっと困ったのが、メールアドレス1件しか登録することができないという点でした。ドキュメントを探してみたり、実際にスペース区切り、コンマ区切り等も登録を試してみましたが、画面上からは設定することができませんでした。もちろん手動でSNS TopicにSubscriptionを追加することはできますが、管理上お勧めはできません。

Notification設定は、OptionSettingsに含まれます。

$ aws elasticbeanstalk describe-configuration-settings \
  --application-name testapp \
  --environment-name testapp-env-001 \
  --query "ConfigurationSettings[].OptionSettings[?contains(OptionName,\`Notification\`)]"
[
    [
        {
            "OptionName": "Notification Endpoint",
            "Namespace": "aws:elasticbeanstalk:sns:topics",
            "Value": "nishizawa.tetsunori@classmethod.jp"
        },
        {
            "OptionName": "Notification Protocol",
            "Namespace": "aws:elasticbeanstalk:sns:topics",
            "Value": "email"
        },
        {
            "OptionName": "Notification Topic ARN",
            "Namespace": "aws:elasticbeanstalk:sns:topics",
            "Value": "arn:aws:sns:us-west-2:123456789012:ElasticBeanstalkNotifications-Environment-testapp-env-001"
        },
        {
            "OptionName": "Notification Topic Name",
            "Namespace": "aws:elasticbeanstalk:sns:topics"
        }
    ]
]

Notification Endpoint削除時の挙動

EBの通知設定を削除するとどうなるでしょうか?Email欄を空にして"Apply"を押下してみます。

eb-notification-3

すると、SNS Topicも削除されました。この挙動は、Environment削除時も同様となります。手動でSubscription追加等を行っても問答無用で削除されました。EB環境とセットでSNS TopicもImmutableに管理というわけです。

eb-notification-4

Notification Topic ARNでの管理

Notification Topic ARN設定

AWS Management Consoleからは操作ができませんが、.ebextensionsやAWS CLI、SDK等を利用すれば、事前に定義したSNS TopicをEBの通知先として設定することも可能です。今回はAWS CLIから確認してみました。

$ aws elasticbeanstalk update-environment \
  --application-name testapp \
  --environment-name testapp-env-001 \
  --option-settings Namespace=aws:elasticbeanstalk:sns:topics,OptionName="Notification Topic ARN",Value="arn:aws:sns:us-west-2:123456789012:cm-test"
{
    "ApplicationName": "testapp",
    "EnvironmentName": "testapp-env-001",
    "VersionLabel": "Sample Application",
    "Status": "Updating",
    "EnvironmentId": "e-9y2qxxxxxx",
    "EndpointURL": "awseb-e-9-AWSEBLoa-19ZWRROSPJR1G-999999999.us-west-2.elb.amazonaws.com",
    "SolutionStackName": "64bit Amazon Linux 2016.03 v2.1.6 running PHP 7.0",
    "CNAME": "testapp-env-001.us-west-2.elasticbeanstalk.com",
    "Health": "Grey",
    "AbortableOperationInProgress": true,
    "Tier": {
        "Version": " ",
        "Type": "Standard",
        "Name": "WebServer"
    },
    "DateUpdated": "2016-08-29T10:46:54.940Z",
    "DateCreated": "2016-08-29T05:35:31.337Z"
}

$ aws elasticbeanstalk describe-configuration-settings \
  --application-name testapp \
  --environment-name testapp-env-001 \
  --query "ConfigurationSettings[].OptionSettings[?contains(OptionName,\`Notification\`)]"
[
    [
        {
            "OptionName": "Notification Endpoint",
            "Namespace": "aws:elasticbeanstalk:sns:topics"
        },
        {
            "OptionName": "Notification Protocol",
            "Namespace": "aws:elasticbeanstalk:sns:topics",
            "Value": "email"
        },
        {
            "OptionName": "Notification Topic ARN",
            "Namespace": "aws:elasticbeanstalk:sns:topics",
            "Value": "arn:aws:sns:us-west-2:123456789012:cm-test"
        },
        {
            "OptionName": "Notification Topic Name",
            "Namespace": "aws:elasticbeanstalk:sns:topics"
        }
    ]
]

この方法であれば、事前に好きなようにSubscriptionを追加しておくことができますので、Notification Endpoint設定とは異なり、複数の通知先を管理することもできそうです。

Notification Topic ARN削除時の挙動

こちらも今回はAWS CLIから行いました。

$ aws elasticbeanstalk update-environment \
  --application-name testapp \
  --environment-name testapp-env-001 \
  --options-to-remove Namespace=aws:elasticbeanstalk:sns:topics,OptionName="Notification Topic ARN"
{
    "ApplicationName": "testapp",
    "EnvironmentName": "testapp-env-001",
    "VersionLabel": "Sample Application",
    "Status": "Updating",
    "EnvironmentId": "e-9y2qxxxxxx",
    "EndpointURL": "awseb-e-9-AWSEBLoa-19ZWRROSPJR1G-999999999.us-west-2.elb.amazonaws.com",
    "SolutionStackName": "64bit Amazon Linux 2016.03 v2.1.6 running PHP 7.0",
    "CNAME": "testapp-env-001.us-west-2.elasticbeanstalk.com",
    "Health": "Grey",
    "AbortableOperationInProgress": true,
    "Tier": {
        "Version": " ",
        "Type": "Standard",
        "Name": "WebServer"
    },
    "DateUpdated": "2016-08-29T11:00:32.710Z",
    "DateCreated": "2016-08-29T05:35:31.337Z"
}

$ aws elasticbeanstalk describe-configuration-settings \
  --application-name testapp \
  --environment-name testapp-env-001 \
  --query "ConfigurationSettings[].OptionSettings[?contains(OptionName,\`Notification\`)]"
[
    [
        {
            "OptionName": "Notification Endpoint",
            "Namespace": "aws:elasticbeanstalk:sns:topics"
        },
        {
            "OptionName": "Notification Protocol",
            "Namespace": "aws:elasticbeanstalk:sns:topics",
            "Value": "email"
        },
        {
            "OptionName": "Notification Topic ARN",
            "Namespace": "aws:elasticbeanstalk:sns:topics"
        },
        {
            "OptionName": "Notification Topic Name",
            "Namespace": "aws:elasticbeanstalk:sns:topics"
        }
    ]
]

この方法であれば、SNS Topicは削除されませんでした。EB環境とは別にSNS Topicを管理したい場合にはこの方法が適切であることがわかりました。この方式の場合、EB環境のアプリケーションとは別管理にしたいので、.ebextensionsから設定をすることは可能ですが、管理上適切では無いと思います。

eb-notification-5

Notification Topic Nameでの管理

Notification Topic Name設定

今度は、Notification Topic ARNとよく似たNotification Topic Nameを変更してみます。追加方法はほぼ同じです。Notification Endpointを設定したときと同様ですが、Notification Topic ARNも自動的に設定されました。

$ aws elasticbeanstalk update-environment \
  --application-name testapp \
  --environment-name testapp-env-001 \
  --option-settings Namespace=aws:elasticbeanstalk:sns:topics,OptionName="Notification Topic Name",Value="cm-test"
{
    "ApplicationName": "testapp",
    "EnvironmentName": "testapp-env-001",
    "VersionLabel": "Sample Application",
    "Status": "Updating",
    "EnvironmentId": "e-9y2qxxxxxx",
    "EndpointURL": "awseb-e-9-AWSEBLoa-19ZWRROSPJR1G-999999999.us-west-2.elb.amazonaws.com",
    "SolutionStackName": "64bit Amazon Linux 2016.03 v2.1.6 running PHP 7.0",
    "CNAME": "testapp-env-001.us-west-2.elasticbeanstalk.com",
    "Health": "Grey",
    "AbortableOperationInProgress": true,
    "Tier": {
        "Version": " ",
        "Type": "Standard",
        "Name": "WebServer"
    },
    "DateUpdated": "2016-08-29T11:11:18.229Z",
    "DateCreated": "2016-08-29T05:35:31.337Z"
}
$ aws elasticbeanstalk describe-configuration-settings \
  --application-name testapp \
  --environment-name testapp-env-001 \
  --query "ConfigurationSettings[].OptionSettings[?contains(OptionName,\`Notification\`)]"
[
    [
        {
            "OptionName": "Notification Endpoint",
            "Namespace": "aws:elasticbeanstalk:sns:topics"
        },
        {
            "OptionName": "Notification Protocol",
            "Namespace": "aws:elasticbeanstalk:sns:topics",
            "Value": "email"
        },
        {
            "OptionName": "Notification Topic ARN",
            "Namespace": "aws:elasticbeanstalk:sns:topics",
            "Value": "arn:aws:sns:us-west-2:123456789012:cm-test"
        },
        {
            "OptionName": "Notification Topic Name",
            "Namespace": "aws:elasticbeanstalk:sns:topics",
            "Value": "cm-test"
        }
    ]
]

Notification Topic Name削除時の挙動

削除の際も操作方法は同じです。

$ aws elasticbeanstalk update-environment \
  --application-name testapp \
  --environment-name testapp-env-001 \
  --options-to-remove Namespace=aws:elasticbeanstalk:sns:topics,OptionName="Notification Topic Name"
{
    "ApplicationName": "testapp",
    "EnvironmentName": "testapp-env-001",
    "VersionLabel": "Sample Application",
    "Status": "Updating",
    "EnvironmentId": "e-9y2qxxxxxx",
    "EndpointURL": "awseb-e-9-AWSEBLoa-19ZWRROSPJR1G-999999999.us-west-2.elb.amazonaws.com",
    "SolutionStackName": "64bit Amazon Linux 2016.03 v2.1.6 running PHP 7.0",
    "CNAME": "testapp-env-001.us-west-2.elasticbeanstalk.com",
    "Health": "Grey",
    "AbortableOperationInProgress": true,
    "Tier": {
        "Version": " ",
        "Type": "Standard",
        "Name": "WebServer"
    },
    "DateUpdated": "2016-08-29T11:25:24.671Z",
    "DateCreated": "2016-08-29T05:35:31.337Z"
}

$ aws elasticbeanstalk describe-configuration-settings \
  --application-name testapp \
  --environment-name testapp-env-001 \
  --query "ConfigurationSettings[].OptionSettings[?contains(OptionName,\`Notification\`)]"
[
    [
        {
            "OptionName": "Notification Endpoint",
            "Namespace": "aws:elasticbeanstalk:sns:topics"
        },
        {
            "OptionName": "Notification Protocol",
            "Namespace": "aws:elasticbeanstalk:sns:topics",
            "Value": "email"
        },
        {
            "OptionName": "Notification Topic ARN",
            "Namespace": "aws:elasticbeanstalk:sns:topics"
        },
        {
            "OptionName": "Notification Topic Name",
            "Namespace": "aws:elasticbeanstalk:sns:topics"
        }
    ]
]

一見すると同じような挙動に見えますが、大きく異なる点がありました。こちらの設定を削除するとSNS Topicごと削除されてしまいました。ドキュメントに書いてある通りなのですが、直感的に把握できるところではないので注意が必要です。今回はAWS CLIから操作を行いましたが、EB環境とセットで管理するのであれば、.ebextensionsから設定して管理する方針としても良さそうですね。

eb-notification-6

まとめ

簡単に表でまとめてみます。

No. 設定箇所 AMCからの操作 Environment削除時の挙動 備考
1 Notification Endpoint o SNS Topicも削除=EB環境とセットで管理 メールアドレス1件しか登録できない
2 Notification Topic ARN x SNS Topicは残る=EB環境と別で管理 Subscription設定が別途必要
3 Notification Topic Name x SNS Topicも削除=EB環境とセットで管理 Subscription設定が別途必要

EB環境とセットでSNS Topicごと作ったり消したりという場合にはNo.1、No.3を、SNS Topicは別管理としたい場合にはNo.2を使うと良さそうです。動作が気になる方はよく試してみた上で、これらを上手に組み合わせて要件に合わせた設定をご利用いただければと思います。

どこかの誰かのお役に立てば嬉しいです。