AmplifyでSMS認証を導入する際に注意すること!SNSの上限緩和申請に迫る

AmplifyでSMS認証を導入する際に注意すること!SNSの上限緩和申請に迫る

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

こんちには!

Aws Amplify の対話コンソールで電話番号認証を指定した場合には、Aws SNS での SMS 送信が自動構築されます。

SMS送信は、Aws SNS によって実行されますが、この Aws SNS が構築初期時の設定では送信可能上限が 1USD /月 となっているので、開発初期で速攻で使えなくなります。

Aws Amplify での SMS 認証導入方法から、その後の SNS の上限緩和申請を紹介します。

amplfiy add authで認証機能を追加

まずは、Amplify initでAmplify プロジェクトを構築します。

$ amplify init
Note: It is recommended to run this command from the root of your app directory
? Enter a name for the project sample-20200328
? Enter a name for the environment dev
? Choose your default editor: None
? Choose the type of app that you're building javascript
Please tell us about your project
? What javascript framework are you using none
? Source Directory Path:  src
? Distribution Directory Path: dist
? Build Command:  npm run-script build
? Start Command: npm run-script start
Using default provider  awscloudformation

For more information on AWS Profiles, see:
https://docs.aws.amazon.com/cli/latest/userguide/cli-multiple-profiles.html

? Do you want to use an AWS profile? Yes
? Please choose the profile you want to use default
Adding backend environment dev to AWS Amplify Console app: di3hfb43dqb95
⠸ Initializing project in the cloud...

...

Your project has been successfully initialized and connected to the cloud!

では、Amplify へ認証を導入しましょう。

今回は、電話番号属性とログイン時のIDとし、サインアップ時に電話番号の認証を必要とします。

$amplify add auth
Using service: Cognito, provided by: awscloudformation
 
 The current configured provider is Amazon Cognito. 
 
 Do you want to use the default authentication and security configuration? Manual configuration
 Select the authentication/authorization services that you want to use: User Sign-Up, Sign-In, connected with AWS IAM controls (Enables per-user Storage features for images or other content, Analytics, and more)
 Please provide a friendly name for your resource that will be used to label this category in the project: sample20200328
 Please enter a name for your identity pool. sample_202003288ad28725_identitypool_8ad28725
 Allow unauthenticated logins? (Provides scoped down permissions that you can control via AWS IAM) No
 Do you want to enable 3rd party authentication providers in your identity pool? No
 Please provide a name for your user pool: sample_202003288ad28725_userpool_8ad28725
 Warning: you will not be able to edit these selections. 
 How do you want users to be able to sign in? Phone Number
 Do you want to add User Pool Groups? Yes
