Project structure
Amplify (Gen 2) backends are defined using TypeScript, and enable you to collocate resources depending on their function. For example, you can author a Post Confirmation trigger for Amazon Cognito right next to your auth's resource file.
First, after creating your first Amplify project with npm create amplify@latest
, it will contain the scaffolding for Data and Authentication resources:
1├── amplify/2│ ├── auth/3│ │ └── resource.ts4│ ├── data/5│ │ └── resource.ts6│ ├── backend.ts7│ └── package.json8├── node_modules/9├── .gitignore10├── package-lock.json11├── package.json12└── tsconfig.json
As your project grows and your backend is built out, the structure of your project may look like the following:
1├── amplify/2│ ├── auth/3│ │ ├── custom-message/4│ │ │ ├── custom-message.tsx5│ │ │ ├── handler.ts6│ │ │ ├── package.json7│ │ │ └── resource.ts8│ │ ├── post-confirmation.ts9│ │ ├── pre-sign-up.ts10│ │ ├── resource.ts11│ │ └── verification-email.tsx12│ ├── data/13│ │ ├── resolvers/14│ │ │ ├── list-featured-posts.ts15│ │ │ └── list-top-10-posts.ts16│ │ ├── resource.ts17│ │ └── schema.ts18│ ├── jobs/19│ │ ├── monthly-report/20│ │ │ ├── handler.ts21│ │ │ └── resource.ts22│ │ ├── process-featured-posts/23│ │ │ ├── handler.py24│ │ │ ├── requirements.txt25│ │ │ └── resource.ts26│ │ └── store-top-10-posts/27│ │ ├── handler.ts28│ │ └── resource.ts29│ ├── storage/30│ │ ├── photos/31│ │ │ ├── resource.ts32│ │ │ └── trigger.ts33│ │ └── reports/34│ │ └── resource.ts35│ ├── backend.ts36│ └── package.json37├── node_modules/38├── .gitignore39├── package-lock.json40├── package.json41└── tsconfig.json
Backend resources are defined in resource
files using the define*
helpers:
1import { defineAuth } from '@aws-amplify/backend';2
3export const auth = defineAuth({4 loginWith: {5 email: true6 }7});
After defining backend resources they are set on the backend:
1import { defineBackend } from '@aws-amplify/backend';2import { auth } from './auth/resource.js';3import { data } from './data/resource.js';4
5defineBackend({6 auth,7 data8});
Backends can be extended using the AWS Cloud Development Kit (CDK). By leveraging CDK you can build using any AWS service, such as an Amazon S3 bucket that authenticated users have read and write access to. You can get started by installing AWS CDK:
1npm add --save-dev aws-cdk-lib
Then add CDK to your backend:
1import * as s3 from 'aws-cdk-lib/aws-s3';2import { defineBackend } from '@aws-amplify/backend';3import { auth } from './auth/resource.js';4import { data } from './data/resource.js';5
6const backend = defineBackend({7 auth,8 data9});10
11// create the bucket and its stack12const bucketStack = backend.getStack('BucketStack');13const bucket = new s3.Bucket(bucketStack, 'Bucket', {14 blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL15});16
17// allow any authenticated user to read and write to the bucket18const authRole = backend.resources.auth.resources.authenticatedUserIamRole;19bucket.grantReadWrite(authRole);20
21// allow any guest (unauthenticated) user to read from the bucket22const unauthRole = backend.resources.auth.resources.unauthenticatedUserIamRole;23bucket.grantRead(unauthRole);