既存のプロジェクトでプロダクション環境をAmplifyで構築していた。
ローンチ前だったので、プロダクション環境化で構築を行っていたが、そろそろローンチが近づいてきたのでAmplifyでDev環境を構築することにした。

既存環境

ざっくりでこんな感じの環境を持っていた。

$ amplify status

Current Environment: prd

| Category | Resource name                       | Operation | Provider plugin   |
| -------- | ----------------------------------- | --------- | ----------------- |
| Auth     | userPoolGroups                      | No Change | awscloudformation |
| Auth     | xxxxxx                              | No Change | awscloudformation |
| Function | AdminQueriesxxxxxx                  | No Change | awscloudformation |
| Function | xxxxxxCustomMessage                 | No Change | awscloudformation |
| Function | xxxxxxPostConfirmation  | No Change | awscloudformation |
| Api      | AdminQueries                        | No Change | awscloudformation |
| Api      | red                                 | No Change | awscloudformation |

Dev環境を構築してみる

$ amplify env add
Note: It is recommended to run this command from the root of your app directory
? Do you want to use an existing environment? No
? Enter a name for the environment dev
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 red
Adding backend environment dev to AWS Amplify Console app: dplqbaaykltjo
⠏ Initializing project in the cloud...

CREATE_IN_PROGRESS amplify-xxxxxx-dev-xxxxxx AWS::CloudFormation::Stack Sat Feb 15 2020 11:21:18 GMT+0900 (Japan Standard Time) User Initiated             
CREATE_IN_PROGRESS DeploymentBucket       AWS::S3::Bucket            Sat Feb 15 2020 11:21:22 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS AuthRole               AWS::IAM::Role             Sat Feb 15 2020 11:21:22 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS UnauthRole             AWS::IAM::Role             Sat Feb 15 2020 11:21:22 GMT+0900 (Japan Standard Time)                            
CREATE_IN_PROGRESS AuthRole               AWS::IAM::Role             Sat Feb 15 2020 11:21:23 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠙ Initializing project in the cloud...

CREATE_IN_PROGRESS UnauthRole AWS::IAM::Role Sat Feb 15 2020 11:21:24 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠏ Initializing project in the cloud...

CREATE_IN_PROGRESS DeploymentBucket AWS::S3::Bucket Sat Feb 15 2020 11:21:24 GMT+0900 (Japan Standard Time) Resource creation Initiated
⠋ Initializing project in the cloud...

CREATE_COMPLETE AuthRole AWS::IAM::Role Sat Feb 15 2020 11:21:38 GMT+0900 (Japan Standard Time) 
⠙ Initializing project in the cloud...

CREATE_COMPLETE UnauthRole AWS::IAM::Role Sat Feb 15 2020 11:21:39 GMT+0900 (Japan Standard Time) 
⠙ Initializing project in the cloud...

CREATE_COMPLETE DeploymentBucket       AWS::S3::Bucket            Sat Feb 15 2020 11:21:45 GMT+0900 (Japan Standard Time) 
CREATE_COMPLETE amplify-xxxxx-dev-xxxxxx AWS::CloudFormation::Stack Sat Feb 15 2020 11:21:48 GMT+0900 (Japan Standard Time) 
✔ Successfully created initial AWS cloud resources for deployments.
✔ Initialized provider successfully.

 You've opted to allow users to authenticate via Facebook.  If you haven't already, you'll need to go to https://developers.facebook.com and create an App ID. 

ではDev環境追加できたっぽいので、プッシュしてみる。

$ amplify push 
✔ Successfully pulled backend environment dev from the cloud.

Current Environment: dev

| Category | Resource name                       | Operation | Provider plugin   |
| -------- | ----------------------------------- | --------- | ----------------- |
| Auth     | userPoolGroups                      | Create    | awscloudformation |
| Auth     | xxxxxx                              | Create    | awscloudformation |
| Function | AdminQueriesxxxxxx                  | Create    | awscloudformation |
| Function |  xxxxxxCustomMessage                | Create    | awscloudformation |
| Function |  xxxxxxPostConfirmation             | Create    | awscloudformation |
| Api      | AdminQueries                        | Create    | awscloudformation |
| Api      | red                                 | Create    | awscloudformation |
? Are you sure you want to continue? Yes 
.........

