プロジェクト構造
Amplify Gen 2バックエンドはTypeScriptを使用して定義され、機能に応じてリソースを並べて配置することができます。例えば、Amazon Cognitoのポスト確認トリガーを作成して、UserProfileモデルを作成することができ、それをauthのリソースファイルの横に配置できます。
npm create amplify@latestを使用して最初のAmplifyプロジェクトを作成すると、DataとAuthenticationリソースのスキャフォルディングが自動的に設定されます:
├── amplify/│ ├── auth/│ │ └── resource.ts│ ├── data/│ │ └── resource.ts│ ├── backend.ts│ └── package.json├── node_modules/├── .gitignore├── package-lock.json├── package.json└── tsconfig.jsonプロジェクトが成長し、バックエンドを構築していくと、プロジェクトの構造は次のようになる可能性があります:
├── amplify/│ ├── auth/│ │ ├── custom-message/│ │ │ ├── custom-message.tsx│ │ │ ├── handler.ts│ │ │ ├── package.json│ │ │ └── resource.ts│ │ ├── post-confirmation.ts│ │ ├── pre-sign-up.ts│ │ ├── resource.ts│ │ └── verification-email.tsx│ ├── data/│ │ ├── resolvers/│ │ │ ├── list-featured-posts.ts│ │ │ └── list-top-10-posts.ts│ │ ├── resource.ts│ │ └── schema.ts│ ├── jobs/│ │ ├── monthly-report/│ │ │ ├── handler.ts│ │ │ └── resource.ts│ │ ├── process-featured-posts/│ │ │ ├── handler.py│ │ │ ├── requirements.txt│ │ │ └── resource.ts│ │ └── store-top-10-posts/│ │ ├── handler.ts│ │ └── resource.ts│ ├── storage/│ │ ├── photos/│ │ │ ├── resource.ts│ │ │ └── trigger.ts│ │ └── reports/│ │ └── resource.ts│ ├── backend.ts│ └── package.json├── node_modules/├── .gitignore├── package-lock.json├── package.json└── tsconfig.jsonバックエンドリソースはresourceファイルでdefine*ヘルパーを使用して定義されます:
amplify/auth/resource.ts
import { defineAuth } from '@aws-amplify/backend';
export const auth = defineAuth({ loginWith: { email: true }});リソースが定義されたら、バックエンドで設定されます:
amplify/backend.ts
import { defineBackend } from '@aws-amplify/backend';import { auth } from './auth/resource';import { data } from './data/resource';
defineBackend({ auth, data});AWS Cloud Development Kit (AWS CDK)を使用してバックエンドを拡張できます。AWS CDKはcreate-amplifyワークフローの一部としてデフォルトでインストールされています。CDKを使用すると、認証されたユーザーが読み取りと書き込みアクセスを持つAmazon S3バケットなど、任意のAWSサービスを使用して構築できます。CDKを使い始めるには、バックエンドに追加してください:
amplify/backend.ts
import * as s3 from 'aws-cdk-lib/aws-s3';import { defineBackend } from '@aws-amplify/backend';import { auth } from './auth/resource';import { data } from './data/resource';
const backend = defineBackend({ auth, data});
// create the bucket and its stackconst bucketStack = backend.getStack('BucketStack');const bucket = new s3.Bucket(bucketStack, 'Bucket', { blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL});
// allow any authenticated user to read and write to the bucketconst authRole = backend.auth.resources.authenticatedUserIamRole;bucket.grantReadWrite(authRole);
// allow any guest (unauthenticated) user to read from the bucketconst unauthRole = backend.auth.resources.unauthenticatedUserIamRole;bucket.grantRead(unauthRole);