SNS(Simple Notification Service)をコマンドラインから設定します。
sns*系のコマンドライン版プログラムをインストールしていない場合は、以前の記事を参考にインストールしておいてください。


・SNS登録の手順/概要
SNSに登場するオブジェクトは3つ。
トピック           - 通知先ごとのグループ
サブスクリプション - 通知先、あるいはメッセージの購読者
メッセージ - 送信されるテキスト、あるいはデータ
SNSでは、まず 通知先ごとのグループとなるトピックを作成し、そこに購読者である通知先を登録(サブスクリプト)します。トピックへの通知先登録時には確認(confirmation)が必要になります。
その後、メッセージをトピックに対して送信すると、各登録者(通知先)にメッセージが送信されます。



・SNS系コマンドの使い方
sns*系コマンドの一覧は、http://docs.aws.amazon.com/sns/latest/cli/command-reference.htmlか、あるいは sns-cmd コマンドで入手できます。

$ sns-cmd
Command Name                       Description
------------                       -----------
help
sns-add-permission                 Add a permission to a topic.
sns-confirm-subscription           Confirm a Subscription.
sns-create-topic                   Create a topic.
sns-delete-topic                   Delete a topic.
sns-get-subscription-attributes    Get subscription attributes.
sns-get-topic-attributes           Get topic attributes.
sns-list-subscriptions             List all subscriptions.
sns-list-subscriptions-by-topic    List subscriptions by topic.
sns-list-topics                    Lists all topics.
sns-publish                        Publish a message to a topic.
sns-remove-permission              Remove permission from a topic.
sns-set-subscription-attributes    Set subscription attributes.
sns-set-topic-attributes           Set topic attributes.
sns-subscribe                      Subscribe to a topic.
sns-unsubscribe                    Unsubscribe from a topic.
version                            Prints the version of the CLI tool and the API.

    For help on a specific command, type '<commandname> --help'

コマンド一覧を見る限り、流れとしては以下のようになると思われます。
sns-create-topic                - トピックの作成
sns-list-topics - トピック一覧の表示

sns-subscribe - トピックの通知先追加を要求
sns-confirm-subscription - トピックへの通知先追加を承認
sns-list-subscriptions - 通知先一覧の表示
sns-list-subscriptions-by-topic - トピックごとの通知先一覧の表示

sns-publish - 通知の送信

sns-unsubscribe - 通知先の削除

sns-delete-topic - トピックの削除
それでは、その手順でやってみましょう。




・トピック作成
sns-create-topic コマンドでトピック名を指定するだけで作成されるらしい。
$ sns-create-topic sample_topic
arn:aws:sns:us-east-1:488224276535:sample_topic
あれ???
us-east-1 って???
環境変数の EC2_URL と EC2_REGION を指定しているハズですが、効果が無いようですね...。

sns-create-topic に --help オプションを与えて説明を見てみると… AWS_REGION と AWS_SNS_URL という環境変数が参照されるようです。メンドクサイので設定しておきましょう。ここでは、既に EC2_URL と EC2_REGION の環境変数が設定してあるので、それを利用して再設定します。マトメるとこんな感じ。
export EC2_REGION='ap-northeast-1'
export EC2_URL=ec2.${EC2_REGION}.amazonaws.com

export AWS_REGION=${EC2_REGION}
export AWS_SNS_URL=http://sns.${AWS_REGION}.amazonaws.com
バッチファイルや .bash_profile / .login にも追記しておくと良いでしょう。
⇒ AWS_SNS_URL は先頭に http:// が無いと正しく認識されないようです。EC2_URL側は 無くてもOKなのに...。

間違って作成したトピックは、↑の環境変数を設定する前に、以下のように削除しておきます。
$ sns-delete-topic \
     --force \
     --topic-arn arn:aws:sns:us-east-1:488224276535:sample_topic

Topic deleted.

$ sns-list-topics
No topics found.
で、トピックの再作成は以下のような感じ。というか、さっきと同じコマンドです。
$ sns-create-topic sample_topic
arn:aws:sns:ap-northeast-1:488224276535:sample_topic

$ sns-list-topics
arn:aws:sns:ap-northeast-1:488224276535:sample_topic
期待通りに Tokyo Region (ap-northeast-1)になってますね。



・サブスクライブ(送信先の登録)
送信先を登録するには sns-subscribe コマンドを使用します。
パラメータには TOPIC_ARN (トピックID)、--protocol オプションで http/https/email/email-json/sqs のいずれか、--endpoint オプションで URL/メールアドレス/SQS_ARN を指定します。
ここでは、「メールを送信する」という設定で登録することにします。
$ sns-subscribe \
     arn:aws:sns:ap-northeast-1:488224276535:sample_topic \
     --protocol email \
     --endpoint 'exploreaws@example.com'

