Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated May 1, 2024

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 that creates a UserProfile model right next to your auth's resource file.

When you create your first Amplify project using npm create amplify@latest, it will automatically set up the scaffolding for Data and Authentication resources:

1├── amplify/
2│ ├── auth/
3│ │ └── resource.ts
4│ ├── data/
5│ │ └── resource.ts
6│ ├── backend.ts
7│ └── package.json
8├── node_modules/
9├── .gitignore
10├── package-lock.json
11├── package.json
12└── tsconfig.json

As your project grows and you build out your backend, the structure of your project may look like the following:

1├── amplify/
2│ ├── auth/
3│ │ ├── custom-message/
4│ │ │ ├── custom-message.tsx
5│ │ │ ├── handler.ts
6│ │ │ ├── package.json
7│ │ │ └── resource.ts
8│ │ ├── post-confirmation.ts
9│ │ ├── pre-sign-up.ts
10│ │ ├── resource.ts
11│ │ └── verification-email.tsx
12│ ├── data/
13│ │ ├── resolvers/
14│ │ │ ├── list-featured-posts.ts
15│ │ │ └── list-top-10-posts.ts
16│ │ ├── resource.ts
17│ │ └── schema.ts
18│ ├── jobs/
19│ │ ├── monthly-report/
20│ │ │ ├── handler.ts
21│ │ │ └── resource.ts
22│ │ ├── process-featured-posts/
23│ │ │ ├── handler.py
24│ │ │ ├── requirements.txt
25│ │ │ └── resource.ts
26│ │ └── store-top-10-posts/
27│ │ ├── handler.ts
28│ │ └── resource.ts
29│ ├── storage/
30│ │ ├── photos/
31│ │ │ ├── resource.ts
32│ │ │ └── trigger.ts
33│ │ └── reports/
34│ │ └── resource.ts
35│ ├── backend.ts
36│ └── package.json
37├── node_modules/
38├── .gitignore
39├── package-lock.json
40├── package.json
41└── tsconfig.json

Backend resources are defined in resource files using the define* helpers:

amplify/auth/resource.ts
1import { defineAuth } from '@aws-amplify/backend';
2
3export const auth = defineAuth({
4 loginWith: {
5 email: true
6 }
7});

After the resources are defined, they are set up on the backend:

amplify/backend.ts
1import { defineBackend } from '@aws-amplify/backend';
2import { auth } from './auth/resource';
3import { data } from './data/resource';
4
5defineBackend({
6 auth,
7 data
8});

You can extend backends by using the AWS Cloud Development Kit (AWS CDK), which is installed by default as part of the create-amplify workflow. With the CDK, you can build using any AWS service, such as an Amazon S3 bucket that authenticated users have read and write access to. To get started with the CDK, add it to your backend:

amplify/backend.ts
1import * as s3 from 'aws-cdk-lib/aws-s3';
2import { defineBackend } from '@aws-amplify/backend';
3import { auth } from './auth/resource';
4import { data } from './data/resource';
5
6const backend = defineBackend({
7 auth,
8 data
9});
10
11// create the bucket and its stack
12const bucketStack = backend.getStack('BucketStack');
13const bucket = new s3.Bucket(bucketStack, 'Bucket', {
14 blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL
15});
16
17// allow any authenticated user to read and write to the bucket
18const authRole = backend.auth.resources.authenticatedUserIamRole;
19bucket.grantReadWrite(authRole);
20
21// allow any guest (unauthenticated) user to read from the bucket
22const unauthRole = backend.auth.resources.unauthenticatedUserIamRole;
23bucket.grantRead(unauthRole);

Next steps