こんにちは!

もうすぐ年末ですね。

2019年はAmplifyの年と言えるほど、Amplifyしていました。

電話番号認証を実装したのでシェアします。

環境

  • Nodejs v10.16.3
  • Aws Amplify CLI 3.17.0

処理の流れ

  1. amplify init
  2. amplify add auth
  3. amplify push
  4. SIgnUp / SMSコードの送信
  5. SMSコードの検証

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
? 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 vue
? Source Directory Path:  src
? Distribution Directory Path: dist
? Build Command:  npm run-script build
? Start Command: npm run-script serve
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
⠹ Initializing project in the cloud...

CREATE_IN_PROGRESS amplify-sample-dev-120720 AWS::CloudFormation::Stack Sun Dec 22 2019 12:07:21 GMT+0900 (Japan Standard Time) User Initiated             
CREATE_IN_PROGRESS UnauthRole                AWS::IAM::Role             Sun Dec 22 2019 12:07:25 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS AuthRole                  AWS::IAM::Role             Sun Dec 22 2019 12:07:25 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS DeploymentBucket          AWS::S3::Bucket            Sun Dec 22 2019 12:07:25 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS UnauthRole                AWS::IAM::Role             Sun Dec 22 2019 12:07:25 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS AuthRole                  AWS::IAM::Role             Sun Dec 22 2019 12:07:26 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠇ Initializing project in the cloud...

CREATE_IN_PROGRESS DeploymentBucket AWS::S3::Bucket Sun Dec 22 2019 12:07:27 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠼ Initializing project in the cloud...

CREATE_COMPLETE UnauthRole AWS::IAM::Role Sun Dec 22 2019 12:07:41GMT+0900 (Japan Standard Time) 
CREATE_COMPLETE AuthRole   AWS::IAM::Role Sun Dec 22 2019 12:07:41 GMT+0900 (Japan Standard Time) 
⠴ Initializing project in the cloud...

CREATE_COMPLETE DeploymentBucket          AWS::S3::Bucket            Sun Dec 22 2019 12:07:47 GMT+0900 (Japan Standard Time) 
CREATE_COMPLETE amplify-sample-dev-120720 AWS::CloudFormation::Stack Sun Dec 22 2019 12:07:50 GMT+0900 (Japan Standard Time) 
✔ Successfully created initial AWS cloud resources for deployments.
✔ Initialized provider successfully.
Initialized your environment successfully.

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

Some next steps:
"amplify status" will show you what you've added already and if it's locally configured or deployed
"amplify <category> add" will allow you to add features like user login or a backend API
"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

Pro tip:
Try "amplify add api" to create a backend API and then "amplify publish" to deploy everything

amplify add auth

ここで、デフォルトの設定はメールアドレスに認証コード送信設定になっているので、SMS
で送信するようにします。

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

$  sample 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: samplexxxxxx
 Please enter a name for your identity pool. samplexxxxxx_identitypool_xxxxxx
 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? Yes
 Select the third party identity providers you want to configure for your identity pool: (Press <space> to select, <a> to toggle all, <i> to invert selection)
 Please provide a name for your user pool: sampleacxxxxxx_userpool_xxxxxx
 Warning: you will not be able to edit these selections. 
 How do you want users to be able to sign in? Username
 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? (Press <space> to select, <a> to toggle all, <i> to invert selection)Email
 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? (Press <space> to select, <a> to toggle all, <i> to invert selection)
 Do you want to use an OAuth flow? Yes
 What domain name prefix you want us to create for you? sampleacxxxxxx
 Enter your redirect signin URI: https://example.com/
? Do you want to add another redirect signin URI No
 Enter your redirect signout URI: https://example.com/
? Do you want to add another redirect signout URI No
 Select the OAuth flows enabled for this project. Authorization code grant
 Select the OAuth scopes enabled for this project. (Press <space> to select, <a> to toggle all, <i> to invert selection)Phone, Email, OpenID, Profile, aws.cognito.signin.user.admin
 Select the social providers you want to configure for your user pool: (Press <space> to select, <a> to toggle all, <i> to invert selection)