Subscription request received.
sns-subscription コマンドだけでは、登録は完了しません。
sns-subscriptionが 正しく受け付けられると、指定したメールアドレスにメールが届きます。
送信主は no-reply@sns.amazonaws.com で、タイトルが「AWS Notification - Subscription Confirmation」となっているハズです。
メールには URL (リンク)が含まれていますが、ここではクリックはしないでください。
メールに含まれているURLは、以下のようになっています。
https://sns.ap-northeast-1.amazonaws.com/confirmation.html
     ?TopicArn=arn:aws:sns:ap-northeast-1:488224276535:sample_topic
     &Token=~長い文字列~
     &Endpoint=exploreaws@example.com
Token パラメータである ~長い文字列~ の部分がサブスクライブ用トークンなので、コピーしておきます。TopicArn は、先ほど作成したトピックのIDですね。

sns-confirm-subscription  コマンドを使った サブスクライブの確認(confirmation)には、先ほどコピーした サブスクライブ用トークンと TOPIC_ARN が必要です。以下のようにして sns-confirm-subscription コマンドを呼び出します。
$ sns-confirm-subscription \
     arn:aws:sns:ap-northeast-1:488224276535:sample_topic \
     --token サブスクライブ用トークン

arn:aws:sns:ap-northeast-1:488224276535:sample_topic:e39a9b52-d792-481d-a5a6-8e8763951e99
sns-confirm-subscription のレスポンスとして、サブスクリプションARN(Subscription ARN)が表示が表示されます。

登録がホントに完了しているかどうか sns-list-subscriptions と sns-list-subscriptions-by-topic  コマンドで確認してみます。
$ sns-list-subscriptions
arn:aws:sns:ap-northeast-1:488224276535:sample_topic:e39a9b52-d792-481d-a5a6-8e8763951e99  email  exploreaws@example.com


$ sns-list-subscriptions-by-topic \
     arn:aws:sns:ap-northeast-1:488224276535:sample_topic

arn:aws:sns:ap-northeast-1:488224276535:sample_topic:e39a9b52-d792-481d-a5a6-8e8763951e99  email  exploreaws@example.com
登録が1コしかないので良く分かりませんが、一応登録できているのでしょう…。
email の前に表示されているのが、Subscription ARN でしょうね。



・メッセージ送信
sns-publish コマンドで送信します。
本文は --message オプションで指定します。
プロトコルが email-json の場合は --message-structure で指定するらしいです。
更にプロトコルが email の場合、--subject でタイトルを指定できます。
$ sns-publish \
     arn:aws:sns:ap-northeast-1:488224276535:sample_topic \
     --message 'Message body.'

a225674d-2aa5-540c-9db3-2ed850443eee
レスポンスで表示される文字列は message-ID らしいです。
指定されたメールアドレス(ここでは exploreaws@example.com)に…
タイトル「AWS Notification Message」
本文「Message body.」+ 登録解除用URL等
…というメールが届きました。

今度は、タイトルを指定して送信してみましょう。ついでに複数行の送信が出来るかも試してみます。
$ sns-publish \
     arn:aws:sns:ap-northeast-1:488224276535:sample_topic \
     --message 'Message body.
<改行キー>
     Second line.' \
     --subject 'email title'

f9e9c50b-b1d3-50c2-9ab6-8c9ecad42662
この送信方法だと、
タイトル「email title
本文「Message body.
   Second line.」+ 登録解除用URL等
…というメールが届きました。バッチリですね。



・サブスクライブ解除(送信先の登録解除)
サブスクライブ解除には sns-unsubscribe コマンドを使用します。
sns-confirm-subscription あるいは sns-list-subscription コマンドなどで得られる Subscription ARN がパラメータとして必要になります。
$ sns-list-subscriptions
arn:aws:sns:ap-northeast-1:488224276535:sample_topic:e39a9b52-d792-481d-a5a6-8e8763951e99  email  exploreaws@example.com

$ sns-unsubscribe  \
     arn:aws:sns:ap-northeast-1:488224276535:sample_topic:e39a9b52-d792-481d-a5a6-8e8763951e99
Unsubscribed.

$ sns-list-subscriptions
No subscriptions found.
これで、サブスクリプション登録が消えました。



・トピックの削除

最後に残ったトピック「sample_topic」を削除します。
sns-list-topics で TopicARN を取得し、sns-delete-topic に渡すことで削除できます。
ホントに削除してよいかどうか確認するプロンプトが出るので「y」と答えれば、削除が完了します。
$ sns-list-topics
arn:aws:sns:ap-northeast-1:488224276535:sample_topic

$ sns-delete-topic \
     arn:aws:sns:ap-northeast-1:488224276535:sample_topic

    Are you sure you want to delete this topic? [Ny] y
Topic deleted.

$ sns-list-topics
No topics found
確認プロンプトを出したくない場合は --force オプションを使用してください。


これでトピックもバッチリ消えました!



今回はここまで。