AWS Amplify CLIの amplify auth push の挙動について

AWS Amplify CLI のREADMEを見ると、

amplify auth push

Provisions only Auth cloud resources with the latest local developments.

https://github.com/aws-amplify/amplify-cli/tree/master/packages/amplify-category-auth

という記述がありました。

気になったので試してみたところ、思っていた挙動と違っていたのでメモを残します。

なお、手順などで勘違いしている部分があれば、ご指摘いただけるとありがたいです。

 
目次

 

環境

 

勘違いしていたこと

手元の環境で

のモジュールがあった時に amplify auth push をすると、Authモジュールだけがpushされると思っていました。

ただ、実際には、API・Authの両方がpushされました。

 

新規作成した時の流れ

init

特に変わりない、一般的なinitです。

$ amplify init

Note: It is recommended to run this command from the root of your app directory
? Enter a name for the project auth_before_api
? Enter a name for the environment dev
? Choose your default editor: Visual Studio Code
? 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

 

Authモジュールの追加

APIを作る前に、Authモジュールを追加しました。

今回はデフォルト設定にしました。

$ amplify auth add
Using service: Cognito, provided by: awscloudformation
 
 The current configured provider is Amazon Cognito. 
 
 Do you want to use the default authentication and security configuration? Default configuration
 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 configure advanced settings? No, I am done.

 

APIモジュールの追加

次にAPIモジュールを追加します。

$ amplify api add
? Please select from one of the below mentioned services GraphQL
? Provide API name: authbeforeapi
? Choose an authorization type for the API API key
? Do you have an annotated GraphQL schema? No
? Do you want a guided schema creation? No
? Provide a custom type name MyType
Creating a base schema for you...

GraphQL schema compiled successfully.

 

状態確認

両方ともpushされていません。

$ amplify status

Current Environment: dev

| Category | Resource name         | Operation | Provider plugin   |
| -------- | --------------------- | --------- | ----------------- |
| Auth     | authbeforeapibf6779e5 | Create    | awscloudformation |
| Api      | authbeforeapi         | Create    | awscloudformation |

 

amplify auth push

Authモジュールだけpushするつもりでした。

$ amplify auth push

Current Environment: dev

| Category | Resource name         | Operation | Provider plugin   |
| -------- | --------------------- | --------- | ----------------- |
| Auth     | authbeforeapibf6779e5 | Create    | awscloudformation |
? Are you sure you want to continue? Yes

GraphQL schema compiled successfully.
...

? Do you want to generate code for your newly created GraphQL API Yes
? Choose the code generation language target javascript
? Enter the file name pattern of graphql queries, mutations and subscriptions src/graphql/**/*.js
? Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions Yes
? Enter maximum statement depth [increase from default if your schema is deeply nested] 2

 
しばらく待つと、想定していなかった

  • APIのエンドポイント
  • API Key

が作成されました。

✔ All resources are updated in the cloud

GraphQL endpoint: https://xxx.appsync-api.region.amazonaws.com/graphql
GraphQL API KEY: da2-xxx

 
状況を確認しても、両方ともpushされていました。

$ amplify status

Current Environment: dev

| Category | Resource name         | Operation | Provider plugin   |
| -------- | --------------------- | --------- | ----------------- |
| Auth     | authbeforeapibf6779e5 | No Change | awscloudformation |
| Api      | authbeforeapi         | No Change | awscloudformation |

GraphQL endpoint: https://xxx.appsync-api.region.amazonaws.com/graphql
GraphQL API KEY: da2-xxx

 

両方とも更新して、再度 amplify auth push した時の流れ

Authモジュールを更新します。

$ amplify auth update
Please note that certain attributes may not be overwritten if you choose to use defaults settings.

You have configured resources that might depend on this Cognito resource.  Updating this Cognito resource could have unintended side effects.

Using service: Cognito, provided by: awscloudformation
 What do you want to do? Walkthrough all the auth configurations
 Select the authentication/authorization services that you want to use: User Sign-Up & Sign-In only (Best used with a cloud API only)
 Multifactor authentication (MFA) user login options: OFF
 Email based user registration/forgot password: Enabled (Requires per-user email entry at registration)
 Please specify an email verification subject: Your verification code
 Please specify an email verification message: Your verification code is {####}
 Do you want to override the default password policy for this User Pool? No
 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? No
? Do you want to configure Lambda Triggers for Cognito? No
Successfully updated resource authbeforeapibf6779e5 locally
...

 
続いて、APIもupdateします。

$ amplify api update
? Please select from one of the below mentioned services GraphQL
? Choose an authorization type for the API Amazon Cognito User Pool

GraphQL schema compiled successfully.

 
状態を確認します。

$ amplify status

Current Environment: dev

| Category | Resource name         | Operation | Provider plugin   |
| -------- | --------------------- | --------- | ----------------- |
| Auth     | authbeforeapibf6779e5 | Update    | awscloudformation |
| Api      | authbeforeapi         | Update    | awscloudformation |

 
Authモジュールをpushしてみます。

$ amplify auth push

Current Environment: dev

| Category | Resource name         | Operation | Provider plugin   |
| -------- | --------------------- | --------- | ----------------- |
| Auth     | authbeforeapibf6779e5 | Update    | awscloudformation |
? Are you sure you want to continue? Yes

 
APIとAuthの両方が更新されました。

$ amplify status

Current Environment: dev

| Category | Resource name         | Operation | Provider plugin   |
| -------- | --------------------- | --------- | ----------------- |
| Auth     | authbeforeapibf6779e5 | No Change | awscloudformation |
| Api      | authbeforeapi         | No Change | awscloudformation |