? Do you want to configure Lambda Triggers for Cognito? No
Successfully added resource samplexxxxxx 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

SMSの送信はAWSデフォルトでは送信上限が1$となっている

AWSサポートより事前に送信緩和申請を出しておきましょう。開発もできない状態となってしまいます。

amplify push

amplify pushは、amplify statusで表示された差分をクラウド上へデプロイしてくれます。勝手にGit的なファイルの変更を追跡してくれるので、便利ですね。amplifyディレクトリ以下をみているっぽいです。

$ sample amplify push 

Current Environment: dev

| Category | Resource name          | Operation | Provider plugin   |
| -------- | ---------------------- | --------- | ----------------- |
| Auth     | userPoolGroups         | Create    | awscloudformation |
| Auth     | samplexxxxxxx | Create    | awscloudformation |
? Are you sure you want to continue? Yes
⠏ Updating resources in the cloud. This may take a few minutes...

UPDATE_IN_PROGRESS amplify-sample-dev-120720      AWS::CloudFormation::Stack Sun Dec 22 2019 12:15:38 GMT+0900 (Japan Standard Time) User Initiated
CREATE_IN_PROGRESS UpdateRolesWithIDPFunctionRole AWS::IAM::Role             Sun Dec 22 2019 12:15:43 GMT+0900 (Japan Standard Time)               
⠧ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS authsamplexxxxxxx AWS::CloudFormation::Stack Sun Dec 22 2019 12:15:43 GMT+0900 (Japan Standard Time) 
⠇ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS UpdateRolesWithIDPFunctionRole AWS::IAM::Role             Sun Dec 22 2019 12:15:44 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS authsamplexxxxxxx     AWS::CloudFormation::Stack Sun Dec 22 2019 12:15:45 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠹ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS amplify-sample-dev-120720-authsamplexxxxxxxAWS::CloudFormation::Stack Sun Dec 22 2019 12:15:45 GMT+0900 (Japan Standard Time) User Initiated
⠼ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS SNSRole AWS::IAM::Role Sun Dec 22 2019 12:15:57 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS SNSRole AWS::IAM::Role Sun Dec 22 2019 12:15:58 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠋ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE UpdateRolesWithIDPFunctionRole AWS::IAM::Role Sun Dec 22 2019 12:16:01 GMT+0900 (Japan Standard Time) 
⠦ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE SNSRole AWS::IAM::Role Sun Dec 22 2019 12:16:14 GMT+0900 (Japan Standard Time) 
⠦ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS UserPool AWS::Cognito::UserPool Sun Dec 22 2019 12:16:20 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS UserPool AWS::Cognito::UserPool Sun Dec 22 2019 12:16:23 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_COMPLETE    UserPool AWS::Cognito::UserPool Sun Dec 22 2019 12:16:23 GMT+0900 (Japan Standard Time)                            
⠧ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS UserPoolClient    AWS::Cognito::UserPoolClient Sun Dec 22 2019 12:16:26 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS UserPoolClientWeb AWS::Cognito::UserPoolClient Sun Dec 22 2019 12:16:27 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS UserPoolClient    AWS::Cognito::UserPoolClient Sun Dec 22 2019 12:16:28 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠦ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE    UserPoolClient    AWS::Cognito::UserPoolClient Sun Dec 22 2019 12:16:28 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS UserPoolClientWeb AWS::Cognito::UserPoolClient Sun Dec 22 2019 12:16:29 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_COMPLETE    UserPoolClientWeb AWS::Cognito::UserPoolClient Sun Dec 22 2019 12:16:29 GMT+0900 (Japan Standard Time)                            
⠇ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS UserPoolClientRole AWS::IAM::Role Sun Dec 22 2019 12:16:32 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS UserPoolClientRole AWS::IAM::Role Sun Dec 22 2019 12:16:33 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠋ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE UserPoolClientRole AWS::IAM::Role Sun Dec 22 2019 12:16:48 GMT+0900 (Japan Standard Time) 
⠦ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS HostedUIProvidersCustomResource AWS::Lambda::Function Sun Dec 22 2019 12:16:52 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS UserPoolClientLambda            AWS::Lambda::Function Sun Dec 22 2019 12:16:52 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS HostedUICustomResource          AWS::Lambda::Function Sun Dec 22 2019 12:16:53 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS HostedUIProvidersCustomResource AWS::Lambda::Function Sun Dec 22 2019 12:16:53 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS UserPoolClientLambda            AWS::Lambda::Function Sun Dec 22 2019 12:16:53 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS HostedUICustomResource          AWS::Lambda::Function Sun Dec 22 2019 12:16:53 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_COMPLETE    HostedUIProvidersCustomResource AWS::Lambda::Function Sun Dec 22 2019 12:16:53 GMT+0900 (Japan Standard Time)                            
CREATE_COMPLETE    UserPoolClientLambda            AWS::Lambda::Function Sun Dec 22 2019 12:16:54 GMT+0900 (Japan Standard Time)                            
⠹ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE HostedUICustomResource AWS::Lambda::Function Sun Dec 22 2019 12:16:54 GMT+0900 (Japan Standard Time) 
⠦ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS UserPoolClientLambdaPolicy            AWS::IAM::Policy Sun Dec 22 2019 12:16:58 GMT+0900 (Japan Standard Time) 
CREATE_IN_PROGRESS HostedUIProvidersCustomResourcePolicy AWS::IAM::Policy Sun Dec 22 2019 12:16:58 GMT+0900 (Japan Standard Time) 
CREATE_IN_PROGRESS HostedUICustomResourcePolicy          AWS::IAM::Policy Sun Dec 22 2019 12:16:58 GMT+0900 (Japan Standard Time) 
⠙ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS UserPoolClientLambdaPolicy            AWS::IAM::Policy Sun Dec 22 2019 12:16:59 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS HostedUIProvidersCustomResourcePolicy AWS::IAM::Policy Sun Dec 22 2019 12:16:59 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠦ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS HostedUICustomResourcePolicy AWS::IAM::Policy Sun Dec 22 2019 12:17:00 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠸ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE HostedUIProvidersCustomResourcePolicy AWS::IAM::Policy Sun Dec 22 2019 12:17:13 GMT+0900 (Japan Standard Time) 
CREATE_COMPLETE UserPoolClientLambdaPolicy            AWS::IAM::Policy Sun Dec 22 2019 12:17:13 GMT+0900 (Japan Standard Time) 
⠦ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE    HostedUICustomResourcePolicy             AWS::IAM::Policy Sun Dec 22 2019 12:17:14 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS HostedUIProvidersCustomResourceLogPolicy AWS::IAM::Policy Sun Dec 22 2019 12:17:17 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS UserPoolClientLogPolicy                  AWS::IAM::Policy Sun Dec 22 2019 12:17:17 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS HostedUICustomResourceLogPolicy          AWS::IAM::Policy Sun Dec 22 2019 12:17:18 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS HostedUIProvidersCustomResourceLogPolicy AWS::IAM::Policy Sun Dec 22 2019 12:17:18 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠙ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS UserPoolClientLogPolicy AWS::IAM::Policy Sun Dec 22 2019 12:17:19 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠼ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS HostedUICustomResourceLogPolicy AWS::IAM::Policy Sun Dec 22 2019 12:17:19 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠧ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE HostedUIProvidersCustomResourceLogPolicy AWS::IAM::Policy Sun Dec 22 2019 12:17:32 GMT+0900 (Japan Standard Time) 
CREATE_COMPLETE UserPoolClientLogPolicy                  AWS::IAM::Policy Sun Dec 22 2019 12:17:33 GMT+0900 (Japan Standard Time) 
⠇ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE HostedUICustomResourceLogPolicy AWS::IAM::Policy Sun Dec 22 2019 12:17:34 GMT+0900 (Japan Standard Time) 
⠏ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS HostedUIProvidersCustomResourceInputs Custom::LambdaCallout Sun Dec 22 2019 12:17:37 GMT+0900 (Japan Standard Time) 
CREATE_IN_PROGRESS UserPoolClientInputs                  Custom::LambdaCallout Sun Dec 22 2019 12:17:37 GMT+0900 (Japan Standard Time) 
CREATE_IN_PROGRESS HostedUICustomResourceInputs          Custom::LambdaCallout Sun Dec 22 2019 12:17:37 GMT+0900 (Japan Standard Time) 
⠧ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS HostedUIProvidersCustomResourceInputs Custom::LambdaCallout Sun Dec 22 2019 12:17:41 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS UserPoolClientInputs                  Custom::LambdaCallout Sun Dec 22 2019 12:17:41 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_COMPLETE    HostedUIProvidersCustomResourceInputs Custom::LambdaCallout Sun Dec 22 2019 12:17:41 GMT+0900 (Japan Standard Time)                            
CREATE_COMPLETE    UserPoolClientInputs                  Custom::LambdaCallout Sun Dec 22 2019 12:17:41 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS HostedUICustomResourceInputs          Custom::LambdaCallout Sun Dec 22 2019 12:17:42 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_COMPLETE    HostedUICustomResourceInputs          Custom::LambdaCallout Sun Dec 22 2019 12:17:43 GMT+0900 (Japan Standard Time)                            
⠏ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS IdentityPool        AWS::Cognito::IdentityPool Sun Dec 22 2019 12:17:45 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS OAuthCustomResource AWS::Lambda::Function      Sun Dec 22 2019 12:17:46 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS OAuthCustomResource AWS::Lambda::Function      Sun Dec 22 2019 12:17:46 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_COMPLETE    OAuthCustomResource AWS::Lambda::Function      Sun Dec 22 2019 12:17:47 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS IdentityPool        AWS::Cognito::IdentityPool Sun Dec 22 2019 12:17:47 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_COMPLETE    IdentityPool        AWS::Cognito::IdentityPool Sun Dec 22 2019 12:17:48 GMT+0900 (Japan Standard Time)                            
⠸ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS OAuthCustomResourcePolicy AWS::IAM::Policy                         Sun Dec 22 2019 12:17:51 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS IdentityPoolRoleMap       AWS::Cognito::IdentityPoolRoleAttachment Sun Dec 22 2019 12:17:51 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS OAuthCustomResourcePolicy AWS::IAM::Policy                         Sun Dec 22 2019 12:17:52 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS IdentityPoolRoleMap       AWS::Cognito::IdentityPoolRoleAttachment Sun Dec 22 2019 12:17:53 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠧ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE IdentityPoolRoleMap AWS::Cognito::IdentityPoolRoleAttachment Sun Dec 22 2019 12:17:53 GMT+0900 (Japan Standard Time) 
⠋ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE OAuthCustomResourcePolicy AWS::IAM::Policy Sun Dec 22 2019 12:18:06 GMT+0900 (Japan Standard Time) 
⠸ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS OAuthCustomResourceLogPolicy AWS::IAM::Policy Sun Dec 22 2019 12:18:10 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS OAuthCustomResourceLogPolicy AWS::IAM::Policy Sun Dec 22 2019 12:18:12 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠸ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE OAuthCustomResourceLogPolicy AWS::IAM::Policy Sun Dec 22 2019 12:18:26 GMT+0900 (Japan Standard Time) 
⠹ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS OAuthCustomResourceInputs Custom::LambdaCallout Sun Dec 22 2019 12:18:30 GMT+0900 (Japan Standard Time) 
⠏ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS OAuthCustomResourceInputs                                         Custom::LambdaCallout      Sun Dec 22 2019 12:18:34 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_COMPLETE    OAuthCustomResourceInputs                                         Custom::LambdaCallout      Sun Dec 22 2019 12:18:34 GMT+0900 (Japan Standard Time)                            
CREATE_COMPLETE    amplify-sample-dev-120720-authsamplexxxxxxx AWS::CloudFormation::Stack Sun Dec 22 2019 12:18:38 GMT+0900 (Japan Standard Time)                            
⠙ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE    authsamplexxxxxxx AWS::CloudFormation::Stack Sun Dec 22 2019 12:18:50 GMT+0900 (Japan Standard Time) 
CREATE_IN_PROGRESS UpdateRolesWithIDPFunction AWS::Lambda::Function      Sun Dec 22 2019 12:18:53 GMT+0900 (Japan Standard Time) 
⠧ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS UpdateRolesWithIDPFunction AWS::Lambda::Function Sun Dec 22 2019 12:18:53 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠙ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS authuserPoolGroups                AWS::CloudFormation::Stack Sun Dec 22 2019 12:18:54 GMT+0900 (Japan Standard Time)                            
CREATE_COMPLETE    UpdateRolesWithIDPFunction        AWS::Lambda::Function      Sun Dec 22 2019 12:18:54 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS authuserPoolGroups                AWS::CloudFormation::Stack Sun Dec 22 2019 12:18:55 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS UpdateRolesWithIDPFunctionOutputs Custom::LambdaCallout      Sun Dec 22 2019 12:18:57 GMT+0900 (Japan Standard Time)                            
⠦ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS amplify-sample-dev-xxxxxxx-authuserPoolGroups-xxxxxxxAWS::CloudFormation::Stack Sun Dec 22 2019 12:18:55 GMT+0900 (Japan Standard Time) User Initiated
⠹ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS UpdateRolesWithIDPFunctionOutputs Custom::LambdaCallout Sun Dec 22 2019 12:19:01 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_COMPLETE    UpdateRolesWithIDPFunctionOutputs Custom::LambdaCallout Sun Dec 22 2019 12:19:02 GMT+0900 (Japan Standard Time)                            
⠇ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS customerGroupRole   AWS::IAM::Role Sun Dec 22 2019 12:18:59 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS LambdaExecutionRole AWS::IAM::Role Sun Dec 22 2019 12:19:00 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS LambdaExecutionRole AWS::IAM::Role Sun Dec 22 2019 12:19:01 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS customerGroupRole   AWS::IAM::Role Sun Dec 22 2019 12:19:01 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠋ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE customerGroupRole AWS::IAM::Role Sun Dec 22 2019 12:19:16 GMT+0900 (Japan Standard Time) 
⠹ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS customerGroup       AWS::Cognito::UserPoolGroup Sun Dec 22 2019 12:19:19 GMT+0900 (Japan Standard Time)                            
CREATE_COMPLETE    LambdaExecutionRole AWS::IAM::Role              Sun Dec 22 2019 12:19:20 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS customerGroup       AWS::Cognito::UserPoolGroup Sun Dec 22 2019 12:19:21 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_COMPLETE    customerGroup       AWS::Cognito::UserPoolGroup Sun Dec 22 2019 12:19:21 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS RoleMapFunction     AWS::Lambda::Function       Sun Dec 22 2019 12:19:23 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS RoleMapFunction     AWS::Lambda::Function       Sun Dec 22 2019 12:19:23 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠇ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE RoleMapFunction AWS::Lambda::Function Sun Dec 22 2019 12:19:24 GMT+0900 (Japan Standard Time) 
⠋ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS RoleMapFunctionInput Custom::LambdaCallout Sun Dec 22 2019 12:19:26 GMT+0900 (Japan Standard Time) 
⠙ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS RoleMapFunctionInput                                       Custom::LambdaCallout      Sun Dec 22 2019 12:19:29 GMT+0900 (Japan Standard Time) Resource creation Initiated
CREATE_COMPLETE    RoleMapFunctionInput                                       Custom::LambdaCallout      Sun Dec 22 2019 12:19:30 GMT+0900 (Japan Standard Time)                            
CREATE_COMPLETE    amplify-sample-dev-xxxxxxx-authuserPoolGroups-xxxxxxx AWS::CloudFormation::Stack Sun Dec 22 2019 12:19:32 GMT+0900 (Japan Standard Time)                            
⠦ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE authuserPoolGroups AWS::CloudFormation::Stack Sun Dec 22 2019 12:19:42 GMT+0900 (Japan Standard Time) 
⠏ Updating resources in the cloud. This may take a few minutes...