? Provide a name for your user pool group: customer
? Do you want to add another User Pool Group No
✔ Sort the user pool groups in order of preference · customer
 Do you want to add an admin queries API? No
 Multifactor authentication (MFA) user login options: OFF
 Email based user registration/forgot password: Disabled (Uses SMS/TOTP as an alternative)
 Please specify an SMS verification message: Your verification code is {####}
 Do you want to override the default password policy for this User Pool? No
 Warning: you will not be able to edit these selections. 
 What attributes are required for signing up? Phone Number (This attribute is not supported by Facebook, Login With Amazon.)
 Specify the app's refresh token expiration period (in days): 30
 Do you want to specify the user attributes this app can read and write? No
 Do you want to enable any of the following capabilities? 
 Do you want to use an OAuth flow? Yes
 What domain name prefix you want us to create for you? sample202003288ad28725-8ad28725
 Enter your redirect signin URI: https://blog.ragate.co.jp:3000/signin/
? Do you want to add another redirect signin URI No
 Enter your redirect signout URI: https://blog.ragate.co.jp:3000/signout/
? Do you want to add another redirect signout URI No
 Select the OAuth flows enabled for this project. Implicit grant
 Select the OAuth scopes enabled for this project. Phone, Email, OpenID, Profile, aws.cognito.signin.user.admin
 Select the social providers you want to configure for your user pool: 
? Do you want to configure Lambda Triggers for Cognito? No
Successfully added resource sample20200328 locally

Some next steps:
"amplify push" will build all your local backend resources and provision it in the cloud
"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud

$ amplify push
....
success !

引用:

Email based user registration/forgot password: Disabled (Uses SMS/TOTP as an alternative)

これで電話番号の認証機能が構築されました。あっという間に認証基盤が出来上がるので楽チンですね。

もちろん、この auth を作成する段階で細かな設定をすれば色々な権限管理が可能ですので、与件に応じて回答する内容を変更してみてください。(例えばグループを作成してグループで Aws サービスの利用に制限を付与するなど)

構築された Aws SNS を見てみよう

Aws の管理コンソールより Aws SNS を表示し「テキストメッセージング (SMS)」を選択しましょう。

Aws SNS モバイルテキストメッセージング (SMS)

この中で、「テキストメッセージングの優先設定」という箇所を見てみると、「アカウントの使用制限」という項目があります。

ここが構築初期時では、1となっており、つまり初期構築段階では1ドル分しか SMS メッセージを送信ができないといった様子になります。

編集というボタンから詳しく表示してみましょう。

テキストメッセージングの優先設定の編集

アカウントの使用制限が、「1 USD/月」となっているのがその証拠です。

そして、上限緩和申請をしないとその値は変更できないのが落とし穴です。(まぁ確かに上限を厳格に設けないとアプリが不正利用で使われまくった時に請求額がとんでも無いことになるので、正しいって上ば正しいのかもしれません)

では、次にいよいよ本題の「SMSテキストメッセージ送信上限緩和申請」を行います。

SNS の SMS メッセージ送信上限緩和申請

AWS サポートページの表示

管理コンソールの右上のサポートというプルダウンより、サポートセンターをクリックします。

Aws サポートセンターの開き方

ケースの作成

Aws のサポートへ問い合わせするケースを作成します。

その後、内容を下記のように入力します。

ケースの入力内容

入力内容:

申請理由の説明: サービス公開にあたり、SES の SMS 送信上限を、1000ドルに設定したいです。
厳しければ、500ドルでもかまいません。ご検討のほどお願い申し上げます。

実際に許可されたのは、500ドルでした笑

翌日、Awsからの返答が!

500ドルを許可するようなメッセージが来ましたので、内容を紹介しておきます。

Awsの返信内容

平素は Amazon Web Services をご利用いただき、誠にありがとうございます。

新しい SMS の月額使用限度額 $500 USD が適用されました。

この処理がコンソールに反映されるまでに最大 1 時間かかる場合があります。 現在より大きな額を申請していただきましたが、まずは現在の使用限度額を利用して、メッセージの受信対象者に高品質の SMS メッセージを送信してください。

後日、お客様のリクエストを再検討させていただきますので、新しい使用限度額リクエストを作成してください。

SMS メッセージの送信における各国の現在のキャリア料金については、ワールドワイド SMS 料金ページをご覧ください。 メッセージを送信する前に、Amazon SNS コンソールまたは API を使用してアカウントの使用限度額を更新する必要があります。

これらの手順を完了すると、デフォルトの限度額が 1.00 USD であると記載されたメッセージが表示されることがあります。このメッセージは無視できます。 CloudWatch を使用して Amazon SNS のメトリクスをモニタリングすることをお勧めします。

Amazon SES の使用を開始する際は、以下のことをお勧めします。

— 必要となる前に、使用限度額の引き上げを申し込みます。

— 必要に応じて、AWS サポートセンターでケースを作成し、他の SMS オプションをリクエストします。

— ベストプラクティスについては、SMS メッセージの送信に関する追加情報をご覧ください。

まとめ

実運用する際に1ドルでは現実的では無いので、上限緩和申請は必須だと感じます。サポートの返答は運よく1日で来ましたが、これにどの程度時間がかかるかは正直その時々で変わると思います。

早めに申請しておかないと、本番ローンチ直前に「あっ!!」なんてこともありえますね。要注意です。

サーバーレス開発については、お気軽にお問い合わせください。