Amplify(Gen1) React file upload (s3upload-cognitoemail)
- 2024.06.20(木)
GitHubリポジトリのクローン
$ git clone https://github.com/aws-samples/s3uploader-ui.git
Cloning into 's3uploader-ui'...
remote: Enumerating objects: 54, done.
remote: Counting objects: 100% (25/25), done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 54 (delta 4), reused 2 (delta 2), pack-reused 29
Receiving objects: 100% (54/54), 4.37 MiB | 15.24 MiB/s, done.
Resolving deltas: 100% (6/6), done.
依存関係のインストール
$ npm install
up to date, audited 89 packages in 1s
2 packages are looking for funding
run npm fund
for details
found 0 vulnerabilities
Amplifyの初期化
$ amplify init
- 以下のパラメータを選択します。
「Enter a name for the project: s3-uploader-ui」
「Initialize the project with the above configuration:」 ⇒ Yesを入力
「Select the authentication method you want to use:」 ⇒ AWS profile を選択
「Please choose the profile you want to use: 」 ⇒ default を選択
✔ Help improve Amplify CLI by sharing non-sensitive project configurations on failures (y/N) · no
Note: It is recommended to run this command from the root of your app directory
? Enter a name for the project s3uploadcognitoemail
The following configuration will be applied:
Project information
| Name: s3uploadcognitoemail
| Environment: dev
| Default editor: Visual Studio Code
| App type: javascript
| Javascript framework: none
| Source Directory Path: src
| Distribution Directory Path: dist
| Build Command: npm run-script build
| Start Command: npm run-script start
? Initialize the project with the above configuration? Yes
Using default provider awscloudformation
? Select the authentication method you want to use: AWS profile
For more information on AWS Profiles, see:
https: // docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html
? Please choose the profile you want to use default
Adding backend environment dev to AWS Amplify app: d3io949m7y1n3a
Deployment completed.
Deployed root stack s3uploadcognitoemail [ ======================================== ] 4/4
amplify-s3uploadcognitoemail-… AWS::CloudFormation::Stack CREATE_COMPLETE Thu Jun 20 2024 0
UnauthRole AWS::IAM::Role CREATE_COMPLETE Thu Jun 20 2024 0
DeploymentBucket AWS::S3::Bucket CREATE_COMPLETE Thu Jun 20 2024 0
AuthRole AWS::IAM::Role CREATE_COMPLETE Thu Jun 20 2024 0
✔ Help improve Amplify CLI by sharing non-sensitive project configurations on failures (y/N) · no
You can always opt-in by running "amplify configure --share-project-config-on"
Deployment state saved successfully.
✔ 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 add <category>" 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 console" to open the Amplify Console and view your project status
"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 push" to deploy everything
認証(Cognito)の追加
amplify add auth
- 以下を選択する。
・default
・email
──────────────────────────────────────────────
│ │
│ Update available: │
│ Run amplify upgrade for the latest features and fixes! │
│ │
──────────────────────────────────────────────
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? Email
Do you want to configure advanced settings? Yes, I want to make some additional changes.
Warning: you will not be able to edit these selections.
What attributes are required for signing up? Email
Do you want to enable any of the following capabilities?
✅ Successfully added auth resource s3uploadcognitoemail76241edd 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
Storage(s3)の追加
amplify add storage
-
以下のパラメータを選択します。
「Select from one of the below mentioned services」 ⇒ Content (Images, audio, video, etc.) を選択
「Provide bucket name」 ⇒ 任意の新規バケット名を指定
「Who should have access: 」 ⇒ Auth and guest usersを選択
「What kind of access do you want for Authenticated users?」 ⇒ create/update を選択(スペースキー)
「Do you want to add Lambda Trigger for your S3 Bucket? 」 ⇒ N を選択 -
以下が表示されます。
? Select from one of the below mentioned services: Content (Images, audio, video, etc.)
✔ Provide a friendly name for your resource that will be used to label this category in the project: · s3cognitoemail
✔ Provide bucket name: · s3uploadcognitoemail41212ba207394fd69eef1961dc8
✔ Who should have access: · Auth and guest users
✔ What kind of access do you want for Authenticated users? · No items were selected
⚠️ Select at least one option
✔ What kind of access do you want for Authenticated users? · create/update
✔ What kind of access do you want for Guest users? · create/update
✔ Do you want to add a Lambda Trigger for your S3 Bucket? (y/N) · no ●no を入力
⚠️ Specified resource configuration requires Cognito Identity Provider unauthenticated access but it is not enabled.
✅ Successfully updated auth resource locally.
✅ Successfully added resource s3cognitoemail locally
⚠️ If a user is part of a user pool group, run "amplify update storage" to enable IAM group policies for CRUD operations
✅ Some next steps:
"amplify push" builds all of your local backend resources and provisions them in the cloud
"amplify publish" builds all of your local backend and front-end resources (if you added hosting category) and provisions them in the cloud
ホスティング機能(s3 CloudFront)の追加
$ amplify hosting add
-
以下のパラメータを選択します。
「Select the plugin module to execute」 ⇒ Amazon CloudFront and S3 を選択
「Select the environment setup」 ⇒ PROD (S3 with CloudFront using HTTP) を選択
「hosting bucket name」 ⇒ 任意の新規ホスティングバケット名を指定 -
以下が表示されます。
? Select from one of the below mentioned services: Content (Images, audio, video, etc.)
✔ Provide a friendly name for your resource that will be used to label this category in the project: · s3cognitoemail
✔ Provide bucket name: · s3uploadcognitoemail41212ba207394fd69eef1961dc8
✔ Who should have access: · Auth and guest users
✔ What kind of access do you want for Authenticated users? · No items were selected
⚠️ Select at least one option
✔ What kind of access do you want for Authenticated users? · create/update
✔ What kind of access do you want for Guest users? · create/update
✔ Do you want to add a Lambda Trigger for your S3 Bucket? (y/N) · no
⚠️ Specified resource configuration requires Cognito Identity Provider unauthenticated access but it is not enabled.
✅ Successfully updated auth resource locally.
✅ Successfully added resource s3cognitoemail locally
⚠️ If a user is part of a user pool group, run "amplify update storage" to enable IAM group policies for CRUD operations
✅ Some next steps:
"amplify push" builds all of your local backend resources and provisions them in the cloud
"amplify publish" builds all of your local backend and front-end resources (if you added hosting category) and provisions them in the cloud
$ amplify hosting add
✔ Select the plugin module to execute · Amazon CloudFront and S3
✔ hosting bucket name · s3uploadcognitoemail-20240620081905-hostingbucket
Static webhosting is disabled for the hosting bucket when CloudFront Distribution is enabled.
You can now publish your app using the following command:
Command: amplify publish
依存関係のインストール
cat package.json
cat: package.json: No such file or directory
$ npm install
up to date, audited 89 packages in 1s
2 packages are looking for funding
run npm fund
for details
found 0 vulnerabilities
確認
- リージョンを確認する
$ cat ~/.aws/config
[default]
region=ap-northeast-1
output = json
- デフォルトプロファイルの設定値(アクセスキー、シークレットキー、リージョン、出力形式)を確認する。
$ aws configure list --profile default
profile default manual --profile
access_key ****************2B7M shared-credentials-file
secret_key ****************1n6b shared-credentials-file
region ap-northeast-1 config-file ~/.aws/config
backendをデプロイ(amplify push)
- バックエンドリソースをAWSにデプロイする。
$ amplify push
「Are you sure you want to continue? 」 ⇒ Yes を入力
✔ Successfully pulled backend environment dev from the cloud.
Current Environment: dev
┌──────────┬──────────────────────────────┬───────────┬───────────────────
│ Category │ Resource name │ Operation │ Provider plugin │
├──────────┼──────────────────────────────┼───────────┼───────────────────
│ Auth │ s3uploadcognitoemail76241edd │ Create │ awscloudformation │
├──────────┼──────────────────────────────┼───────────┼───────────────────
│ Storage │ s3cognitoemail │ Create │ awscloudformation │
├──────────┼──────────────────────────────┼───────────┼───────────────────┤
│ Hosting │ S3AndCloudFront │ Create │ awscloudformation │
└──────────┴──────────────────────────────┴───────────┴───────────────────┘
✔ Are you sure you want to continue? (Y/n) · yes ●yes を入力する。
Deploying resources into dev environment. This will take a few minutes. ⠇
Deployment completed.
Deploying root stack s3uploadcognitoemail [ ==============================---------- ] 3/4
amplify-s3uploadcognitoemail-… AWS::CloudFormation::Stack UPDATE_COMPLETE_CLEANUP_IN_PR… Thu Jun 20 2024 0
hostingS3AndCloudFront AWS::CloudFormation::Stack CREATE_COMPLETE Thu Jun 20 2024 0
storages3cognitoemail AWS::CloudFormation::Stack CREATE_COMPLETE Thu Jun 20 2024 0
auths3uploadcognitoemail76241… AWS::CloudFormation::Stack CREATE_COMPLETE Thu Jun 20 2024 0
Deployed auth s3uploadcognitoemail76241edd [ ======================================== ] 6/6
UserPoolClientRole AWS::IAM::Role CREATE_IN_PROGRESS Thu Jun 20 2024 0
UserPool AWS::Cognito::UserPool CREATE_COMPLETE Thu Jun 20 2024 0
UserPoolClient AWS::Cognito::UserPoolClient CREATE_COMPLETE Thu Jun 20 2024 0
UserPoolClientWeb AWS::Cognito::UserPoolClient CREATE_COMPLETE Thu Jun 20 2024 0
IdentityPool AWS::Cognito::IdentityPool CREATE_COMPLETE Thu Jun 20 2024 0
IdentityPoolRoleMap AWS::Cognito::IdentityPoolRol… CREATE_COMPLETE Thu Jun 20 2024 0
Deployed storage s3cognitoemail [ ======================================== ] 9/9
S3AuthProtectedPolicy AWS::IAM::Policy CREATE_IN_PROGRESS Thu Jun 20 2024 0
S3AuthPublicPolicy AWS::IAM::Policy CREATE_IN_PROGRESS Thu Jun 20 2024 0
S3AuthPrivatePolicy AWS::IAM::Policy CREATE_IN_PROGRESS Thu Jun 20 2024 0
S3GuestUploadPolicy AWS::IAM::Policy CREATE_IN_PROGRESS Thu Jun 20 2024 0
S3GuestPublicPolicy AWS::IAM::Policy CREATE_IN_PROGRESS Thu Jun 20 2024 0
S3AuthUploadPolicy AWS::IAM::Policy CREATE_IN_PROGRESS Thu Jun 20 2024 0
Deployed hosting S3AndCloudFront [ ======================================== ] 4/4
S3Bucket AWS::S3::Bucket CREATE_COMPLETE Thu Jun 20 2024 0
OriginAccessIdentity AWS::CloudFront::CloudFrontOr… CREATE_COMPLETE Thu Jun 20 2024 0
PrivateBucketPolicy AWS::S3::BucketPolicy CREATE_COMPLETE Thu Jun 20 2024 0
CloudFrontDistribution AWS::CloudFront::Distribution CREATE_COMPLETE Thu Jun 20 2024 0
Deployment state saved successfully.
cognito ユーザに s3 アップロード権限
backendとfrontendをデプロイ(amplify publish)
- amplify publish
$ amplify publish
✔ Successfully pulled backend environment dev from the cloud.
Current Environment: dev
┌──────────┬──────────────────────────────┬───────────┬───────────────────┐
│ Category │ Resource name │ Operation │ Provider plugin │
├──────────┼──────────────────────────────┼───────────┼───────────────────┤
│ Auth │ s3uploadcognitoemail76241edd │ No Change │ awscloudformation │
├──────────┼──────────────────────────────┼───────────┼───────────────────┤
│ Storage │ s3cognitoemail │ No Change │ awscloudformation │
├──────────┼──────────────────────────────┼───────────┼───────────────────┤
│ Hosting │ S3AndCloudFront │ No Change │ awscloudformation │
└──────────┴──────────────────────────────┴───────────┴───────────────────┘
No changes detected
? Do you still want to publish the frontend? Yes
npm ERR! Missing script: "build"
npm ERR!
npm ERR! To see a list of scripts, run:
npm ERR! npm run
npm ERR! A complete log of this run can be found in: /home/ec2-user/.npm/_logs/2024-06-21T02_03_56_613Z-debug-0.log
frontend build command exited with code 1
🛑 1
Resolution: Please report this issue at https ://github.com/aws-amplify/amplify-cli/issues and include the project identifier from: 'amplify diagnose --send-report'
Learn more at: https ://docs.amplify.aws/cli/project/troubleshooting/
Session Identifier: 00221f85-c738-4718-bc7f-58e9d98c8d2e
依存関係の再インストール
- node_modules ディレクトリの削除
$ rm -rf node_modules
- package-lock.json ファイルの削除
$ rm -f package-lock.json
- キャッシュのクリア
$ npm cache clean --force
- 依存関係の再インストール
$ npm install
up to date, audited 89 packages in 1s
2 packages are looking for funding
run npm fund
for details
found 0 vulnerabilities
$ node --version
v18.18.2
- 2024.06.21(金)
frontend
- 元のプロジェクトの srcディレクトリ public ディレクトリを新しいプロジェクトにコピーする。
$ npm start
npm ERR! Missing script: "start"
npm ERR!
npm ERR! Did you mean one of these?
npm ERR! npm star # Mark your favorite packages
npm ERR! npm stars # View packages marked as favorites
npm ERR!
npm ERR! To see a list of scripts, run:
npm ERR! npm run
- エラー発生のため、以下のように調査して対応する。
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See npm help init
for definitive documentation on these fields
and exactly what they do.
Use npm install <pkg>
afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (s3upload-cognitoemail)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /home/ec2-user/environment/s3upload-cognitoemail/package.json:
{
"name": "s3upload-cognitoemail",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes) yes ● yes を入力
ls -ls
$ ls -ls
total 4
0 drwxr-xr-x. 6 ec2-user ec2-user 143 Jun 21 02:03 amplify
4 -rw-r--r--. 1 ec2-user ec2-user 217 Jun 21 02:31 package.json ●新規作成された
0 drwxr-xr-x. 5 ec2-user ec2-user 186 Jun 20 07:29 s3uploader-ui
0 drwxr-xr-x. 2 ec2-user ec2-user 61 Jun 20 07:36 src
$ cat package.json
{
"name": "s3upload-cognitoemail",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "",
"license": "ISC"
}
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "echo \"Error: no test specified\" && exit 1"
},
- package.json を編集する。
{
"name": "s3upload-cognitoemail",
"version": "0.2.0",
"private": true,
"dependencies": {
"@aws-amplify/ui-react": "^5.0.2",
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@cloudscape-design/components": "^3.0.316",
"@cloudscape-design/global-styles": "^1.0.10",
"aws-amplify": "^5.3.1",
"react": "^17.0.0",
"react-dom": "^17.0.0",
"react-router-dom": "^6.13.0",
"react-scripts": "^5.0.1",
"source-map-explorer": "^2.5.3"
},
"scripts": {
"analyze": "source-map-explorer 'build/static/js/*.js'",
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
- 依存関係を更新する。
$ npm install
added 2553 packages, and audited 2554 packages in 3m
282 packages are looking for funding
run npm fund
for details
8 vulnerabilities (2 moderate, 6 high)
To address all issues (including breaking changes), run:
npm audit fix --force
Run npm audit
for details.
$ npm audit
npm audit report
nth-check <2.0.1
Severity: high
Inefficient Regular Expression Complexity in nth-check - https://github.com/advisories/GHSA-rp65-9cf3-cjxr
fix available via npm audit fix --force
Will install react-scripts@3.0.1, which is a breaking change
node_modules/svgo/node_modules/nth-check
css-select <=3.1.0
Depends on vulnerable versions of nth-check
node_modules/svgo/node_modules/css-select
svgo 1.0.0 - 1.3.2
Depends on vulnerable versions of css-select
node_modules/svgo
@svgr/plugin-svgo <=5.5.0
Depends on vulnerable versions of svgo
Compiled successfully!
- 開発サーバを起動する。
npm start
You can now view s3upload-cognitoemail in the browser.
Local: http://localhost:8080
On Your Network: http://172.16.0.167:8080
Note that the development build is not optimized.
To create a production build, use npm run build.
webpack compiled successfully
- Preview Runnning Application で閲覧する。
/src/aws-exports.json を修正
/src (main) $ ls -ls
4 -rw-r--r--. 1 ec2-user ec2-user 1257 Jun 14 15:00 aws-exports.js
- フロントエンドプロジェクトに新しいバックエンドの設定を反映させる。
/src (main) $ amplify pull
Pre-pull status:
Current Environment: dev
┌──────────┬──────────────────────────────┬────
│ Category │ Resource name │ Operation │ Provider plugin │
├──────────┼──────────────────────────────┼────
│ Auth │ s3uploadcognitoemail76241edd │ No Change │ awscloudformation │
├──────────┼──────────────────────────────┼────
│ Storage │ s3cognitoemail │ No Change │ awscloudformation │
├──────────┼──────────────────────────────┼────
│ Hosting │ S3AndCloudFront │ No Change │ awscloudformation │
└──────────┴──────────────────────────────┴────
✔ Successfully pulled backend environment dev from the cloud.
Browserslist: caniuse-lite is outdated. Please run:
npx update-browserslist-db@latest
Why you should do it regularly: https://github.com/browserslist/update-db#readme
✅
Post-pull status:
Current Environment: dev
┌──────────┬──────────────────────────────┬────
│ Category │ Resource name │ Operation │ Provider plugin │
├──────────┼──────────────────────────────┼────
│ Auth │ s3uploadcognitoemail76241edd │ No Change │ awscloudformation │
├──────────┼──────────────────────────────┼────
│ Storage │ s3cognitoemail │ No Change │ awscloudformation │
├──────────┼──────────────────────────────┼────
│ Hosting │ S3AndCloudFront │ No Change │ awscloudformation │
└──────────┴──────────────────────────────┴────
/src (main) $ ls -lt
-rw-r--r--. 1 ec2-user ec2-user 1085 Jun 21 06:21 amplifyconfiguration.json ●新規作成
-rw-r--r--. 1 ec2-user ec2-user 1327 Jun 21 06:21 aws-exports.js ●新規作成
-rw-r--r--. 1 ec2-user ec2-user 1704 Jun 14 08:08 App.js
- 開発サーバを起動する。
$ npm start
Github
- git を初期化する。
$ git init
Initialized empty Git repository in /home/ec2-user/environment/s3upload-cognitoemail/.git/
- Cloud9環境に既存のプロジェクトをGithub に連携する。
$ git remote add origin https://github.com/***/s3upload-cognitoemail.git
- GitHubリポジトリに変更をプッシュする
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin master
- 開発サーバを起動する。
$ npm start
- auth を削除する。
amplify remove auth
amplify push
認証(Cognito)の追加【再】
- amplifyにauthを追加する。
$ 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? Default configuration ●選択
Warning: you will not be able to edit these selections.
How do you want users to be able to sign in? Email ●選択
Do you want to configure advanced settings? No, I am done. ●選択
✅ Successfully added auth resource s3uploadcognitoemailf4d911a4 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
- Amplifyの設定変更をAWSクラウドにpushする。
$ amplify push
- 2024.06.25
Cannot find the distribution folder.
Distribution folder is currently set as:
🛑 Cannot find the distribution folder.
- 以下のように対応する。
$ amplify configure project
$ amplify push
$ amplify publish