[基本操作]Amazon SNSをAWS CLIで操作する

2017.11.29

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

こんにちは。池田です。Amazon Echoの置き場所は確保しましたが、肝心の招待メールは今日も届いていません。

はじめに

本記事は2011年08月30日に公開された記事Amazon SNSをコマンドラインから使うと、Amazon SNSとAuto Scalingを組み合わせるを現在(2017年11月)も同じ方法で実行できるのかを確認しつつ、詳細手順を記録したものです。まだAmazon SNSAWS Command Line Interfaceをご利用されたことがない方を対象としております。予めご了承ください。 *Amazon SNSについては[基本操作]Amazon SNSでメールを送信する もご覧ください。

Amazon Command Line Interface(awscli)とは

Amazon Command Line Interface (awscli)はWEBブラウザを介さずにコマンドライン操作でAWSリソースを扱うことができるツールです。

awscliのインストール(MacOS編)

まずはawscliをインストールするための環境が整っているか確認します。

command: python --version
Python 2.7.10

PythonはMacOSデフォルトのもので問題ありません。

command: which pip
command:

awscliをコマンドラインでインストールするためのpipがないのでインストールします。

command: sudo easy_install pip
Password:
...
Finished processing dependencies for pip

command: which pip
/usr/local/bin/pip

いよいよawscliをインストールします。

command: pip install awscli
...
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/docutils'
command: sudo pip install awscli
...
OSError: [Errno 1] Operation not permitted: '/tmp/pip-eVuWYz-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'
command:

エラーが発生しました。こんなときは迷わずDevelopers.IOで先人の知恵を借ります。

【Tips】Mac OS X El CapitanでpipでのAWS CLIのインストール/アップデートが失敗するときの対処法

この記事にも掲載されているとおり、既にインストールされているsixの影響を受けているようですので、インストールオプションを指定します。

command: sudo pip install awscli --upgrade --ignore-installed six

command: pip -V
pip 9.0.1 from /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7)
command: aws --version
aws-cli/1.11.188 Python/2.7.10 Darwin/16.7.0 botocore/1.7.46

無事にawscliがインストールできました。 続いてawscliを使うための初期設定を行っておきます。

command: aws configure

(このコマンドの詳細については公式サイトでご確認ください

コマンドの確認

無事にawscliがインストールできましたので、2011年の記事と同じsns-cmdコマンドが使えるか確かめてみます。

command: sns-cmd
-bash: sns-cmd: command not found

どうやら同じコマンドはなくなっているようですので、awsコマンドのヘルプで確かめることにします。

command: aws help

ないですね...

command: aws sns help
NAME
sns -

DESCRIPTION
Amazon Simple Notification Service (Amazon SNS) is a web service that
enables you to build distributed web-enabled applications. Applications
can use Amazon SNS to easily push real-time notification messages to
interested subscribers over multiple delivery protocols. For more
information about this product see http://aws.amazon.com/sns . For
detailed information about Amazon SNS features and their associated API
calls, see the Amazon SNS Developer Guide .
(省略)

ありました。sns-cmdというコマンドは2017年11月現在aws snsというコマンドに置き換わっているようです。 他のコマンドも当然置き換わっていることが推測されますので、それぞれ確かめておきます(内容の掲載は省略します)。

・トピックの作成に関するコマンドの確認

command: aws sns create-topic help

・サブスクライブの設定に関するコマンドの確認

command: aws sns subscribe help

・パブリッシュ操作に関するコマンドの確認

command: aws sns pablish help

トピックの作成からメールの送信まで

各操作コマンドを確認しましたので、以前(2011年8月)のコマンドと現在(2017年11月)のコマンドを比較しながら、それぞれの操作を行なっていきます。

・トピック作成 以前のコマンド: sns-create-topic mytopic1 --region ap-northeast-1 現在のコマンド: aws sns create-topic --name my-topic

command: aws sns create-topic --name topic_02
{
"TopicArn": "arn:aws:sns:ap-northeast-1:986936546233:topic_02"
}

マネジメントコンソールで確かめます。 topic_02 が表示されています。

・サブスクライブ作成 以前のコマンド: sns-subscribe arn:aws:sns:ap-northeast-1:771293814336:mytopic1 --protocol email --endpoint XXXXXXXX@gmail.com --region ap-northeast-1 現在のコマンド: aws sns subscribe --topic-arn arn:aws:sns:us-west-2:0123456789012:my-topic --protocol email --notification-endpoint my-email@example.com

aws sns subscribe --topic-arn arn:aws:sns:ap-northeast-1:hogehoge:topic_02 --protocol email --notification-endpoint mailto@example.com
{
"SubscriptionArn": "pending confirmation"
}

コマンド内で指定したメールアドレスに確認のメールが届いていますので「Confirrm subscription」をクリックし承認します。 承認後、マネジメントコンソールを確認すると以下のようになっているはずです。

・パブリッシュ操作 以前のコマンド: sns-publish arn:aws:sns:ap-northeast-1:771293814336:mytopic1 --message "hello" --region ap-northeast-1 現在のコマンド: aws sns publish --topic-arn "arn:aws:sns:us-west-2:0123456789012:my-topic" --message file://message.txt

aws sns publish --topic-arn "arn:aws:sns:ap-northeast-1:hogehoge:topic_02" --message "test publish message1"
{
"MessageId": "hogefuga-102c-5362-ar4b-hoghogefuga"
}

topic_02 でのメールが届きました。

どのコマンドもaws configureでデフォルトに指定したリージョンを使ってくれるので、各コマンドの主目的となるものを最後に入力するようになっています。今、何をしようとしているのかをコマンド入力の最初と最後で確かめられるので、間違いを防止できますね。

まとめ

本記事の作成を始めた当初は、冒頭の2つの過去記事で紹介している内容をひとつの記事でまとめようと考えていたのですが、思いのほか長くなってしまいましたので素直に2つの記事にわけてご紹介することにします。 次回はELB配下にEC2インスタンスを構築し、インスタンスの状態を変更させた際にメール通知をさせてみます。