UPDATE_COMPLETE_CLEANUP_IN_PROGRESS amplify-sample-dev-120720 AWS::CloudFormation::Stack Sun Dec 22 2019 12:19:45 GMT+0900 (Japan Standard Time) 
UPDATE_COMPLETE                     amplify-sample-dev-120720 AWS::CloudFormation::Stack Sun Dec 22 2019 12:19:46 GMT+0900 (Japan Standard Time) 
✔ All resources are updated in the cloud

Hosted UI Endpoint: https://sampleac302e93-ac302e93-dev.auth.ap-northeast-1.amazoncognito.com/
Test Your Hosted UI Endpoint: https://sampleac302e93-ac302e93-dev.auth.ap-northeast-1.amazoncognito.com/login?response_type=code&client_id=3ad7h9ulvb6uqrmop29h7qrpvk&redirect_uri=https://example.com/

SIgnUp / SMSコードの送信

ではいよいよ、実際に電話番号でSignUpさせてみる。下記を実行すると指定の電話番号へ認証コードが送信されてくる。

import {Auth, API, graphqlOperation} from 'aws-amplify'
import _ from 'lodash'

async verifyPhoneNumber() {
        const next = async () => {
          // SignUp後の処理(パスコード入力画面へ遷移を想定)
        }
        if (_.isUndefined(this.phoneNumber) || _.isUndefined(_.isEmpty(this.phoneNumber))) return
        const userName = this.getUserName()
        const initPassword = this.initialPassword
        try {
          await Auth.signIn(this.getUserName(), this.initialPassword)
          / 以前に一度認証している場合はここ以降に記述
        } catch (e) {
          const errCode = e.code
          // 電話番号の登録がされていない
          if (errCode === 'UserNotFoundException') {
            try {
              await Auth.signUp({
                username: userName,
                password: initPassword,
                validationData: []  //optional
              }).then(() => next())
            } catch (e) {
              const errCode = e.code
              if (errCode === 'InvalidParameterException') {
                // 電話番号形式じゃないのにSignUp実行するとThrowされる(入力段階で電話番号形式を保証すること)
              }
            }
          }
          // SMSコードでの電話番号検証が済んでいない
          else if (errCode === 'UserNotConfirmedException') {
            await Auth.resendSignUp(this.getUserName()).then(() => next()).catch(e => {
              const errCode = e.code
              // 一定時間でのSMSコードの送信可能数を超えた
              if (errCode === 'LimitExceededException') {
              }
            })
          }
          // 電話番号は登録済みだが初期パスワードが既に変更されている(以前に一度認証を完了している)
          else if (errCode === 'NotAuthorizedException') {
          }
        }
      }

SMSコードの検証

最後に送信された認証コードを検証する。

async confirmVerifyCode() {
        const next = () => {
          // 認証コード疎通後の処理
        }
        if (_.isUndefined(this.smsVerifyCode) || _.isUndefined(_.isEmpty(this.smsVerifyCode))) {
          return
        }
        const userName = this.getUserName()
        const op = {
          forceAliasCreation: true
        }
        await Auth.confirmSignUp(userName, this.smsVerifyCode, op).then(() => next()).catch(e => {
          const errCode = e.code
          this.isModalLoading = false
          if (errCode === 'CodeMismatchException') {
            // 入力された認証コードは誤っている
          }
        })
      },

本来認証処理を実装するにはサーバサイドでパスコードの有効期限の監視やその他いろんな実装が必要になりますが、設問に答えてSDK呼びだけなので、かなり楽ですね。Amplify、感動です。