Amplify CLIでCognito UserPoolsのトリガー関数を実装する

そういえばできるのかなと思ったのでちょっと調べてみました。

作成済みの場合はamplify auth update

作成済みであれば、amplify auth updatewalkthrough all the auth configurationsを選びます。

$ amplify auth update
Using service: Cognito, provided by: awscloudformation
  What do you want to do? Walkthrough all the auth configurations

いろいろ項目の確認を進めていく中で、カスタム認証を付けたい場合は、この項目で設定しておきましょう。

$ Do you want to enable any of the following capabilities? (Press  to select,  to toggle all,  to invert 
 selection)
  ◯ Add Google reCaptcha Challenge
  ◯ Email Verification Link with Redirect
  ◯ Add User to Group
  ◯ Email Domain Filtering (blacklist)
  ◯ Email Domain Filtering (whitelist)
 ❯◯ Custom Auth Challenge Flow (basic scaffolding - not for production)
  ◯ Override ID Token Claims

トリガー関数についてはDo You want to configure Lambda Triggers for Cognitoyと答えると設定できます。

? Do you want to configure Lambda Triggers for Cognito? (Y/n) y
? Which triggers do you want to enable for Cognito (Press  to select,  to toggle all,  to invert selecti
 on)
 ❯◯ Learn More
  ──────────────
  ◉ Create Auth Challenge
  ◯ Custom Message
  ◉ Define Auth Challenge
  ◯ Post Authentication
  ◯ Post Confirmation
  ◉ Verify Auth Challenge Response
 (Move up and down to reveal more choices)

? What functionality do you want to use for Create Auth Challenge (Press  to select,  to toggle all,  to
  invert selection)
 ❯◯ Learn More
  ──────────────
  ◯ MFA w/ Captcha (Creation)
  ◉ Custom Auth Challenge Scaffolding (Creation)
  ◯ Create your own module

ファイルはamplify/backend/function/{APP_NAME}{FUNCTION_NAME}

作成したファイルはこのようなパスで配置されます。

Untracked files:
   (use "git add …" to include in what will be committed)
         amplify/backend/function/ionicamplifyexample3af4138e3af4138eCreateAuthChallenge/
         amplify/backend/function/ionicamplifyexample3af4138e3af4138eDefineAuthChallenge/
         amplify/backend/function/ionicamplifyexample3af4138e3af4138eVerifyAuthChallengeResponse/

package.jsonもそれぞれで配置されていますので、ライブラリを追加する場合はそちらに追加しましょう。

参考

Comment