LoginSignup
4
3

More than 5 years have passed since last update.

AmazonSNSをCLIからササっと使ってみる(メール+SMS)

Posted at

IAMユーザーを作成

AmazonSNSFullAccessを持たせてIAMユーザーを作成する。
Key IDとSecret Access Keyをメモっておく。

AWSのCLIをインストール

# pipがない人はpipを入れる
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
sudo python get-pip.py

# pip経由でCLIをインストール
sudo pip install awscli

aws configure
# 以下のように設定
# AWS Access Key ID [None]: 先ほど作成したIAMのKey ID
# AWS Secret Access Key [None]: 先ほど作成したIAMのSecret Access Key
# Default region name [None]: ap-northeast-1
# Default output format [None]: json

トピックを作成

AmazonSNSではトピックを購読するという形でメッセージを送ります。
試しにトピックを作ります。

aws sns create-topic --name my_topic

以下のように出力されれば成功です。

{
    "TopicArn": "arn:aws:sns:ap-northeast-1:[AWSアカウントID]:my_topic"
}

メールを送ってみる

まずはメールを送ってみます。

aws sns subscribe \
--topic-arn arn:aws:sns:ap-northeast-1:[AWSアカウントID]:my_topic \
--protocol email \
--notification-endpoint wktq@dev.jp

これで購読確認のメールが送信されます。承認すると、管理画面に購読者が追加されます。
スクリーンショット 2019-04-14 11.24.51.png

試しにメールを送ってみます。

aws sns publish \
--topic-arn arn:aws:sns:ap-northeast-1:[AWSアカウントID]:my_topic \
--subject "Test mail" \
--message "Hello World"

無事に届きました。
スクリーンショット 2019-04-14 11.27.42.png

SMSを送ってみる

aws sns subscribe \
--topic-arn arn:aws:sns:ap-northeast-1:[AWSアカウントID]:my_topic \
--protocol sms \
--notification-endpoint +818012345678
{
    "SubscriptionArn": "arn:aws:sns:ap-northeast-1:[AWSアカウントID]:my_topic:[ユニークARN]"
}

SMSの場合は2段階確認が不要です。おそらく自動的に有効な電話番号かチェックしています。
--target-arnには直接電話番号を入れてもokのようです。

ちゃんと届きました。

aws sns publish \
--target-arn "+818091246940" \
--subject "Test mail" \
--message "Hello World"

IMG_757065D71941-1.jpeg

宝箱は早速アンロックします。

料金

https://aws.amazon.com/jp/sns/sms-pricing/
料金は上記より確認できます。(Japanを検索)

キャリア以外にもHNIとかあってよくわかりませんが、$0.06278~0.09268(7.03円〜10.38円のようです)

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3