エラー・・。順番対応していく。

Following resources failed

Resource Name: MFALambda (AWS::Lambda::Function)
Event Type: create
Reason: The runtime parameter of nodejs8.10 is no longer supported for creating or updating AWS Lambda functions. We recommend you use the new runtime (nodejs12.x) while creating or updating functions. (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: 23ffa231-3003-4148-8f3c-a865cbb3542c)


Resource Name: amplify-xxxxx-xxxxx (AWS::IAM::Policy)
Event Type: create
Reason: Resource creation cancelled

Resource Name: amplify-xxxxx-xxxxx-authredxxxxx-UserPoolPostConfirmationLambdaInvokePermis-xxxxx (AWS::Lambda::Permission)
Event Type: create
Reason: Resource creation cancelled

Resource Name: amplify-xxxxx-xxxxxx-authredxxxxx-UserPoolCustomMessageLambdaInvokePermissio-xxxxx (AWS::Lambda::Permission)
Event Type: create
Reason: Resource creation cancelled


✖ An error occurred when pushing the resources to the cloud

Resource is not in the state stackUpdateComplete
An error occured during the push operation: Resource is not in the state stackUpdateComplete

LambdaのNode8をNode12へアップデート

これらのファイルでクラウドフォーメーションの情報を管理している。実際に構築してアプリによってここで修正する対象は変動あると思う。(作り方によって必要なLambda関数の工数も場所も変わってくるので)

私は今回、amplify/以下をnodejs8で検索し、それらをnodejs12.xに置換した。

amplify/backend/auth/xxxxxx/xxxxxx-cloudformation-template.yml
amplify/backend/auth/userPoolGroups/template.json
amplify/backend/function/AdminQueriesxxxxxx/AdminQueriesxxxxxx-cloudformation-template.json
amplify/backend/function/xxxxxxCustomMessage/xxxxxxCustomMessage-cloudformation-template.json
amplify/backend/function/xxxxxxPostConfirmation/xxxxxxPostConfirmation-cloudformation-template.json

これでLambdaのバージョンエラー問題は解決。
それ以外のエラーが謎すぎる、S3バケットを新規に作成しようとしているのに既に存在すると怒られる。
(ここのS3バケットはCognitoを構築しようとするときに自動的に作成されるもなので意図したものではない)

とりあえず、以前にごにょごにょメンバーがDevを作ろうとしていたみたいだから、ひとまずDevをRemoveして再構築してみる。

$ amplify env remove dev
$ amplify env add
....
$ amplify push
Resource Name: amplify-xxxxx-xxxxx (AWS::IAM::Policy)
Event Type: create
Reason: Resource creation cancelled

Resource Name: amplify-xxxxx-xxxxx-authredxxxxx-UserPoolPostConfirmationLambdaInvokePermis-xxxxx (AWS::Lambda::Permission)
Event Type: create
Reason: Resource creation cancelled

Resource Name: amplify-xxxxx-xxxxxx-authredxxxxx-UserPoolCustomMessageLambdaInvokePermissio-xxxxx (AWS::Lambda::Permission)
Event Type: create
Reason: Resource creation cancelled

ダメだーエラー変わらず。Devではなく、Tstに変えてみるか。

$ amplify env remove dev
...
$ amplify env add tst
...
$ amplify push

S3の構築箇所を乗り越えた!
ElasticSearchの構築が長いのでしばらく待つ・・。(30分近く)

GraphQL endpoint: https://xxxxxx.appsync-api.ap-northeast-1.amazonaws.com/graphql
Hosted UI Endpoint: https://xxxxxx-xxxxxx-tst.auth.ap-northeast-1.amazoncognito.com/
Test Your Hosted UI Endpoint: https://xxxxxx-xxxxxx-tst.auth.ap-northeast-1.amazoncognito.com/login?response_type=code&client_id=xxxxxx&redirect_uri=https://xxxxxx.jp/thankyou/

感想

生まれたてのフレームワークだからまだまだ色々と安定しない時があるけど、思想が本当に好きなのでついていきたいと思いました。うちの会社ではフルスタックエンジニアを随時募集or育成しているので浸透させたいと思います。

https://www.ragate.co.jp/