AWS

AWS Amplify CLIで既存のCognito User PoolをImportする

個人的な理由でCognito User PoolsをAWS Amplify CLIの管理外に置こうとしています。 その場合のAmplify CLIでの対応についてまとめました。 Amplify initは必須 ampli […]

広告ここから
広告ここまで

個人的な理由でCognito User PoolsをAWS Amplify CLIの管理外に置こうとしています。

その場合のAmplify CLIでの対応についてまとめました。

Amplify initは必須

amplify initでセットアップは事前に済ませましょう。この時選択するAWSのprofileに設定されているregionが、Cognitoのあるリージョンと同じであることが必須です。

下の例のように、No Cognito User Poolsと言われた場合はまずregionの設定を疑いましょう。

% amplify import auth
 Using service: Cognito, provided by: awscloudformation
 No Cognito User Pools were found in the configured region: us-east-1.

amplify import authでインポートする

amplify import authコマンドを実行すると、リージョン内にあるUser Poolsを自動で探してくれます。

% amplify import auth  
 Using service: Cognito, provided by: awscloudformation
 ? What type of auth resource do you want to import? … 
 ❯ Cognito User Pool and Identity Pool
   Cognito User Pool only

? Select the User Pool you want to import: … 
 cognito-webauthn-UserPool (us-west-2_XXXXP)
 webauthn-cognito2-UserPool (us-west-2_XXXXc)
 (Type in a partial name or scroll up and down to reveal more choices)

成功すれば、以下のようにログが出力されます。

% amplify import auth 
 Using service: Cognito, provided by: awscloudformation
 ✔ What type of auth resource do you want to import? · Cognito User Pool only
 ✔ Select the User Pool you want to import: · us-west-2_XXXXP

 ✔ Only one Web app client found: 'my-app' was automatically selected.
 ✔ Only one Native app client found: 'aws-amplify' was automatically selected.
 ✔ Federated identity providers are not configured, no OAuth configuration needed.
 ✅ Cognito User Pool 'cognito-webauthn-UserPool' was successfully imported.
 Next steps:
 This resource will be available for GraphQL APIs ('amplify add api')
 Use Amplify libraries to add signup, signin, signout capabilities to your client
 application.
 iOS: https://docs.amplify.aws/lib/auth/getting-started/q/platform/ios
 Android: https://docs.amplify.aws/lib/auth/getting-started/q/platform/android
 JavaScript: https://docs.amplify.aws/lib/auth/getting-started/q/platform/js 

amplify statusで確認

amplify statusでもImportステータスで表示されます。

Current Environment: dev

| Category | Resource name               | Operation | Provider plugin   |
| -------- | --------------------------- | --------- | ----------------- |
| Auth     | ionicamplifyexample853f2b6c | Import    | awscloudformation |

あとはamplify pushamplify pubishなどでAmplifyアプリをAWSにアップしましょう。

Authまわりを実装する前に、aws-exports.jsをチェック

amplify import authしただけでは、Authの設定がimportされていない様子でした。もしAuth系のデータがファイルに反映されていない場合、amplify pushなどでAWS側とデータを同期させることで、情報が更新されます。

トラブルシューティング

“No Cognito User Pools were found in the configured region: us-east-1”

amplify initでセットアップしたリージョンにCognito User Poolsが見つかりませんでした。リージョンが異なる場合は、新しくamplify initし直すか、ユーザープールをAmplify側のリージョンに作り直しましょう。

“❯ The selected Cognito User Pool does not have at least 1 Native app client configured. Native app clients are app clients with a client secret.”

User Poolsのアプリクライアントが存在しない。またはクライアントシークレットを持つものが存在しないのどちらかです。新しくアプリクライアントを作成してやりましょう。

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts